"Algorithm" 4th edition, Chapter 2nd ' sort ': primary sorting algorithm (selection, bubbling, inserting, Hill)

Source: Internet
Author: User

The author of the 4th edition of the algorithm is Robert Sedgewick and Kevin Wayne.

1. Select sort

Choosing a sort is the simplest way to sort. First, the smallest element in the array is found, and secondly, it is swapped with the first element of the array (if the first element is the smallest element, then it is swapped with itself), and again, the smallest element is found in the remaining element, and the position is swapped with the second element of the array. This is repeated until the entire array is sorted.

The book puts forward a proposition: for an array of length n, it takes about N2/2 and n times to select a sort.

The procedure is as follows:

1 voidSelectionsort::sort ()2 {3     //swap the smallest elements in a[i] and A[i+1..len]4      for(inti =0; i < Len; i++)5     {6         intMin =i;7          for(intj = i +1; J < Len; J + +)8         {9             if(Less (Arr[j], arr[min]))Ten             { OneMin =J; A             } -         } - Exchange (i, min); the     } -}
2. Bubble sort

From the first element, the adjacent element 22 is compared, according to the order from small to large or from large to small to exchange, so a trip past, the largest or smallest number is exchanged to the last one; then, from the beginning of the 22 comparison exchange, until the second-lowest end; Until all elements are in order. An example is as follows:

Original Array to sort | 6 | 2 | 4 | 1 | 5 | 9 |

  First trip sort (outer loop)

First 22 comparison 6 > 2 swap (inner loop)

Pre-swap Status | 6 | 2 | 4 | 1 | 5 | 9 |

Post-swap Status | 2 | 6 | 4 | 1 | 5 | 9 |

Second 22 comparison, 6 > 4 swap

Pre-swap Status | 2 | 6 | 4 | 1 | 5 | 9 |

Post-swap Status | 2 | 4 | 6 | 1 | 5 | 9 |

Third 22 comparison, 6 > 1 swap

Pre-swap Status | 2 | 4 | 6 | 1 | 5 | 9 |

Post-swap Status | 2 | 4 | 1 | 6 | 5 | 9 |

Fourth time 22 comparison, 6 > 5 swap

Pre-swap Status | 2 | 4 | 1 | 6 | 5 | 9 |

Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Fifth time 22 comparison, 6 < 9 no swap

Pre-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

  Second trip sort (outer loop)

First 22 comparison 2 < 4 no swap

Pre-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Post-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |

Second 22 comparison, 4 > 1 swap

Pre-swap Status | 2 | 4 | 1 | 5 | 6 | 9 |
Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Third 22 Comparisons, 4 < 5 non-exchangeable

Pre-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |
Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Fourth time 22 comparison, 5 < 6 no swap

Pre-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

  Third trip sort (outer loop)

First time 22 comparison 2 > 1 swap

Post-swap Status | 2 | 1 | 4 | 5 | 6 | 9 |

Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |

Second 22 comparison, 2 < 4 non-exchangeable

Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |
Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |

Third 22 Comparisons, 4 < 5 non-exchangeable

Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |
Post-swap Status | 1 | 2 | 4 | 5 | 6 | 9 |

Four-trip sort (outer loop) No swap

Five-trip sort (outer loop) No swap

Sort finished, output final result 1 2 4 5 6 9

1 voidBubblesort::sort ()2 {3     //one by one compare a[j] and a[j+1] and element Exchange when A[j]>a[j+1]4      for(inti = len-1; I >-1; i--)5     {6          for(intj =0; J < I; J + +)7         {8             if(!less (Arr[j], arr[j+1]))9             {TenExchange (J, J +1); One             } A         } -     } -}
3. Insert Sort

The insertion sort is where each step inserts a pending data in its size into the appropriate position in the sorted data until all is inserted. The insertion sort method is divided into two types: direct insert sort and binary insert sort, here only the direct insertion sort is introduced. An example such as:
  

The book proposes a proposition: for an array of randomly arranged N and a primary key that is not duplicated, an average insertion order requires approximately N2/4 comparisons and approximately N2/4 interchanges. In the worst case, approximately N2/2 comparisons and approximately N2/2 interchanges are required, preferably N-1 and 0 times.

The code is as follows:

1 voidInsertionsort::sort ()2 {3      for(inti =1; i < Len; i++)4     {5         //Insert a[i] into a[i-1], a[i-2], a[i-3] ... Among6          for(intj = i; J >0&& less (Arr[j], arr[j-1]); j--)7         {8Exchange (J, J-1);9         }Ten     } One}
4. Hill sort

The following description is excerpted from a blog post:

The essence of the hill sort is the grouping insert sort, which is also known as narrowing the incremental sort, because of the DL. The shell was named after it was introduced in 1959.

The basic idea of this method is that the whole sequence of elements is divided into several sub-sequences (consisting of elements separated by an "increment"), then the direct insertion sort is done separately, then the increment is reduced and then sorted, and then the whole element is sorted by a direct insertion when the elements in the entire sequence are basically ordered (the increment is small enough). Because the direct insert sort is very efficient when the elements are basically ordered (close to the best case), the hill sort is more efficient in time than the first two methods.

An array of n=10 49, 38, 65, 97, 26, 13, 27, 49, 55, 4 for example

First time gap = 10/2 = 5

49 38 65 97 26 13 27 49 55 4

1 A |      |     |      |    1 B |   |   | |

2 A |     |             |    2 B |     | |

3 A |                   |    3 B | |

4 A | 4 B |

5 a 5B

1 A, 1B, 2 A, 2B, and so on are grouped tags, the same number of numbers in the same group, the capital letter is the first element of the group, each time the same group of data directly into the sort. It is divided into five groups (49, 13) (38, 27) (65, 49) (97, 55) (26, 4), so that each group is sorted (13, 49) (27, 38) (49, 65) (55, 97) (4, 26), the same as below.

Second time gap = 5/2 = 2

After sorting

13 27 49 55 4 49 38 65 97 26

1 A |    1 B |    1C |    1D | 1E |

2 a 2B 2C 2D 2E

The third time gap = 2/2 = 1

4 26 13 27 38 49 49 55 97 65

1 a 1 b 1C 1D 1E 1F 1G 1H 1I 1J

Fourth time gap = 1/2 = 0 Sort Done to get the array:

4 13 26 27 38 49 49 55 65 97

The code is as follows:

1 voidShellsort::sort ()2 {3      for(intStep = Len/2; Step >0; Step/=2)4     {5          for(inti = step; i < Len; i++)6         {7             //Insert a[i] into a[i-step], A[i-2*step], A[i-3*step] ... Among8              for(intK = i; K >= Step && Less (Arr[k], arr[k-step]); K-=Step)9             {TenExchange (K, K-step); One             } A         } -     } -}

Please refer to GitHub for full code.

"Algorithm" 4th edition, Chapter 2nd ' sort ': primary sorting algorithm (selection, bubbling, inserting, Hill)

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.