Sorting algorithm comparison, steps, improvement

Source: Internet
Author: User

Picture Town Building

Insert Sort (insertsort)

Steps:

1. Select a record to sort

2. compare with ordered sequential sequence, and insert

3. repeat the above steps each time for less and fewer elements until all elements are inserted.

Improved:

  Binary insertion sort, direct and intermediate comparison of ordered sequences.

  Hill sort .

Hill sort (also known as narrowing incremental sort, shellsort)

Steps :

1. first segment the entire sequence of elements to be ordered into several sub-sequences

2. Insert sort separately

3. then decrease the increment and then insert sort

4. when the elements in the entire sequence are basically ordered (the increment is small enough) , then the whole element is sorted once

Bubble sort (bubblesort)

Steps:

1. Compare adjacent elements. If the first one is bigger than the second one, swap them both.

2. Do the same work for each pair of adjacent elements, starting from the first pair to the end of the last pair. At this point, the last element should be the maximum number.

3. Repeat the above steps for all elements except the last one.

4. Repeat the above steps each time for fewer elements until there are no pairs of numbers to compare.

Improved:

  Quick Sort .

Quick Sort (QuickSort)

Steps:

1. Select an element from the sequence called "Datum", reorder the sequence, all elements are placed in front of the datum in a smaller position than the base value, and all elements are larger than the base value at the back of the Datum (the same number can be on either side).

2. Recursively sort sub-columns that are less than the base value element and that are larger than the base value element.

3. At the bottom of the recursive situation, the size of the sequence is 0 or one, which is always sorted.

Select Sort (selectsort)

Steps:

1. Find the smallest (large) element in the unordered sequence, and place it in the starting position of the sort sequence

2. continue to find the smallest (large) element from the remaining unordered elements

3. drop to the end of the sorted sequence

4. and so on until all elements are sorted.

Improved:

Traditional simple sorting, each loop can only determine the positioning of an element after sorting. We can consider improving the position of two elements (the current maximum and minimum records) for each cycle , thus reducing the number of cycles required for sorting. Improved to sort n data, up to [N/2] loop.

Heap sort.

Heap Sort (heapsort)

Steps:

1. build a heap without sequences, and select a large top heap or a small top heap according to the ascending descending order requirement;

2. Swap the top element of the heap with the end element and "sink" the largest element to the end of the array;

3. Readjust the structure so that it satisfies the heap definition, and then proceed to swap the top element of the heap with the current end element and perform the adjustment + Exchange step repeatedly until the entire sequence is ordered

Merge sort (mergesort)

Steps:

1. Divide the input sequence of length n into two sub-sequences with a length of N/2.

2. The two sub-sequences were sorted by merge.

3. Recursively combine two sorted sub-sequences into a final sort sequence.

Bucket sort (bucket sort)

Steps:

1. Create an equal-capacity bucket array and initialize the bucket array elements to 0

2. Iterate through the array and set the value of the array as the subscript for the bucket array. When the data is read, the value of the bucket is added 1.

3. Remove the key for the value of the bucket array that is not 0, and the number is the value of the key

Improved:

base Sort . Count Sort

Base sort (Radix sort)

Steps:

1. unify all the values to be compared to the same number of digits, and 0 for the shorter digits.

2. start with the lowest bit and order one at a time.

Counting sort (count sort)

Steps:

    1. Find the maximum and minimum values in the sequence, and open up the auxiliary space of max-min+1
    2. The minimum number corresponds to the position of the subscript 0, and a number is given to the corresponding subscript at the value of +1.
    3. Iterate through the secondary space to get an ordered set of sequences

Sorting algorithm comparison, steps, improvement

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.