Algorithm for direct insertion of sort and hill sort

Source: Internet
Author: User

1. Insert sort-Direct insert sort (straight insertion sort)

Basic idea:

Insert a record into the sorted ordered table to get a new, sequential table with a 1 increase in the number of records. That is, the 1th record of the sequence is considered an ordered subsequence, and then inserted from the 2nd record one after the other until the entire sequence is ordered.

Important: Set up Sentinel as a temporary storage and judgment array boundary.

Example of a direct insert sort:

If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.

The implementation of the algorithm:

  1. void print (int a[], int n,int i)
  2. {
  3. Cout<<i <<":";
  4. For (int j= 0; j<10; j + +)
  5. {
  6. COUT<<A[J] <<"";
  7. }
  8. cout<<endl;
  9. }
  10. void Insertsort (int a[], int n)
  11. {
  12. For (int i= 1; i<n; i++)
  13. {
  14. if (A[i] < a[i-1])
  15. { //If the first element is greater than the i-1 element, insert it directly. Less then, move the ordered table after inserting
  16. int j= i-1;
  17. int x = a[i]; //Copy as Sentinel, which stores the elements to be sorted
  18. A[i] = a[i-1]; //Move an element successively
  19. While (x < a[j])
  20. { //Find the insertion position in an ordered table
  21. A[J+1] = A[j];
  22. j--; //element move back
  23. }
  24. A[J+1] = x; //Insert to the correct position
  25. }
  26. Print (a,n,i); //Print the results of each trip sort
  27. }
  28. }
  29. int main ()
  30. {
  31. int a[10],i;
  32. for (i=0; i <; i++)
  33.     {
  34. A[i] = rand ()% 100;
  35.     }
  36. Insertsort (a,10);
  37. Print (a,10,10);
  38. }

Efficiency:

Time complexity: O (n^2).

The other insert sort has two-insert sort, 2-way insert sort.

2. Insert sort-Hill sort (Shell ' s sort)

The hill sort was proposed by D.l.shell in 1959, and the relative direct ranking has been greatly improved. Hill sort is also called narrowing the incremental sort

Basic idea:

First, the entire sequence of records to be sorted into a number of sub-sequences of the direct insertion of the order, the entire series of records in the "Basic order", then the whole record is inserted in order.

Operation Method:

    1. Select an incremental sequence T1,T2,...,TK, where ti>tj,tk=1;
    2. According to the number of increment series K, the sequence is sorted by K-trip;
    3. Each order, according to the corresponding increment ti, the backlog sequence is divided into several sub-sequences of length m, respectively, the sub-table is directly inserted sort. Only the increment factor is 1 o'clock, the entire sequence is treated as a table, and the length of the table is the length of the entire sequence.

Example of a hill sort:

Algorithm implementation:

We simply handle the increment sequence: Delta sequence d = {N/2, N/4, N/8 ..... 1} n is the number of digits to be sorted

That is: A set of records to be sorted by an increment D(the number of n/2,n to be sorted) into several sets of sub-sequences, each group of records of the subscript difference D. Direct Insert sorting of all elements in each group, followed by a smaller increment ( D/2) to group it, and then make a direct insert sort in each group. Continue to shrink the increment until it is 1, and finally use the direct insert sort to complete the sorting.

  1. void print (int a[], int n,int i)
  2. {
  3. Cout<<i <<":";
  4. For (int j= 0; j<10; j + +)
  5. {
  6. COUT<<A[J] <<"";
  7. }
  8. cout<<endl;
  9. }
  10. /* directly into the general form of the sort, int dk Shrinks the increment, if the direct insertion sort, dk=1 */
  11. void Shellinsertsort (int a[], int n, int dk)
  12. {
  13. For (int i= dk; i<n; ++i)
  14. {
  15. if (A[i] < A[I-DK])
  16. { //If the first element is greater than the i-1 element, insert it directly. Less then, move the ordered table after inserting
  17. int j = I-DK;
  18. int x = a[i]; //Copy as Sentinel, which stores the elements to be sorted
  19. A[i] = A[I-DK]; //First move back one element
  20. While (x < a[j])
  21. {//Find the insertion position in an ordered table
  22. A[J+DK] = A[j];
  23. J-= DK; //element move back
  24. }
  25. A[J+DK] = x; //Insert to the correct position
  26. }
  27. Print (A, n,i);
  28. }
  29. }
  30. /* First sort by increment D (N/2,n for the number of orders to sort */
  31. void Shellsort (int a[], int n)
  32. {
  33. int DK = N/2;
  34. While (DK >= 1)
  35. {
  36. Shellinsertsort (A, n, DK);
  37. DK = DK/2;
  38. }
  39. }
  40. int main ()
  41. {
  42. int a[10],i;
  43. for (i=0; i <; i++)
  44. {
  45. A[i] = rand ()% 100;
  46. }
  47. Shellsort (a,10); //Hill Insert sort
  48. Print (a,10,10);
  49. }
It is difficult for hill to sort the time-lapse analysis, the comparison number of key code and the number of record moves depend on the selection of increment factor sequence d, which can accurately estimate the comparison number of key codes and the number of movement of records in certain cases. At present, no one has given a method to select the best increment factor sequence. increment factor sequence can have a variety of ways, there are odd, but also take prime numbers, but need to note: The increment factor in addition to 1 there is no public factor, and the last increment factor must be 1. The hill sort method is an unstable sort method.

Algorithm for direct insertion of sort and 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.