Sorting Summary (on)

Sort: The element according to ascending or descending order (Comparable to any object that implements the interface Comparable any further definition of the object class compareTo method, can be sorted)
sequencing is a linear operation table.
Data sequence: a set of data elements to be sorted.
The sort of benchmarks is keyword.
Sort Sort divided into inner and outer Sort:
the sort: all the elements to be sorted data is stored in memory

External sorting: ordering the data elements to be very large, not in memory, the memory needs to be introduced into the batch data stored on the external storage medium to store all batches sort.
Stability: it is assumed that a sequence of records to be sorted, a plurality of records having the same key, if sorted, the relative order of these records remain unchanged, i.e. in the original sequence, r [i] = r [ j] prior to, and r [i] before r [j], and in the sorted sequence, r [i] is still r [j], the sort is called stable.
Performance Evaluation of sorting algorithms:
an important measure of performance is the sorting algorithm sorting algorithm time complexity and space complexity.
Sorting time complexity of the algorithm is determined by comparing the number of elements and the movement of the algorithm execution times.
This storage array uses this summary data sequence, the data elements to be sorted contains keywords only.

  1. Direct insertion sort:
    the records to be sorted according to their size by one key value is inserted into an already ordered sequence of rows of sorted until all the records until the completion of the insertion, to give a new ordered sequence.
    Algorithm Description:
    (1) a first i (1 <= i <= n) times, the data sequence {a0, a1, ..., a (i-1), a (i), ..., a (n-1)} , a subsequence thereof before i-th element composed of {a0, a1, ..., a (i-1)} are sorted, an element a (i) is inserted into the sequence {a0, a1, ..., a (i -1)} of the proper position, so that after insertion of sequences still ordered, a (i) is determined by comparing the position of the inserted key.
    (2) repeating the above steps, elements common n-n-1 require scanning times, one per trip element before it is inserted into the subsequence.
    Illustrated as follows:
    Here Insert Picture Description

Space complexity: O (1);
time complexity: O (n ^ 2)
if the data sequence has been ordered, the time complexity is O (n)
the ordered sequence of data, the faster sorting.
Code:

public static void insertSort(int[] array) {
    int temp = 0;
    int j = 0;
    for (int i = 0; i < array.length; i++) {
        temp = array[i];
        for (j = i - 1; j > 0; j--) {
            if (array[j] > temp) {
                array[j + 1] = array[j];
            } else {
                break;
            }
        }
        array[j + 1] = temp;
    }
}

  1. Hill sorting :( said reduced incremental method)
    (1) The data sequence into a plurality of groups, each consisting of several elements spaced from the composition, this distance called incremental, direct insertion sort within a group sorting algorithm
    (2) increments the initial value is generally half the length of the data sequence after each trip increments gradually reduced, and finally the value of 1. with the increment is gradually decreased, but also reduce the number of groups within a set of elements the number increases, the number of columns is close to the entire order. When the increment is 1, only one set, the entire sequence of elements, then the trip can direct insertion sort.
    Illustration:
    Here Insert Picture Description
    Hill sorting characteristics:
    Shell sort is inserted directly into the optimized sequence; as GAP (number of groups or sub-interval)> 1 are pre-sorted, the purpose is to allow closer ordered array. When the gap == 1, the array is already close to the orderly, so it will soon.
    Space Hill sorting algorithm complexity is O (1)
    time complexity of O (n- 1.3-n- 2)
    Hill sorting unstable.

code show as below:

public static void shell(int[] array,int gap ){
    int temp = 0;
    int j = 0;
    for (int i = gap; i < array.length; i++) {
        temp = array[i];
        for (j = i - gap; j > 0; j-=gap) {
            if (array[j] > temp) {
                array[j + gap] = array[j];
            } else {
                break;
            }
        }
        array[j + gap] = temp;
    }
}
public static void shellSort(int[] array) {
long start = System.currentTimeMillis();
int[] d={5,3,1};     //分的组数
    for(int i=0;i<d.length;i++) {
        shell(array, d[i]);
    }
        long end = System.currentTimeMillis();
        System.out.println(end-start);

    }

  1. Bubble sort:
    comparing two key values of adjacent elements, if in reverse order, the exchange. If the data series in ascending order, each pass to be scanned in the end position of the largest element exchange, the water as bubbles emerge from the same.
    Graphic method:
    Here Insert Picture Description
    the spatial complexity of bubble sort is O (1), requires only a space for the exchange of two auxiliary elements.
    Time complexity of O (n ^ 2), if the initial sequence of data has been ordered, only single pass, time complexity is O (n).
    Bubble sort algorithm is stable.
    code show as below:
public static void booleanSort(int[] array){
 boolean exchange=true;   //是否交换标记
    for(int i=1;i<array.length&&exchange;i++) {  //有交换时再进行下一趟
    exchange=false;   //假定元素未交换
        for(int j=0;j<array.length-i;j++)   //一趟比较,交换
            if(array[j]>array[j+1]){    //反序时,交换
                int temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
                exchange=true;
            }

        }
    }

  1. Quick Sort:
    Take any element of an element to be sorted sequence as a reference value, in accordance with the sort key will be split into two sub-sequences ordered set. Each trip starts data alternately from both ends of the sequence will be smaller than the reference value of the switching element to the front end of the sequence, the reference value is greater than the switching element to the rear end of the sequence, the position between the two end position becomes the reference value . Using the same method for the two sub-sequences are sorted.

First, select the first value as the reference value 49, a first element position vacated; set i, j are the two ends of element index data sequences before and after the position of the element j comparison with the reference value, if small, the sequence moves to subscript distal position i is empty, add 1 i, j in this case the position vacated; i and then comparing the position value with the reference element, if large, the sequence moves to the rear end of the empty position j, j minus 1; repeat until i and j are equal.
Here Insert Picture Description
(1) Recursive method:
right and left sides of sort quickly sequence a recursive:
Code:
divided into two sub-sequences

public static int partion(int[] array,int low,int high) {
    int tmp = array[low];
    while (low < high) {
        while ((low < high) && array[high] >= tmp) {
            high--;

        }
        if (low >= high) {
            break;
        } else {
            array[low] = array[high];  //相遇
        }

    while ((low < high) && array[low] <= tmp) {
        low++;
    }
    if (low >= high) {
        break;
    } else {
        array[high] = array[low];
    }
}

        array[low]=tmp;
    return low;
}

public static  void Insert(int[] array,int start,int end){
    int temp=0;
    for(int i=start+1;i<=end;i++){
        temp=array[i];
        int j=0;

        for(j=i-1;j>=start;j--) {
            if (array[j] > temp) {
                array[j + 1] = array[j];
            } else {
                break;
            }
        }
        array[j+1]=temp;

    }
}

将其优化一下,规定选取前多少个调用直接插入排序。
对左右两个子序列进行递归
public static void quick(int[] array,int start,int end){
   if(end-start+1<=24){
       Insert(array,start,end);     //前24个用直接插入排序
       return;

       }
       int par=partion(array,start,end);
   if(par>start+1){  //保证左边是否有两个数据及以上
       quick(array,start,par-1);   //左边递归
   }
   if(par<end-1){
       quick(array,par+1,end);   //右边递归
       }
   }
进行快速排序
public static void quickSort(int[] arr){
    long start = System.currentTimeMillis();
    quick(arr,0,arr.length-1);

    long end = System.currentTimeMillis();
    System.out.println(end-start);

}

(2) taking three digital France:
Take the left, middle, right three numbers, and sorted, the number of the intermediate value as a hub, the hub value found by reference
guaranteed array [mid] <= array [ low] <= arry [ hiagh]
graphic method:
Here Insert Picture Description
according segmentation hub
bidirectional scanning, to find the number is greater than the value of the hub from the left, looking from the number is less than the value of the right side of the hub, and then exchange.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Code:

public static void swap(int[] array,int start,int end){
    int temp=array[start];
    array[start]=array[end];
    array[end]=temp;

}
public static void medianOfThree(int[] array,int low,int high){
    int mid=(low+high)>>>1;  //除二
    if(array[mid]>array[low]){
        swap(array,mid,low);
    }
    if(array[mid]>array[high]){
        swap(array,mid,high);
    }
     if(array[low]>array[high]){
        swap(array,low,high);
    }

}

(3) Non-recursive quicksort:
just use a stack to save the interval on it.
General recursive program into a non-recursive first thought is to use the stack, because the recursive process itself is a push
illustration:
Here Insert Picture Description
code implementation:

public static void quickSort1(int[] array) {
    int[] stack = new int[array.length * 2];
    int top = 0;

    int low = 0;
    int high = array.length - 1;
    //先进性一趟快排
    int par = partion(array, low, high);
    //1.判断当前par的左右两边是否有两个数据以上
    if (par > low + 1) {
        stack[top++] = low;
        stack[top++] = par - 1;
    }
    if (par < high - 1) {
        stack[top++] = par + 1;
        stack[top++] = high;
    }
    //两边的数对已经全部入栈
    //需要做的就是判断栈是否为空,不为空,取出两个数对
    while (top > 0) {    //表明栈不为空
        high = stack[--top];
        low = stack[--top];
         par = partion(array, low, high);
        //1.判断当前par的左右两边是否有两个数据以上
        if (par > low + 1) {
            stack[top++] = low;
            stack[top++] = par - 1;
        }
        if (par < high - 1) {
            stack[top++] = par + 1;
            stack[top++] = high;
        }
    }
}

Quicksort best case space complexity is: O (log (2) n ), in the worst case O (n), the average space complexity is O (log (2) n)
time complexity: the best case O (nlogn), the worst case O (n ^ 2), the average time complexity is O (nlogn).
quicksort algorithm is unstable.

Guess you like

Origin blog.csdn.net/weixin_42373873/article/details/90063852