数据结构(五):排序算法

//归并排序
public class MergeSort {

      private static void mergeSort(int [] a,int [] tempArr,int left,int right){
          if (left<right) {
              //递归分治
              int center = (left + right) / 2;
              mergeSort(a, tempArr, left, center);
              mergeSort(a, tempArr, center + 1, right);
              merge(a, tempArr, left, center + 1, right);
          }

      }
      
      private static void mergeSort(int [] a){
          int[] tempArr = new int[a.length];
          mergeSort(a,tempArr,0,a.length-1);
      }

      private static void merge(int [] a,int [] tempArr,int leftPos,int rightPos,int rightEnd){
              int leftEnd = rightPos-1;
              int tempPos = leftPos;
              int numElements = rightEnd-leftPos+1;
              while (leftPos<=leftEnd&&rightPos<=rightEnd){
                  if (a[leftPos]<=a[rightPos]){
                      tempArr[tempPos++] = a[leftPos++];
                  }else {
                      tempArr[tempPos++] = a[rightPos++];
                  }
              }
              while (leftPos<=leftEnd){
                  tempArr[tempPos++] = a[leftPos++];
              }
              while (rightPos<=rightEnd){
                  tempArr[tempPos++] = a[rightPos++];
              }
          for (int i = 0; i < numElements; i++,rightEnd--) {
              a[rightEnd] = tempArr[rightEnd];
          }
      }

    public static void main(String[] args) {
        int arr[] = {15,75,25,55,43,88,33,14,66};
        mergeSort(arr);
        Arrays.stream(arr).forEach(System.out::println);
    }
}
/*插入排序*/
public static void insertSort(int[] arr){
    int tmp = 0;
    for (int i = 0; i < arr.length; i++) {
        for (int j = i; j >=0; j--) {
            if (arr[i]<arr[j]){
               tmp = arr[i];
               arr[i] = arr[j];
               arr[j] = tmp;
            }
        }
    }
}

/*希尔排序*/
public  static  void shellSort(int[] a){
    int j;
    for (int gap = a.length/2; gap > 0; gap/=2) {
        for (int i = gap; i < a.length; i++) {
            int tmp = a[i];
            for (j = i; j>=gap && tmp<a[j-gap] ; j-=gap) {
                a[j] = a[j-gap];
            }
            a[j] = tmp;
        }
    }
}

public static void percolateDown(int[] a,int i,int n){
    int child;
    int tmp;
    for (tmp = a[i];2*i<n; i=child){
        child = 2*i;
        if (child != n-1 && a[child]<a[child+1])
            child++;
        if (tmp<a[child])
            a[i]=a[child];
        else
            break;
    }
    a[i]=tmp;
}
private static void swap(int[] a,int index1,int index2){
    int tmp = a[index1];
    a[index1] = a[index2];
    a[index2] = tmp;
}

/*堆排序*/
public static void helpSort(int[] a){
    for (int i= a.length/2;i>=0;i--)
        percolateDown(a,i,a.length);
    for (int i = a.length-1; i > 0 ; i--) {
        swap(a,0,i);
        percolateDown(a,0,i);
    }
}
public static void main(String[] args) {
    int [] arr={9,7,8,23,12,15,89,29};
    helpSort(arr);

    Arrays.stream(arr).forEach(System.out::println);
}

暂时写了三种排序
插入排序平均时间复杂度为O(n2)
希尔排序平均时间复杂度O(nlogn)
堆排序平均时间复杂度O(nlogn)

/*选择排序*/
public class SelectSort {
    public static void main(String[] args) {
        int arr[] = {15,75,25,55,43,88,33};
        for (int i = 0; i < arr.length; i++) {
            int minNum = arr[i];
            int minIndex = i;
            for (int j = i+1; j < arr.length; j++) {

                if (arr[j]<minNum){
                    minNum = arr[j];
                    minIndex = j;
                }

            }
             arr[minIndex] = arr[i];
             arr[i] = minNum;
        }
        Arrays.stream(arr).forEach(System.out::println);
        List<Integer> list = Arrays.asList(1,2,3);
        list.forEach(System.out::println);
    }
}
/*冒泡排序*/
public class BubbleSort {
    public static void main(String[] args) {
       // int arr[] = {3,5,6,8,11,27};
        int arr[] = new int[80000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*8000000);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        String format1 = format.format(new Date());
        System.out.println(format1);
        boolean flag = false;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 1; j < arr.length-i; j++) {
                if (arr[j-1] > arr[j]) {
                   int temp = arr[j];
                   arr[j] = arr[j-1];
                   arr[j-1] = temp;
                   flag =true;
                }
            }
            if (flag){
                flag = false;
            }else {
                break;

            }
        }
        String format2 = format.format(new Date());
        System.out.println(format2);
    }
}
//快速排序
public class QuickSort {

    public static void main(String[] args) {

//        int arr[] = {49, 38, 65, 97, 23, 22, 76, 1, 5, 8, 2, 0, -1, 22};
        int arr[] = new int[8000000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*8000000);
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        String format1 = format.format(new Date());
        System.out.println(format1);
        quickSort(arr,0,arr.length-1);
        String format2 = format.format(new Date());
        System.out.println(format2);
    }
    public static int getIndex(int[] arr,int low,int high){
         //基准数据
        int temp = arr[low];
        while (low<high){
          while (low<high&&arr[high]>=temp){
                high--;
          }
          arr[low] = arr[high];
          while (low<high&&arr[low]<=temp){
              low++;
          }
            arr[high] = arr[low];
        }
        arr[low] = temp;
        return low;
    }

    public static void quickSort(int a [],int low,int high){
        if (low<high) {
            int index = getIndex(a, low, high);
            quickSort(a, low, index - 1);
            quickSort(a, index + 1, high);
        }

    }

}

output:
20200528 09:17:45
20200528 09:17:47

快速排序是真的快 800w数据只需要2秒 像冒泡和插入排序8w数据都要10几秒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值