package week_3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class DataSort {
static String srcPath = "c:\\largeW.txt";
static String bubbleSortPath = "largeW_bubble.txt";
static String mergeSortPath = "largeW_merge.txt";
public static void main(String args[]) {
try{
File file = new File(srcPath);
FileInputStream fis = new FileInputStream(file);
InputStreamReader inputReader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String lineData = "";
int num = 0;
List<Integer> list = new ArrayList<Integer>(); //建立一个列表数组
while((lineData=bufferedReader.readLine()) != null){ //判断是否是有数据
num = num + 1;
list.add(Integer.parseInt(lineData.trim())); //将数据加到数组里
}
Integer[] lineDataArray = list.toArray(new Integer[0]);
//开始冒泡排序
long timeStart = System.currentTimeMillis(); //开始冒泡排序
BubbleSort(lineDataArray); //冒泡排序
long timeEnd = System.currentTimeMillis(); //冒泡排序结束
System.out.println("冒泡排序所花的时间是"+(timeEnd-timeStart));
ResultSortWriteTxt(lineDataArray,0);
}catch(Exception e) {
}
}
//把排序结果写到结果文本文件
public static void ResultSortWriteTxt(Integer[] lineDataArray,int n) throws IOException{
File file = null;
if (n==0) { //n=0代表是冒泡排序
file = new File(bubbleSortPath);
}else if(n==1) { //n=1代表是归并排序
file = new File(mergeSortPath);
}
if(!file.exists()) { //如果文件已经排序好了
file.createNewFile();
}
FileOutputStream fos= new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos,"gb2312");
BufferedWriter writer = new BufferedWriter(osw);
for(int i=0;i<lineDataArray.length;i++) {
writer.write(lineDataArray[i].toString()+"\r\n");
writer.flush();
}
fos.close();
osw.close();
writer.close();
}
//下面是实现冒泡排序,两两对比,大的排在前面
public static void BubbleSort(Integer[] Array) throws IOException{
Integer[] lineDataArray = (Integer[]) Array;
int temp = 0;
int len = lineDataArray.length; //取文件数据的长度
for(int i=0;i<len+1;i++) {
for(int j=1;j<len-i-1;j++) {
if(lineDataArray[i]<lineDataArray[j]){
temp = lineDataArray[i];
lineDataArray[i] = lineDataArray[j];
lineDataArray[j] = temp;
}
}
//System.out.println(lineDataArray[i]);
}
}
//下面是实现归并排序
public static void mergeSort(int[] src,int[] des){
//从下标1开始存放待排序数组
int len = 1, n=src.length-1;
while(len<n){
MergePass(src,des,n,len); //待排序序列从数组src中传到des中
len = 2*len;
MergePass(des,src,n,len); //待排序序列从数组des中传到src中
}
}
//一趟归并排序算法
public static void MergePass(int[] src,int[] des,int n,int h) {
int i=1;
while(i<=(n-2*h+1)) {
Merge(src,des,i,i+h-1,i+2*h-1);
i+=2*h;
}
if(i<(n-h+1))
Merge(src,des,i,i+h-1,n);
else {
while(i<=n) {
des[i] = src[i];
i++;
}
}
}
//一次归并算法
public static void Merge(int[] src,int[] des,int s,int m,int t) {
int i,j,k;
i=s; j = m+1; k = s;
while(i<=m && j<=t) {
if(src[i]<=src[j]){
src[k++] = src[i++] ;
}else{
src[k++] = src[j++] ;
}
}
if(i<=m)
while(i<=m){
des[k++] = src[i++];
}
else{
while(j<=t) {
des[k++] = src[j++];
}
}
}
}
未完待续
第三周作业——冒泡排序和归并排序
最新推荐文章于 2023-09-24 09:19:30 发布