冒泡排序

来源:互联网 发布:淘宝页头 编辑:程序博客网 时间:2024/06/07 11:23
package suanfazuoye_3;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Arrays; public class suanfa_Rank {         BufferedReader br = null;    BufferedWriter bw = null;    static String [] array = null;         public suanfa_Rank() throws IOException{        br = new BufferedReader(new FileReader(new File("E:"+File.separator+"largeW.txt")));        bw = new BufferedWriter(new FileWriter(new File("f:"+File.separator+"largeW_bubble.txt")));    }   //数据排序    public void sort(){ //System.out.println(array.length);    String tmp;    for(int i = 0;i < array.length-1;i++){           for(int j = 0;j < array.length - i-1;j++ ){           if (Double.parseDouble(array[j]) > Double.parseDouble(array[j+1])) {           tmp = array[j];                   array[j] = array[j+1];                   array[j+1] = tmp;           }}                }    }    //输出已经排好序的数据的文件    public void setArray() throws IOException{        String tmp = "";        for(int r = 0;r < array.length;r++){            tmp = array[r] + ",";            bw.write(tmp);            tmp = "";        }        bw.close();    }  //获取文件中的数据    public String[] getArray() throws IOException{        String  tmp[] = new String[1000000];        int num = 0;        while(br.read() != -1){            tmp[num] = br.readLine();//            System.out.println(tmp[num]);            num++;        }        tmp = Arrays.copyOf(tmp, num);        br.close();        return tmp;    }         public static void main(String[] args) throws IOException {        suanfa_Rank test = new suanfa_Rank();        array = test.getArray();        test.sort();        test.setArray();    }}

0 0
原创粉丝点击