Research on sorting algorithms

Source: Internet
Author: User

There are many sorting algorithms, and their algorithms vary according to their ideas. Next we will study several common sorting algorithms.

 

1. Select sorting method

Sorting is to select the smallest element from the unsorted data and put it at the beginning (or the last) of the series ). After sorting, remove the first element, sort the subsequent data, and so on.

The program implementation is as follows:

# Include <stdio. h>

# Pragma hdrstop

# Include <tchar. h>

# Pragma argsused

Int _ tmain (INT argc, _ tchar * argv [])

{

Int I, j, a [10];

Int K, M;

Printf ("Please input 10 digitals: \ n ");

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

{

Scanf ("% d", & A [I]);

}

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

{

K = I;

For (j = I + 1; j <10; j ++)

{

If (A [k]> A [J])

K = J;

}

If (K! = I)

{

M = A [I];

A [I] = A [k];

A [k] = m;

}

}

Printf ("the sorted digitals: \ n ");

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

{

Printf ("% d", a [I]);

}

Return 0;

}

2. Bubble Sorting:

The Bubble sorting method sorts the numbers based on the bubble's floating and sinking. The idea is to compare the adjacent two numbers, put them in front, and put the numbers in front. First compare the first number and the second number. Then, compare the second number and the third number after the decimal place, before the decimal place, and after the large number... And so on until the last two numbers are compared, and then repeat the above steps again until the sorting is complete.

The program algorithm is implemented as follows:

Int _ tmain (INT argc, _ tchar * argv [])

{

Int I, j, a [10];

Int K;

Printf ("Please input 10 digitals: \ n ");

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

{

Scanf ("% d", & A [I]);

}

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

{

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

{

If (A [J]> A [J + 1])

{

K = A [J];

A [J] = A [J + 1];

A [J + 1] = K;

}

}

}

Printf ("the sorted digitals: \ n ");

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

{

Printf ("% d", a [I]);

}

Return 0;

}

3. Insert sorting

The basic operation of insert sorting is to insert a data to an ordered data that has been sorted, so as to obtain a new number plus 1 ordered data. This algorithm is applicable to sorting a small amount of data.

The implementation steps are as follows:

(1) starting from the first element, this element can be considered sorted.

(2) extract the next element and scan it forward from the back in the sorted element sequence.

(3) If the element (sorted) is greater than the new element, move the element to the next position.

(4) Repeat Step (3) until you find the position where the sorted element is smaller than or equal to the new element.

The program algorithm is implemented as follows:

Int _ tmain (INT argc, _ tchar * argv [])

{

Int I, j, a [10];

Int K;

Printf ("Please input 10 digitals: \ n ");

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

{

Scanf ("% d", & A [I]);

}

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

{

K = A [I];

J = I-1;

While (k <A [J]) & (j> = 0 ))

{

A [J + 1] = A [J];

J --;

}

A [J + 1] = K;

}

Printf ("the sorted digitals: \ n ");

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

{

Printf ("% d", a [I]);

}

Return 0;

}

 

4. Hill sorting

Hill sorting is a kind of insert sorting, which is an improvement of the direct insertion sorting algorithm. This method is also called to narrow down incremental sorting, which was named after D. l. Shell in 1959.

First, take an integer D1 less than N as the first increment, and divide all the data elements into D1 groups. The number of elements in the same group is a multiple of D1, insert and sort directly in each group, and then take the second incremental D2 <d1, repeat the group and sort until the incremental dt = 1 (dt <dt-1 <... <D2 <d1), that is, all elements are placed in the same group for insertion sorting.

The program algorithm is implemented as follows:

# Include <stdio. h>

# Pragma hdrstop

# Include <tchar. h>

# Pragma argsused

Int _ tmain (INT argc, _ tchar * argv [])

{

Int I, j, a [10];

Int T, K = 10;

Printf ("Please input 10 digitals: \ n ");

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

{

Scanf ("% d", & A [I]);

}

While (k> 0)

{

For (I = K; I <10; I ++)

{

J = I-K;

While (j> = 0)

{

If (A [J]> A [J + k])

{

T = A [J];

A [J] = A [J + k];

A [J + k] = T;

}

Else

Break;

}

}

K/= 2;

}

Printf ("the sorted digitals: \ n ");

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

{

Printf ("% d", a [I]);

}

Return 0;

}

Hill sorting is an algorithm based on insert sorting. It adds a new feature that improves efficiency but is not stable. The sorting of medium-size data volumes is good. Because the efficiency of hill sorting is similar to that of average in the worst case, fast sorting is very inefficient in the worst case, therefore, we recommend that you use the hill sorting method to sort any sorting task. If it is not fast enough, you can change it to a more advanced sorting algorithm such as quick sorting.

 

5. Quick sorting

Quick sorting is an improvement of Bubble sorting, which was proposed by C. A. R. Home in 1962. The basic idea is to split the data to be sorted into two independent parts by one sort. All the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be achieved through recursion, and the entire data has become an ordered series.

The program algorithm is implemented as follows:

# Include <stdio. h>

# Pragma hdrstop

# Include <tchar. h>

# Pragma argsused

Int P (int A [], int low, int high)

{

Int key = A [low], P;

While (low

{

While (low <High & A [High]> = key)

-- High;

P = A [low]; A [low] = A [High]; A [High] = P;

While (low <High & A [low] <= key)

++ Low;

P = A [High]; A [High] = A [low]; A [low] = P;

}

A [low] = key;

Return low;

}

 

Void Q (int A [], int low, int high)

{

Int J;

If (low

{

J = P (A, low, high );

Q (A, low, J-1 );

Q (A, J + 1, high );

}

}

 

Int _ tmain (INT argc, _ tchar * argv [])

{

Int I, a [10];

Printf ("Please input 10 digitals: \ n ");

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

{

Scanf ("% d", & A [I]);

}

Q (A, 0, 9 );

Printf ("the sorted digitals: \ n ");

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

{

Printf ("% d", a [I]);

}

Return 0;

}

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.