"Algorithmic supplements (Java descriptive narrative)"---insert sort (direct insert sort, hill Sort)

Source: Internet
Author: User

Insert Sort basic idea

Each time a sorted record is inserted at its keyword size into the appropriate location of the previously sequenced sub-file until all records have been inserted.

Direct Insertion Sort Basic idea

The basic operation of a direct insert sort is to insert a record into an ordered, sorted table. Thus a new ordered table is obtained. That is, if the records to be sorted are stored in the array r[1 N], during sorting, R is divided into two sub-intervals r[1 I] and r[i+1 N], among them. R[1 I] is an ordered area that has been sequenced; R[i+1 N] is the part that is not currently sorted.

Inserts the first record of the current unordered zone r[i+1] into the ordered area r[1 I] to the proper position, so that the r[1 I+1] becomes a new ordered area, inserting one data at a time until all the data is in order.

Java programs
/************************** * 直接插入排序**************************/public class InsertSort {private void insertSort(int[] datas) {    if (datas == null || datas.length < 2)        return;    int i, j, insertData;    for (i = 1; i < datas.length; i++) {        insertData = datas[i];// 要插入的变量        for (j = i - 1; j >= 0 && insertData < datas[j]; j--)            datas[j + 1] = datas[j];        datas[j + 1] = insertData;// 将要插入的数据放置到正确的位置    }}public static void main(String[] args) {    int[] datas = new int[] { 6, 5, 3, 1, 8, 7, 2, 4 };    System.out.println("********排序前********");    for (int i = 0; i < datas.length; i++) {        System.out.print(datas[i] + ",");    }    InsertSort insertSort = new InsertSort();    insertSort.insertSort(datas);    System.out.println("\n********排序后********");    for (int i = 0; i < datas.length; i++) {        System.out.print(datas[i] + ",");    }}}
Performance analysis
    1. Complexity of Time

    1. The direct insert sort is an in-place sort and is a stable sort method.

Basic idea of Hill sort

First take an integer less than n D1 as the first increment, the entire record of the file is divided into D1 groups, all the records of the multiples of D1 are placed in the same group, the inserts are sorted within the groups; Take the second increment D2 < D1, repeating the above grouping and sorting until the obtained increment dt = 1 (dt < dt-1 <······< D2 < D1), that is, all records are placed in the same group for direct insert sorting.

Java programs
/************************** * Hill sort **************************/public class Shellsort {private void Shellsort (int[]    Datas) {if (datas = = NULL | | Datas.length < 2) return; int temp;//temporary variable int datalength;//step int pointer;//The position to be processed datalength = datas.length/2;//initialization step while (DAT            Alength! = 0) {for (int j = datalength; J < Datas.length; J + +) {temp = Datas[j];            pointer = j-datalength;                 while (pointer >= 0 && temp < Datas[pointer]) {datas[pointer + datalength] = Datas[pointer];            pointer = pointer-datalength;        } Datas[pointer + datalength] = temp;    } datalength = DATALENGTH/2;    }}public static void Main (string[] args) {int[] datas = new int[] {6, 5, 3, 1, 8, 7, 2, 4};    System.out.println ("******** ******** before sorting");    for (int i = 0; i < datas.length; i++) {System.out.print (Datas[i] + ","); } Shellsort Shellsort =New Shellsort ();    Shellsort.shellsort (datas);    System.out.println ("\n******** ******** after sorting");    for (int i = 0; i < datas.length; i++) {System.out.print (Datas[i] + ","); }}}
Performance analysis

The run time of the hill sort depends on the selection of the increment sequence (step). Good increment sequences have for example the following characteristics:

    1. The last increment must be 1
    2. You should try to avoid situations in which the values in the sequence, especially the neighboring values, are multiplied by each other.

The time performance of the Hill sort is due to the direct insertion sort, for example the following:

    • When the initial state of the file is basically ordered, the comparison and number of moves are required to insert the order directly.
    • In the beginning of the hill, the increment is larger, more groups, the number of records in each group is small, so the direct insertion in the group is faster, then the incremental di gradually reduced, the number of groups gradually decreased. The number of records in each group is increasing, but because the sequence of di-1 as a distance has been sorted, the file is closer to the ordered state. So the new trip sort process is also faster.

      Therefore, the hill sort is more efficient than the direct insertion sort.

Hill sort is an unstable sort of method

Reference data: Data structure and algorithm analysis--java language description narrative, "Big talk data Structure"

"Algorithmic supplements (Java descriptive narrative)"---insert sort (direct insert sort, hill Sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.