java对文本数据按行用冒泡法进行排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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对文本数据按行用冒泡法进行排序的主要内容,如果未能解决你的问题,请参考以下文章