第三周作业——冒泡排序和归并排序

来源:互联网 发布:火药阴谋是否正义知乎 编辑:程序博客网 时间:2024/05/21 09:38
package test;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class test {

static int len=0;
public static void bubbleSort(String bssz[]) throws IOException
{
int comp=0;
String a=" ";
 for(int i =0;i <len-1;i++)
 {  
       for(int j =i+1;j<len;j++)
       {  
           comp=bssz[i].compareTo(bssz[j]);  
           if(comp>0)
           {  
               a=bssz[i];  
               bssz[i]=bssz[j];  
               bssz[j]=a;  
           }  

       } 
 }
 FileWriter fw =new FileWriter("F:\\largeW_bubble.txt",true);  
          BufferedWriter writer =new BufferedWriter(fw);  
          for(int i=0;i<len;i++){  
              writer.write(bssz[i]+"\r");  
              writer.newLine();
          }  
 writer.flush();
 writer.close();
}
public static void partition(String[] arr,int head,int tail){  
       if(head < tail){  
           int mid = (head+tail )/2;  
           partition(arr,head,mid);
           partition(arr,mid+1,tail);
           mergeSort(arr,head,tail,mid);  
       }  
   }  
         
 
   public static void mergeSort(String[] arr,int head,int tail,int mid){  
       String[] tArr = new String[tail];
       int tArrIndex = 0;  
       int part1ArrIndex = head;
       int part2ArrIndex = mid+1;
       int l;  
             
       while((part1ArrIndex <= mid)&&(part2ArrIndex <= tail)){ 
           l = arr[part1ArrIndex].compareTo(arr[part2ArrIndex]);
           if(l < 0){  
               tArr[tArrIndex ++] = arr[part1ArrIndex ++];  
           }  
           else{  
               tArr[tArrIndex ++] = arr[part2ArrIndex ++];  
           }  
       }  
             
       while(part1ArrIndex < mid){  
           tArr[tArrIndex ++] = arr[part1ArrIndex ++];  
       }  
             
       while(part2ArrIndex < mid){  
           tArr[tArrIndex ++] = arr[part2ArrIndex ++];  
       }  
      
   }  

public static void main(String args[]) throws IOException
{
BufferedReader ip = new BufferedReader(new FileReader("F:\\largeW.txt"));
while((ip.readLine())!=null)
{
len++;
}
ip.close();
System.out.print("文件数据总量: ");  
        System.out.println(len);  
        
String[] sz=new String[len];
FileReader FR1 =new FileReader("F:\\largeW.txt");  
        BufferedReader BR1=new BufferedReader(FR1);  
        String a=" ";
    int t=0;
        while((a=BR1.readLine()) !=null){  
            sz[t]=a;  
            t++;  
        }  
      
        long startTime =System.currentTimeMillis();
        bubbleSort(sz);  
        long endTime =System.currentTimeMillis();
        System.out.println("冒泡排序运行时间: "+(endTime -startTime) +"ms");  
          
        
        long startTime1=System.currentTimeMillis();
        partition(sz,0,len-1);  
        long endTime1=System.currentTimeMillis();
        System.out.println("归并排序运行时间: "+(endTime1-startTime1)+"ms");  
          
        FileWriter fw1=new FileWriter("F:\\largeW_merge.txt",true);  
        BufferedWriter writer1=new BufferedWriter(fw1);  
        for(int i=0;i<len;i++){  
            writer1.write(sz[i]+"\r");  
            writer1.newLine();
        }  
 writer1.flush();
 writer1.close();

}

}


0 0