java实现冒泡,快排,堆排,归并。

     public static void main(String[] args) {
        int [] arr = {989, 261, 326, 486, 963, 19, 910, 866, 656, 143, 603, 762, 808};
        long startTime=System.currentTimeMillis();   //获取开始时间
        //quickSort(arr,0,arr.length-1);
        maoPao(arr);
        //sort(arr);
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
        System.out.println(Arrays.toString(arr));
        System.out.println(arr.length);
    }
    public static void quickSort(int [] arr,int start,int end){
        if(start>=end){
            return;
        }
        int index = getIndex(arr,start,end);
        quickSort(arr,start,index-1);
        quickSort(arr,index+1,end);
    }
    public static int getIndex(int [] arr,int start,int end){
        int pro = arr[start];
        int left = start;
        int right = end;
        while (left!=right){
            while (leftpro){
                right--;
            }
            while (leftarr[j+1]){
                    int tem = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tem;
                }
            }
        }
    }
    public static void sort(int [] arr){
        for(int i = arr.length/2-1;i>=0;i--){
            createNode(arr,i,arr.length);
        }
        for(int j =arr.length-1;j>0;j--){
            int tmp = arr[0];
            arr[0]  = arr[j];
            arr[j]  = tmp;
            createNode(arr,0,j);
        }
    }
    public static void createNode(int [] arr,int i,int length){
        int mid = arr[i];
        for(int k = i*2+1;k=right){
            return;
        }
        int mid = (left+right)/2;
        sort(a,left,mid);
        sort(a,mid+1,right);
        merge(a,left,mid,right);
    }

    public static void merge(int [] a,int left,int mid,int right){
        int [] tem = new int[a.length];
        int r1 = mid + 1;
        int tIndex = left;
        int cIndex = left;
        while(left<=mid&&r1<=right){
            if(a[left]<=a[r1]){
                tem[tIndex++] = a[left++];
            }else{
                tem[tIndex++] = a[r1++];
            }
        }
        while(left<=mid){
            tem[tIndex++] = a[left++];
        }
        while(r1<=right){
            tem[tIndex++] = a[r1++];
        }
        while(cIndex<=right){
            a[cIndex] = tem[cIndex];
            cIndex++;
        }
    }
    public static void mergeSort(int [] a){
        sort(a,0,a.length-1);

    }

 

你可能感兴趣的:(java实现冒泡,快排,堆排,归并。)