Summary and Implementation of simple sorting algorithms (The Implementation of three easy sort algorithm)

Source: Internet
Author: User

Summary and implementation of simple sorting algorithms (The implementation of three easy sort algorithm)

The three simple sorting algorithms are respectively bubble method, and the sorting method and insertion method are selected.

The worst operating efficiency of the three sort algorithms is O (n * n)

 

1 Bubble sorting method:

Void bubbleSort (Type * arr, long len)/* bubble sort algorithm */

{

Long I = 0, j = 0;/* iterator value */

AssertF (arr! = NULL, "In bubble sort, arr is NULL/n ");

For (I = len; I> 1; I --)

For (j = 0; j <I-1; j ++)

If (arr [j]> arr [j + 1]) swapArrData (arr, j, j + 1 );

}

Starting from the position behind the array, if an element is found to be smaller than the number at the previous position, the two numbers are exchanged, A sorting process similar to the rise of light bubbles in water is formed.

 

 

2 insert sorting:

Void insertSort (Type * arr, long len)/* InsertSort algorithm */

{

Long I = 0, j = 0;/* iterator value */

Type tmpData;

AssertF (arr! = NULL, "In InsertSort sort, arr is NULL/n ");

For (I = 1; I <len; I ++)

{

J = I;

TmpData = arr [I];

While (tmpData <arr [J-1] & j> 0)

{

Arr [j] = arr [J-1];

J --;

}

Arr [j] = tmpData;

}

}

The idea of inserting the Sorting Algorithm is:

Assuming that the order of the array is arranged, and then from the beginning to the end, if the number is greater than the value of the current outer element, move the number back, until the value of the current outer element is greater than or equal to the position before it. this algorithm ensures that a [1... K] is partially ordered, ensuring the correctness of the insert process.

 

 

3. Select the sorting method:

Void selectionSort (Type * arr, long len)

{

Long I = 0, j = 0;/* iterator value */

Long maxPos;

AssertF (arr! = NULL, "In InsertSort sort, arr is NULL/n ");

For (I = len-1; I> = 1; I --)

{

MaxPos = I;

For (j = 0; j <I; j ++)

If (arr [maxPos] <arr [j]) maxPos = j;

If (maxPos! = I) swapArrData (arr, maxPos, I );

}

}

 

Select the first-level loop of the sorting method from the starting element to the second-to-last element. The subscript of the outer loop is assigned to the temporary variable before each second-level loop, in the next layer 2 loop, if an element is found to be smaller than the element at the smallest position, the subscript of the smaller element is assigned to the temporary variable. Finally, after a L2 loop exits, if the temporary variable changes, it indicates that there are elements smaller than the current outer loop location, and the two elements need to be exchanged.

 

Because these three algorithms are relatively simple and run less efficiently, it is not a problem to use them when the number of elements is small. however, when the number of elements to be sorted is greater than or equal to 10000, the value of use is basically lost. On the current mainstream machines,

When there are 10000 elements, the time unit for sorting is almost seconds. (relatively speaking ,)

By the time of 100000 elements, the sorting time unit is minutes. At this time, the running time unit of quick sorting is

Still 1% seconds.

Parameters:

Http://blog.csdn.net/EmilMatthew/archive/2005/09/04/471018.aspx

 

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.