8 Sorts of data structures

Source: Internet
Author: User
Tags sorts

Bubble sort

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

Base sort

1) The pseudo-arranged data sequence is as follows:

73 22 93 43 55 14 28 65 39 81

First, based on the numeric value of single digits, they are assigned to buckets numbered 0 to 9 (single digit versus bucket number one by one) when traversing the data.

The result of the allocation (logical imagination) is as follows:

After the assignment ends. Next, the data in all the buckets is collected in order from small to large (from top to bottom in the bucket), and the following data sequences are still unordered:

81 22 73 93 43 14 55 65 28 39

Next, assign again, this time based on the 10-bit value (IBID.), the allocation result (logical imagination) as shown:

After the assignment ends. Next, the data in all buckets (the same principle as above) is then re-collected serially, resulting in the following data series:

14 22 28 39 43 55 65 73 81 93

Hill sort

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 1B

2 a 2B

3 A 3B

4 a 4 b

5 a 5B

1A,1B,2A,2B are grouped, the numbers are the same in the same group, the uppercase letters represent the first elements of the group, and each time the data for the same group is inserted 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

8 Sorts of data structures

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.