Selection sort, insertion sort

Sequence

  • Overall around to solve the problem follows a confusing array of integers sorted
  • Sort results in ascending order
  • Unsorted integer array a []
i 0 1 2 3 4 5
a[i] 2 3 1 5 4 0

Selection Sort

principle

  • Each time the unsorted smallest number of digital switching portion and its first position, number and position of its first portion into sorted, and so on until all the sorted array containing part numbers
  • As part of the ordering process has been highlighted
  • Start out unsorted part
i 0 1 2 3 4 5
a[i] 2 3 1 5 4 0
  • Unsorted portion of the lowest number (0) and the number of its first position (2) exchange, and its first position into digital sorted portion
i ==0== 1 2 3 4 5
a[i] ==0== 3 1 5 4 2
  • The smallest part of the digital numbers (1) and its first position (3) exchange unsorted and its digital first position into a partially sorted
i ==0== ==1== 2 3 4 5
a[i] ==0== ==1== 3 5 4 2
  • The smallest part of the digital numbers (2) and its first position (3) exchange unsorted and its digital first position into a partially sorted
i ==0== ==1== ==2== 3 4 5
a[i] ==0== ==1== ==2== 5 4 3
  • The smallest part of the digital numbers (3) and its first position (5) exchange unsorted and its digital first position into a partially sorted
i ==0== ==1== ==2== ==3== 4 5
a[i] ==0== ==1== ==2== ==3== 4 5
  • The smallest part of the digital numbers (4) and its first position (4) exchange unsorted and its digital first position into a partially sorted
i ==0== ==1== ==2== ==3== ==4== 5
a[i] ==0== ==1== ==2== ==3== ==4== 5
  • The smallest part of the digital numbers (5) and its first position (5) exchange unsorted and its digital first position into a partially sorted
i ==0== ==1== ==2== ==3== ==4== ==5==
a[i] ==0== ==1== ==2== ==3== ==4== ==5==

Code by java

public class Sort {
    static public void selectSort(int[] a){//传入一个数组
       for(int i=0;i<a.length;i++){
           int min=i,temp;//min是未排序部分最小值的下标,初始是a[i]
           for(int j=i+1;j<a.length;j++)//从未排序部分开始找最小值的下标
               if(a[j]<a[min])
                   min=j;
           temp=a[i];//将未排序部分中最小值放到未排序部分的第一个位置,转换为排序部分
           a[i]=a[min];
           a[min]=temp;
       }
    }
}

Insertion Sort

principle

  • Each time the unsorted portion of the first number included in the ranking section, as ordered partially ordered, compared in turn from the highest position to the lowest position, until you find a suitable location
  • As part of the ordering process has been highlighted
  • Start out unsorted part
i 0 1 2 3 4 5
a[i] 2 3 1 5 4 0
  • Part unsorted incorporate a digital ordering parts. This step is usually not the best start operation
i ==0== 1 2 3 4 5
a[i] ==2== 3 1 5 4 0
  • Unsorted portion of the first number (3) into the sorting portion.
i ==0== ==1== 2 3 4 5
a[i] ==2== ==3== 1 5 4 0
  • Part digit unsorted (1) into the sorting portion.
i ==0== ==1== ==2== 3 4 5
a[i] ==1== ==2== ==3== 5 4 0
  • A digital part (5) incorporated unsorted sorting portion.
i ==0== ==1== ==2== ==3== 4 5
a[i] ==1== ==2== ==3== ==5== 4 0
  • A digital part (5) incorporated unsorted sorting portion.
i ==0== ==1== ==2== ==3== ==4== 5
a[i] ==1== ==2== ==3== ==4== ==5== 0
  • A digital part (5) incorporated unsorted sorting portion.
i ==0== ==1== ==2== ==3== ==4== ==5==
a[i] ==0== ==1== ==2== ==3== ==4== ==5==

Code by java

public class Sort {
    static public void insertSort(int[] a){
        for(int i=1;i<a.length;i++){//由于未排序第一个数字不用插入,所以直接从下标为1的位置开始
            int temp=a[i];//拷贝未排序部分的第一个数字
            int j;//记录最后未排序部分第一个数字的落点
            for(j=i-1;j>-1;j--){//由于插入第i个,那么已排序部分最高位下标就是i-1,从大到小比较
                if(a[j]>temp)//如果这个数比未排序部分第一个数字大,那么这个数字后移一位,第一个数字已经拷贝,所以不用担心覆盖(这是一种优化)
                    a[j+1]=a[j];//后移
                else break;//不然位置合适就跳出循环
            }
            a[j+1]=temp;//将未排序部分第一个数字放到合适的位置
        }
    }
}

Hill sorting (shell sort)

principle

  • The entire first row to be recorded is divided into several sub-sequence sequences direct insertion sort, the entire sequence to be 'substantially ordered', then all the records of a direct insertion sort
  • Constitution is not a simple piece-wise division sequence, but a record will be separated by increments to form a sub-sequence. Increments choice is important.
  • Different Hill sorting and insertion sort is, when delta is greater than 1, between the sub-sequence ordered jump, once inserted in the final sort, since the entire sequence has been 'substantially ordered', in this case more insertion sort fast, which is why Hill sorting better than insertion sort

Examples

  • 以下示例增量为1,3,5
  • 插入第一个数字无需调整,仔细观察每趟排序,从第增量个数字开始,每个数字都需要插入操作,当然是在同一增量序列中
  • 优先进行增量大的序列

希尔排序

Code by java

  • 这里取增量为1,4,13...(3*n+1)
static public void shellSort(int[] a){
        int h=1;
        while (h<a.length/3)
            h=h*3+1;//求出最大增量
        while (h>0){//增量大于零
           for(int i=h;i<a.length;i+=h){//每次将以增量为下标位置开始的数字往前插入到数组中,插入必须在同一增量序列中
                int temp=a[i];
                int j;
                for(j=i-h;j>-1;j-=h){
                    if(a[j]>temp)
                        a[j+h]=a[j];
                    else break;
                }
                a[j+h]=temp;
           }
           h/=3;//增量改变为较小的增量
        }
    }

Guess you like

Origin www.cnblogs.com/redo19990701/p/11282494.html