首页 > 代码库 > java对文本数据按行用冒泡法进行排序

java对文本数据按行用冒泡法进行排序

package org.ansj.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.PrintStream;
import java.util.Vector;
public class bijiao{
public static void main(String[] args) {
try {
FileInputStream fis=new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.txt");
File fileName = new File("C:\\Users\\Administrator\\Desktop\\1.txt"); //这个是测试时用的语料文件,非用户输入
BufferedReader reader = new BufferedReader(new FileReader(fileName));

BufferedWriter bw =new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\2.txt"));
// Scanner scan=new Scanner(reader);
Vector<Integer> v=new Vector<Integer>();
String line;
while( ( line=reader.readLine() )!=null )
{
Scanner scan=new Scanner(line);
String[] seg = line.split(" ");
int[] ds=new int[seg.length];
for(int i = 0; i<seg.length;i++)
{
ds[i]=Integer.valueOf(seg[i]);
v.add(scan.nextInt());
bubbleSort(v);
}

// PrintStream ps=new PrintStream("C:\\Users\\Administrator\\Desktop\\2.txt");
for(int b:v){
System.out.print(b+" ");
// ps.append(Integer.toString(b)+‘ ‘);
bw.write(b+" ");
}
System.out.println( );
// ps.append("\r\n");
bw.write("\r\n");
// ps.close();
v.clear();
}
fis.close();
bw.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
static public void bubbleSort(Vector<Integer> v){
for(int b=v.size()-1;b>0;b--)
for(int i=0;i<b;i++){
int v1=v.get(i).intValue(),v2=v.get(i+1).intValue();
if(v1>v2){
v.set(i,v2); v.set(i+1,v1);
}
}
}
}

 

 

输入:技术分享

输出:技术分享

 

java对文本数据按行用冒泡法进行排序