Sorting algorithms (bubbling, selecting, inserting, fast, hill, heap sort, cardinality, count sort)

Source: Internet
Author: User

Time complexity O (n^2) sorting algorithm: Bubble sort, select Sort, insert sort

Bubble sort in array a[n]

Bubble Sort:

If the array is [3,7,4,6,8,9,1]-->[3,7,4,6,8,9,1]-->[3,4,7,6,8,9,1] .... Go on, and compare the next 2 numbers.

1. First round: array of adjacent 2 elements compared (a[0] and a[1] than, a[1] and a[2] than, a[2] and a[3] than, a[3] and a[4] than ... ), the larger number is placed backward, and is compared to the last number of the array.

The second round continues the array neighbor comparison, at which point the second element is compared to the reciprocal of the array.

... Until the last round is only compared with a[0] and a[1] than the end.

In the entire array, a[0] compared to a[1], the larger number is placed in a[1], and then a[1] and a[2] For comparison, equal or larger number put a[2], and so on has been compared to the last one of the array, the last element of the array when the comparison is completed is the maximum number of the entire array. After determining a maximum number, the bubbling occurs, but at this point the range is within the range of the entire array length-1, because after the first round we have determined that the last one is the largest, followed by the second largest number in the entire array, which will be placed in the second-to-last position of the entire array. In turn, the bubble is in the first bit of the array, at which point the array is sorted.

Select Sort:

If the array is [3,7,4,6,8,9,1]--->[1,7,4,6,8,9,3]-->[1,3,4,6,8,9,7]---;

Find the smallest number in the array and put it on the first bit. Next, look for the smallest number in the array except for the first digit, and put it on the second position. And so on

1. Take the first number in the array into the TEMP variable, tmp, and then iterate through the array, encountering a number smaller than the TMP value, the number assigned to the number of tmp,temp is the smallest of the array. and swaps the position of the minimum number and the value of the first position of the array.

2. Remove the second number to do the same treatment as the first step. The second small number in the entire array is placed in the second bit of the array.

3. etc.

Insert Sort:

If the array is [3,7,4,6,8,9,1]-->[3,7,4,6,8,9,1]-->[3,4,7,6,8,9,1]-->[3,4,6,7,8,9,1] ....

1. Starting with the second number in the array (subscript 1), compare forward (because there is no other number in front of the first number, and the subscript is 0), if it is smaller than the first number, swap the position with the first number.

2. Starting with the third number in the array, forward comparison, if larger than the second number, you do not need to move the position because the second number is definitely larger than the first number, if it is smaller than the second number, you also need to compare the first number, if it is smaller than the first number is compared to the first number, if larger than the first number, then only the second number exchange position.

3. Compare the number of nth in the array, and if it is larger than n-1, continue to compare the first n-2. In the code implementation, if it is smaller than the previous number, swap positions with it, and continue to compare forward. Until compared to the first number.

There are also 3 algorithms: quick sort, heap sort, hill sort. Update later

There are also 2 algorithms: Cardinal Sort and count sort

  

Sorting algorithms (bubbling, selecting, inserting, fast, hill, heap sort, cardinality, count 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.