zoukankan      html  css  js  c++  java
  • 快排和归并排

    1.快排:

    Quicksort is a divide and conquer algorithm. It first divides a large list into two smaller sub-lists and then recursively sort the two sub-lists. If we want to sort an array without any extra space, quicksort is a good option. On average, time complexity is O(n log(n)).

    The basic step of sorting an array are as follows:

    • Select a pivot, normally the middle one
    • From both ends, swap elements and make left elements < pivot and all right > pivot
    • Recursively sort left part and right part
    import java.util.*;
    public class Main {
    public static void main(String[] args) {
    int[] arr={23,45,12,6,9,43,57,98,86,24};
    quickSort(arr);
    for(int i:arr){
    System.out.print(i+" ");
    }
    }

    private static void quickSort(int[] arr) {
    int low=0;
    int high=arr.length-1;
    sort(low,high,arr);
    }

    private static void sort(int low, int high, int[] arr) {
    if(arr == null || arr.length==0){ //consider empty array
    return;
    }
    if(low>=high){ //consider just one element in the array
    return;
    }
    int i=low;
    int j=high;
    int pivot=arr[(low+high)/2]; //make the middle position as pivot
    while(i<=j){ //left elements < pivot and all right > pivot
    while(arr[i]<pivot){
    i++;
    }
    while(arr[j]>pivot){
    j--;
    }
    if(i<=j){
    int temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    i++;
    j--;
    }
    }
    if(low<j){ //recursively invoke sort()
    sort(low,j,arr);
    }
    if(high>i){
    sort(i,high,arr);
    }
    }
    }

    2.mergeSort: Merge sort is a divide and conquer algorithm.

    Steps to implement Merge Sort:
    1) Divide the unsorted array into n partitions, each partition contains 1 element. Here the one element is considered as sorted.
    2) Repeatedly merge(归并) partitioned units to produce new sublists until there is only 1 sublist remaining. This will be the sorted list at the end. 

    Merge sort is a fast, stable sorting routine with guaranteed O(n*log(n)) efficiency. When sorting arrays, merge sort requires additional scratch space proportional to the size of the input array. Merge sort is relatively simple to code and offers performance typically only slightly below that of quicksort. 

    import java.util.*;

    public class Main {
    public static void main(String[] args) {
    int[] data = new int[] { 5, 3, 6, 2, 1, 9, 4, 8, 7 };
    mergeSort(data);
    for (int i = 0; i < data.length; i++) {
    System.out.print(data[i] + " ");
    }
    }

    public static void mergeSort(int[] data) {
    sort(data, 0, data.length - 1);
    }

    public static void sort(int[] data, int left, int right) {
    if (left >= right) //if divided to only one element
    return;
    int middle = (left + right) / 2;
    sort(data, left, middle); //first divided then merge
    sort(data, middle + 1, right);
    merge(data, left, middle, right);
    }

    public static void merge(int[] data, int left, int middle, int right) {
    int[] temp = new int[data.length];
    int i=left;
    int j = middle + 1;
    int k = left;
    int tmp = left;
    while (i <= middle && j <= right) {
    if (data[i] <= data[j]) {
    temp[k++] = data[i++];
    } else {
    temp[k++] = data[j++];
    }
    }
    while (j <= right) {
    temp[k++] = data[j++];
    }
    while (i <= middle) {
    temp[k++] = data[i++];
    }
    while (tmp <= right) { //finally,should copy temp array to data
    data[tmp] = temp[tmp++];
    }
    }


    }
  • 相关阅读:
    解决App can’t be opened because it is from an unidentified developer
    Mac之当前目录打开终端
    Mac之安装zsh
    毕业论文之降低重复率
    Latex之希腊字母表 花体字母 实数集
    latex之插入数学公式
    好句收集
    Python之两个列表一起打乱
    Python之时间统计
    Python之正则表达式
  • 原文地址:https://www.cnblogs.com/james111/p/6953167.html
Copyright © 2011-2022 走看看