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

  • Post author:
  • Post category: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 (left<right&&arr[right]>pro){
                right--;
            }
            while (left<right&&arr[left]<=pro){
                left++;
            }

            if(left<right){
                int tem = arr[left];
                arr[left] = arr[right];
                arr[right] = tem;
            }
        }
        int mid = arr[left];
        arr[left] = pro;
        arr[start] = mid;
        return left;
    }
    public static void maoPao(int [] arr){
        for(int i = 0;i<arr.length-1;i++){
            for(int j= 0;j<arr.length-1-i;j++){
                if(arr[j]>arr[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<length;k=k*2+1){
            if(k+1<length&&arr[k]<arr[k+1]){
                k++;
            }
            if(mid<arr[k]){
                arr[i]=arr[k];
                i=k;
            }else {
                break;
            }
        }
        arr[i] = mid;
    }

    //归并
    public static void sort(int [] a,int left,int right){
        if(left>=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);

    }



版权声明:本文为ZhangQiye1993原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。