Preview article: Sorting Algorithm (internal sorting) Summary

Source: Internet
Author: User

Sorting is a very important operation in computer applications. We usually hear some algorithms, but we always write code as we seem to understand. Today I will summarize the common sorting algorithms.

This summary only involves internal sorting (the so-called Internal sorting refers to sorting in memory)

First, let's talk about the concept of stable and non-stable sorting.

If the same elements in a sequence remain in the original order after sorting is completed, the sequence becomes stable, and vice versa.

Insert sort

(1) Insert the sort directly (straiht insertion sort)

Algorithm Description: if there is a sorted sequence {R (20), R (35), R (88)}, when you want to insert an R (66, it needs to be compared with each element, R (35) <R (66) <R (88), so it should be inserted in R (35) and R (88) directly.

When the algorithm starts, an element is taken as the original sequence, and then the above method is repeated to insert each element into the sequence.

Void insertsort (sllist & L)
{
For (INT I = 2; I <= L. lenght; I ++)
{
If (LT (L [I], L [I-1]) // lt function to determine the size of two elements
{
L [0] = L [I];
              L[i] = L[i-1];
for(int j = i-2;LT(L[0],L[j]);j--)
{
L[j+1] = L[j];
}
L[j+1] = L[0];

}



}
Copy code

The time complexity of this algorithm is O (n2)

                                        Quick sorting

Quick sorting is an exchange-based sorting method. The most common options are bubblesort and quicksort)

Next we will talk about the Bubble Sorting:

The basic idea of Bubble Sorting is to sink the largest element into the bottom of a sorting, then narrow down the scope and continue.

Specifically, take the first element and compare it with the second element. If it is larger than the second element, it is exchanged. Otherwise, it is not exchanged, then compare the second element with the third element. Similarly, use the preceding method to swap the largest element until it is swapped to the bottom. This is the first time the sorting ends, and then, narrow down the scope, starting from the second element, where the preceding sorting method is used. The sorting ends until the range is reduced to an element.

The following is a Bubble sorting algorithm described in C ++:

    int a[5] = {1,3,5,4,2};
for(int i=4;i>=0;i--)
for(int j = 0;j<=i;j++)
{
if(a[j]>a[j+1])
{
int temp;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
for(int s = 0;s<5;s++)
cout<<a[s];
Copy code

Since I recently studied assembly, I wrote a code segment on emu8086 to Implement Bubble Sorting:

 

; Assemble the Bubble Sorting Algorithm
N equ 5
MoV CX, N-1
J03: Push CX
Lea BX,
J02: mov Al, [BX]
CMP Al, [bx + 1]
JNB j01
Xchg Al, [bx + 1]
MoV [BX], Al
J01: Inc BX
Loop j02
Pop CX
Loop j03
A DB 1, 2, 3, 4,
Copy code

Select sort

The basic idea of selection sort is that each row is sorted in n-I + 1 (I = 1, 2, 3 ...., n-1.

(1) sample selection sort)

Void selectsort (sqllist & L)
{
For (INT I = 0; I <L. length; I ++)
{
J = selectmin * (L, I); // This function returns the minimum record from I to the end.
// Location
If (I! = J) exchage (L [I], L [J]);

}

}
Copy code

(2) tree selection sort, also known as tournament sort, is a kind of strategy-based comparison, then, compare the remaining smaller n/2 elements in pairs. If this is repeated, the minimum record is selected.

(3) Heap sorting (Heap Sort)

First, we will introduce the large and small top heaps:

In a binary tree, if all the parent nodes are larger than the son node, the tree is called a big top heap. Otherwise, it is a small top heap.

The idea of heap sorting is to build an unordered sequence into a large top heap (or a small top heap), then retrieve the top element of the heap and adjust the heap again, make it into a big top push (or a small top heap) Here, so that all elements are taken out and sorted.

Merge Sorting

Merging sort)

The so-called merge, simply put, is to combine two ordered sequences into a new ordered table. We can use this idea to think of the sequence of n elements as a sub-sequence with a length of 1, then sort by merging and merge by two or two, then we merge n/2 sub-sequences with a length of 2 and repeat the above steps until they are merged into a sequence.

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.