Summary of basic sorting algorithms

Source: Internet
Author: User

I. Insert sorting

1. Sort ideas

Insert the record Ri to the sorted record table R1, R2 ,...., In the Ri-1, a new ordered table with 1 more records is obtained. Until all records are inserted. The complexity is O (n2 ).

The order of records to be sorted is stored in the array R [1... N], at a certain time point of sorting, the record sequence is divided into two parts:

◆ R [1... I-1]: ordered part;

◆ R [I... N]: unordered parts.

Obviously, at the beginning of sorting, R [1] is sorted.

For example, if a keyword sequence is set to 7, 4,-2, 19, 13, 6, the sorting process is directly inserted, as shown in:


 

The Code is as follows:

// Insert the sorting directly


[Cpp]

Void straight_insert_sort (int * L, intlength)
{
Inti, j, temp;
For (I = 1; I <= length; I ++)
{
Temp = L [I];
J = I-1;/* set the sentry */
While (temp <L [j])
{L [j + 1] = L [j];
J --;
}/* Find the Insert Location */
L [j + 1] = temp;/* insert to corresponding position */
}
}

Void straight_insert_sort (int * L, intlength)
{
Inti, j, temp;
For (I = 1; I <= length; I ++)
{
Temp = L [I];
J = I-1;/* set the sentry */
While (temp <L [j])
{L [j + 1] = L [j];
J --;
}/* Find the Insert Location */
L [j + 1] = temp;/* insert to corresponding position */
}
}

 

Ii. Select sorting

The simplest sorting algorithm is as follows: it selects the smallest element in the array and exchanges it with the first element in the array. Then find the small element and exchange it with the second element in the array until the sorting of the entire array ends. It is called "select sort" because it constantly selects the smallest element of the remaining element.

The implementation code is as follows:


[Cpp]
Oid selection (Item a [], int l, int r)
{
Int I, j;
For (I = l; I <r; I ++)
{
Intmin = I; // minimum element index
For (j = I + 1; j <= r; j ++)
{
If (less (a [j]), a [min])
{
Min = j; // update the index value of the smallest element
}
}
Exch (a [I], a [min]); // exchange
}

}

Void selection (Item a [], int l, int r)
{
Int I, j;
For (I = l; I <r; I ++)
{
Intmin = I; // minimum element index
For (j = I + 1; j <= r; j ++)
{
If (less (a [j]), a [min])
{
Min = j; // update the index value of the smallest element
}
}
Exch (a [I], a [min]); // exchange
}

}

 

The execution time of sorting is determined by the number of comparison operations. The approximate number of times used is

: The number of compare operations N * N/2, and the number of switch operations is N. The time consumption for sorting is not related to the sorting status of the current sequence.

Iii. Hill sorting

Insert an extension of sorting during Hill sorting, which improves execution efficiency by allowing non-Adjacent Elements to be exchanged. The essence of hill sorting: h-sorting is to generate a sorted file for every h element. H-sorted files are separated by h. For h sorting files with a large h value, you can move elements that are far away from each other. It is easier to sort h values by h hours until the h values of 1 are sorted, generate ordered files.


[Cpp]
Void hell (int * data, int left, int right)
{
Intlen = right-left + 1;
Intd = len;
While (d> 1)
{
D = (d + 1)/2;
For (int I = left; I <right + 1-d; I ++)
{
If (data [I + d] <data [I])
{
Inttmp = data [I + d];
Data [I + d] = data [I];
Data [I] = tmp;
}
}
}
}

Void hell (int * data, int left, int right)
{
Intlen = right-left + 1;
Intd = len;
While (d> 1)
{
D = (d + 1)/2;
For (int I = left; I <right + 1-d; I ++)
{
If (data [I + d] <data [I])
{
Inttmp = data [I + d];
Data [I + d] = data [I];
Data [I] = tmp;
}
}
}
}

 

Select a better step sequence for hill sorting.

Iv. Bubble Sorting

Bubble Sorting is simple: traversing the file. If the two adjacent elements are in incorrect order, they will be exchanged and repeat this operation until the entire sequence is sorted. For ~ The R-1 I value, the internal loop j through the right to the left traversal of the elements, the continuous elements of the comparison-exchange operation, to achieve a [I],... The smallest element in a [r] is enlarged in a [I. In all the comparison operations, the smallest elements must be moved to the frontend.


[Cpp]
Void maopao (Item a [], int l, int r)
{
Inti, j;
For (I = l; I <r; I ++)
{
For (j = r; j> I; j --)
{
If (less (a [j], a [J-1])
{
Itemtemp = a [j];
A [j] = a [J-1];
A [J-1] = temp;
}
}
}
}

Void maopao (Item a [], int l, int r)
{
Inti, j;
For (I = l; I <r; I ++)
{
For (j = r; j> I; j --)
{
If (less (a [j], a [J-1])
{
Itemtemp = a [j];
A [j] = a [J-1];
A [J-1] = temp;
}
}
}
}

 

In the worst case and average case, Bubble Sorting performs about N * N/2 Comparison operations and N * N/2 exchange operations.

5. Quick sorting

The quick sorting algorithm is a sort algorithm that divides arrays into two parts and sorts the two parts respectively. It sorts the array to meet the following three conditions:

1. For an I, a [I] is at the final position

2, a [l]… The elements in a [I-1] are smaller than those in a [I]

3, a [I + 1]… The elements in a [r] are larger than those in a [I ].

After division, the current round of sorting is completed, and then the sub-files are processed recursively.

If the array contains one or zero elements, nothing is done. Otherwise, you can call the following algorithms to perform quick sorting:


[Cpp]
Int partition (Itema [], int l, int r)
{
Int I = L-1, j = r;
Item v = a [r];
For (;;)
{
While (less (a [++ I], v ))
{
;
}

While (less (v, a [-- j])
{
If (j = 1)
{
Break;
}
}
If (I> = j)
{
Break;
}
Exch (a [I], a [j]);
}
Exch (a [I], a [r]);

Return I;
}

Int partition (Itema [], int l, int r)
{
Int I = L-1, j = r;
Item v = a [r];
For (;;)
{
While (less (a [++ I], v ))
{
;
}
 
While (less (v, a [-- j])
{
If (j = 1)
{
Break;
}
}
If (I> = j)
{
Break;
}
Exch (a [I], a [j]);
}
Exch (a [I], a [r]);
 
Return I;
}

 

The variable v stores the partition element a [r], and the I and j are left scan pointers and right scan pointers, respectively. Division loops reduce I by j, while loops maintain a constant property --------- no elements on the left of I are larger than v, and no elements on the Right of j are smaller than v. Once the two pointers meet, we will switch between a [I] And a [r]. In this way, the elements on the left side of v are smaller than v, and those on the right side of v are greater than or equal to v, end the division process.

Division is an uncertain process. When two pointers encounter each other, the break statement ends. Test j = 1 to prevent the partition element from being the smallest element in the file.


[Cpp]
Voidquichsort (Item a [], int l, int r)
{
Int I;
If (r <= l)
{
Return;
}

I = partition (a, l, r );
Quichsort (a, l, I-1 );
Quichsort (a, I + 1, r );
}

Voidquichsort (Item a [], int l, int r)
{
Int I;
If (r <= l)
{
Return;
}
 
I = partition (a, l, r );
Quichsort (a, l, I-1 );
Quichsort (a, I + 1, r );
}

 

Note: quick sorting is a recursive division process. We divide a file. The Division principle is to put some elements at its final position, sort the array so that elements smaller than the Division element are on the left of the Division element, and those larger than the Division element are on the right of the Division element, then perform recursive processing on the left and right parts. Then, scan from the left side of the array until you find an element greater than the Division element, and scan from the right side of the data until you find an element smaller than the Division element. The two elements that stop the scan are obviously opposite in the final division array, so they are exchanged. To continue this process, we can ensure that elements smaller than the Division elements are on the left of the Division elements, and those larger than the Division elements are on the right of the Division elements.

 

 

Related Article

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.