算法分析与设计——快排和插入排序的实现


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;


public class sort {
	public static void datagentare()
	{
//		create the number of 2k data
		int[] arry = new int[2000];
		for(int i = 0;i < arry.length;i++)
		{
			arry[i]=(int)Math.floor(1000*Math.random())+1;
			//System.out.println(arry[i]);
		}
//		save data
		File file = new File("/Users/emily/Documents/arry2.txt");
		try {
			FileWriter out = new FileWriter(file);
			for(int j = 0;j < 2000; j++)
			{
				out.write(arry[j]+"\n");
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static int[] Data(String file_path){  //read data
		int[] data = new int[2000];
		File file = new File(file_path);
		if(file.isFile() && file.exists())
//			�ж��ļ��Ƿ����
		{
			InputStreamReader read;
			try {
				read = new InputStreamReader(new FileInputStream(file));
				BufferedReader bufferedReader = new BufferedReader(read);
				for(int i = 0 ; i< 2000 ; i++)
				{
					try {
						data[i] = Integer.parseInt( bufferedReader.readLine());
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		for(int j = 0;j < 2000; j++)
//		{
//			System.out.println(data[j]);
//		}
//		
		return data;
	}
	private static int partition(int[] array,int beg,int end){  
        int last = array[end];  
        int i = beg -1;  
        for (int j = beg; j <= end-1; j++) {  
            if(array[j] <= last){  
                i++;  
                if(i != j){  
                    array[i] = array[i]^array[j];  
                    array[j] = array[i]^array[j];  
                    array[i] = array[i]^array[j];  
                }  
            }  
        }  
        if((i+1) != end){  
            array[i+1] = array[i+1]^array[end];  
            array[end] = array[i+1]^array[end];  
            array[i+1] = array[i+1]^array[end];  
        }  
        return i+1;  
    }
	
	public static void quickSort(int[] array)
	{  
        if(array != null){  
            quickSort(array, 0, array.length-1);  
        }  
    }  
      
    private static void quickSort(int[] array,int beg,int end)
    {  
        if(beg >= end || array == null)  
            return;  
        int p = partition(array, beg, end);  
        quickSort(array, beg, p-1);  
        quickSort(array, p+1, end);  
    }
    public static void insertSort(int a[]){  
        int length=a.length; //数组长度  
        int j;               //当前值的位置  
        int i;               //指向j前的位置  
        int key;             //当前要进行插入排序的值  
        //从数组的第二个位置开始遍历值  
        for(j=1;j=0 && a[i]>key){  
                a[i+1]=a[i]; //将a[i]值后移  
                i--;         //i前移  
            }//跳出循环(找到要插入的中间位置或已遍历到0下标)  
            a[i+1]=key;    //将当前值插入  
        }  
    }  
    public static void main(String args[])
    {
    	int[] test = new int[2000];
    		datagentare();
    		test = Data("/Users/emily/Documents/arry2.txt");
//    		quickSort(test);
    		insertSort(test);
    		for(int i = 0 ; i< 2000 ; i++)
    		{
    			System.out.println(test[i]);
    		}
    		File file = new File("/Users/emily/Documents/Sort_out.txt");
    		try {
    			FileWriter out = new FileWriter(file);
    			for(int j = 0;j < 2000; j++)
    			{
    				out.write(test[j]+"\n");
    			}
    			out.close();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    }

}

你可能感兴趣的:(算法分析与设计——快排和插入排序的实现)