Sort summary (under)

  1. Selection sort:
    each data element to be selected from the sorted minimum (or maximum) of an element stored in the starting position of the sequence, until all the data elements to be sorted drained. :
    The elements of the set array [i] -array [n- 1] key selected maximum (minimum) of the data element
    if it is not the last set of elements (first) element, it will be the set of elements the last (first) switching element
    in the rest of the array [i] -array [n- 2] (array [i + 1] -array [n-1]) set in the above step is repeated until a set of remainder a sorting element direct selection
    time complexity: O (^ 2 N)
    space complexity: O (1)
    stability: unstable
    code implementation:
public static void selectSort(int[] array){
        for(int i=0;i<array.length;i++ ){
            int j=0;
            for(j=i+1;j<array.length;j++){
                if(array[j]<array[i]){
                    int temp=array[i];
                    array[i]=array[j];
                    array[j]=temp;
                }
            }
        }
}

2. The heap sort
heap sort refers to a sorting algorithm using tree bulk (bulk) designed such a data structure, which is to select a sort. It is performed by selecting the data stack. Note that the row of piles to be built in ascending, descending row to build small pile.
Rootlets stack: an arbitrary key value of a node is equal to less than the key value of its child nodes, the root node that is the minimum value of
large root stack: the key value of any node is greater than a key value equal to its child node, i.e., root maximum value node
! [inserted here described image] ( https://img-blog.csdnimg.cn/20190511205835453.png?x-oss-pHere Insert Picture Description
Here Insert Picture Description
illustration:
Here Insert Picture Description
Here Insert Picture Description
After adjusting the exchange step, the value of the root node and the last value exchange
so: int temp = array [array.length- 1], ( because the last elements: array.length-1)
for the exchange of two elements: array [array.length-1-j ] = array [0 ];
the Array [0] = TEMP;
adjustment does not require readjustment when ordered data, so execution: adjust = (array, 0, array.length-1-j-1);

Summary: heap sort in two phases. First, a data sequence completed stack sequence, the root node is the maximum (minimum) value, and then select the sort of thinking employed, the value of the root node per trip cross. Change to the back, and then the remaining value adjusting piles successively repeated until the sorting is completed
code for:

       //把这颗树调整为大根堆的时间复杂度是:     log2(n)
public static void adjust(int[] array, int start, int end) {
    int temp = array[start];
    for (int i = 2 * start + 1; i <= end; i = 2 * i + 1) {
        //找到左右 孩子最大值的下标
        if ((i < end) && array[i] < array[i + 1]) {  //肯定有右孩子
            i++;   //左右孩子的最大值下标
        }
            if (array[i] > temp) {
                array[start] = array[i];
                start = i;

            }
            else if (array[i] < temp) {
                break;

            }
        }
        array[start] = temp;

    }
(2)交换
public static void heapSort(int[] array){
    long start=System.currentTimeMillis();

    for(int i=(array.length-1-1)/2;i>=0;i--){   //从最后一颗子树开始调整 //n/2
           adjust(array,i,array.length-1);  //log2(n)
    }
    //交换
    for(int j=0;j<array.length-1;j++){
        int temp=array[array.length-1-j];
        array[array.length-1-j]=array[0];
        array[0]=temp;
        adjust(array,0,array.length-1-j-1); //调整的时候,不需要调整有序的数据
    }
    long end = System.currentTimeMillis();
    System.out.println(end-start);

}

HEAPSORT: the sequence is adjusted to a stack of the time complexity is O (logn), so that the time complexity of O (nlogn)
space complexity is O (. 1)
3. merge sort:
which has been ordered sequence were combined to give the complete ordered sequence; i.e., each first ordered sequence, and then ordered to make inter-segment sequences. If two merged into a sorted list ordered list, called way merge.
Illustration:
Here Insert Picture Description
code for:

public static void merge(int[] array,int start,int mid,int end) {   //合并(某一个区间的合并)
        int[] tmpArray = new int[array.length];  //重新申请一个数组
        int tmpIndex = start;   //放到当前归并段开始的位置
        int s2 = mid + 1;
        int i=start;  //start一直在后移,定义i保存起来
        while (start <= mid && s2 <= end) {   //两个归并段都有数据时
            if (array[start] >=array[s2]) {
               tmpArray[tmpIndex]=array[s2];
               s2++;
               tmpIndex++;
            }
            else{
                tmpArray[tmpIndex]=array[start];
                start++;
                tmpIndex++;
            }

        }
        while(start<=mid){  //第二个归段没有数据,直接将第一个里剩下的拿下来
            tmpArray[tmpIndex++]=array[start++];

        }
        while(s2<=end){
            tmpArray[tmpIndex++]=array[s2++];
        }
        while(i<=end){
            array[i]=tmpArray[i];
            i++;
        }
    }
public static void mergeSort(int[] array,int start,int end){
        if(start>=end){   //相等则结束
            return;
        }
        int mid=(start+end)/2;
        mergeSort(array,start,mid);   //递归
        mergeSort(array,mid+1,end);  //分解
    //肯定是一个一个有序
    //合起来
    merge(array,start,mid,end);
}

Merge sort space complexity: O (n)
time complexity: O (nlogn), stabilized.

Comparison does not require sorting:

  1. Counting sequencing:
    thinking is similar to the hash table counting sequencing directly addressing method given set of sequences, to identify the maximum and minimum values in the sequence, to determine how much of the need to open the auxiliary space, each It has a unique index number in the corresponding auxiliary space.
    Find the maximum and minimum values in the sequence, the auxiliary open space Max-Min + 1,
    the minimum number corresponding to the position 0 of the subscript, a number of encounters give the value +1 corresponds to the subscript of the lower ,.
    Auxiliary space traversed again, you can obtain a set of ordered sequence of
    steps:
    (1) statistical occurrences of the same element
    (2) According to results of the recovery to the original sequence in the sequence
    (3) illustrates;
    Here Insert Picture Description
    stable
    code for:
private static int[] countSort(int[] array,int k)
{
    int[] C=new int[k+1];//构造C数组
    int length=array.length,sum=0;//获取A数组大小用于构造B数组
    int[] B=new int[length];//构造B数组
    for(int i=0;i<length;i++)
    {
        C[array[i]]+=1;// 统计A中各元素个数,存入C数组
    }
    for(int i=0;i<k+1;i++)//修改C数组
    {
        sum+=C[i];
        C[i]=sum;
    }
    for(int i=length-1;i>=0;i--)//遍历A数组,构造B数组
    {

        B[C[array[i]]-1]=array[i];//将A中该元素放到排序后数组B中指定的位置
        C[array[i]]--;//将C中该元素-1,方便存放下一个同样大小的元素
    }
    return B;//将排序好的数组返回,完成排序
}

2. Radix sort:
construct a two-dimensional array: 10 * n, a barrel length of each of the n number of elements each for storing an array ordering bit.
Operation cycle: from the lowest bit, all the elements of the digital bit to be stored to a corresponding bucket (two-dimensional array corresponding to the row), and then all the elements of small to large bucket according to remove the tub reference numerals put to the after extraction. Continue to the next operation.
Process:
Existing: 73 2,293,435,514,286,539 81
: (1) Sort by bits
Here Insert Picture Description
result after sorting:
8,122,739,343,145,565 28 39
(2) Sort by ten:
Here Insert Picture Description
Results after ordering :
1,422,283,943,556,573 81 93
code for:

private static void radixSort(int[] array,int d)
{
    int n=1;//代表位数对应的数...
    int k=0;//保存每一位排序后的结果用于下一位的排序输入
    int length=array.length;
    int[][] bucket=new int[10][length];//排序桶用于保存每次排序后的结果,这一位上排序结果相同的数字放在同一个桶里
    int[] order=new int[length];//用于保存每个桶里有多少个数字
    while(n<d)
    {
        for(int num:array) //将数组array里的每个数字放在相应的桶里
        {
            int digit=(num/n)%10;
            bucket[digit][order[digit]]=num;
            order[digit]++;
        }
        for(int i=0;i<length;i++)//将前一个循环生成的桶里的数据覆盖到原数组中用于保存这一位的排序结果
        {
            if(order[i]!=0)//这个桶里有数据,从上到下遍历这个桶并将数据保存到原数组中
            {
                for(int j=0;j<order[i];j++)
                {
                    array[k]=bucket[i][j];
                    k++;
                }
            }
            order[i]=0;//将桶里计数器置0,用于下一次位排序
        }
        n*=10;
        k=0;//将k置0,用于下一轮保存位排序结果
    }

}

Guess you like

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