Sorting 1 + 2: exchangesort, selectsort and insertsort)

Source: Internet
Author: User

Severe statement: This post is transferred from http://www.cnblogs.com/FlyingBread/archive/2007/01/27/631830.html, forward please indicate the source.

 

Exchange sorting, insert sorting, and select sorting are both simple and basic sorting algorithms. They are easily traced in the book of various algorithms and data structures. It is nothing more than deepening my impression and providing a reference for friends who need it.

1. Exchange sorting

Exchange sorting is a simple sorting method. The main idea is as follows:
(1) compare the data d [I] with other data d [J] in the range by using the 0th data d [I] in the range to be sorted. If the preceding data is larger than the latter, the two data will be exchanged (that is, the smaller data will be placed in the second position ). Then compare it with the next data in the sorting range d [J + 1. And so on.

(2) After completing step (1), the minimum value is the 0th position in the range. Then, find the smallest data in the remaining data and place it in the first place. In this way, the data is searched cyclically until all data ends. Finally, the sorting results are sorted from small to large.

The idea of exchange sorting is similar to that of Bubble sorting, and the Implementation Code is also very similar. The following is a simple example.
Assume that the data to be sorted is: 6 2 5 1 3 4

Step 1:
Because 6> 2, 2 and 6 are exchanged.
2 6 5 1 3 4
Step 2: Do not switch because 2 <5
2 6 5 1 3 4
Step 3: switch because 2> 1
1 6 5 2 3 4
Step 4: Do not switch because 1 <3
1 6 5 2 3 4
Step 5: Do not switch because 1 <4
1 6 5 2 3 4

In this way, we find the minimum value 1. In the same way, we can find the second and third minimum values .. Complete sorting.

The implementation code is very simple, as shown below:

/// <Summary>
/// The idea of exchanging sorting is very simple. Start from the first data and find
/// Data smaller than it. If it is found, it is exchanged between the two.
/// For the first time, the minimum data can be found and exchanged with 0th data records.
/// Search for the remaining n-1 data for the second time, and find the smallest and first data exchange
/// Exchange all the remaining data in sequence
/// </Summary>
/// <Param name = "myarray"> </param>
Public static void exchangesort (INT [] myarray)
{
Int I, J;

For (I = 0; I <myarray. Length-1; I ++) // starts with 0th pieces of data
{
For (j = I + 1; j <myarray. length; j ++) // loops in the remaining data
{
If (myarray [J] <myarray [I]) // if there is something smaller than it, exchange the two
{
Swap (ref myarray [J], ref myarray [I]);
}
}
}
}

2 select sorting

Selecting sorting is also a simple sorting method. The main idea is as follows:
(1) Find the smallest value a [I] in the array for the first time, and exchange it with the 0th positions.

(2) There are n-1 values left, and the smallest value a [J], a [J] and the first data exchange are also found.

(3) Repeat the preceding steps to find the minimum value and the minimum value under the array in the remaining range for exchange.

Generally, an extra small variable is set to record the subscript of the minimum value, so that the values at the specified position and the minimum value can be exchanged after the loop. Based on the above description, we can easily write the algorithm for selecting the sort.

/// <Summary>
/// The central idea of sorting is to select the smallest data from the data that has never been sorted each time,
/// The first data exchange with unordered data.
/// For the first time, the minimum data value is selected and placed in 0th locations.
/// For the second time, select the smallest data from the remaining n-1 data and place it in the first position.
/// Sort all data in sequence
/// </Summary>
/// <Param name = "myarray"> </param>
Public static void selectsort (INT [] myarray)
{
Int I, j, small;

For (I = 0; I <myarray. Length-1; I ++) // data start position, from 0 to the second to the last
{
Small = I; // subscript of the minimum data record
For (j = I + 1; j <myarray. length; j ++) // find the minimum data in the remaining data
{
If (myarray [J] <myarray [small])
{
Small = J; // if there is something smaller than it, record the subscript
}
}
Swap (ref myarray [I], ref myarray [small]); // swap the minimum data with the first unordered data
}
}

3 insert sort

Insert sorting is also a simple sorting method based on the principle. The main idea is as follows:

(1) first, insert the value from the first data to the data that has been sorted before it. The insert position is before the first record greater than the data. If there is no data greater than it, put it at the end of the sorted data. As sorting has not started yet, there are only 1st elements.

(2) next to the second element, insert it in step 1 to the front of the number greater than it. Now the first and second data are ordered.

(3) You can rank the third and fourth data in sequence until all the data is fully sorted. The detailed process can be seen from the example below.

Or suppose the number to be sorted is: 6 2 5 1 3 4

Step 1: Move the first
6 2 5 1 3 4
Step 2: Move the second one. Because 2 <6, put 2 before 6.
2 6 5 1 3 4
Step 3: Move the third one. Because 2 <5 <6, put 5 before 6.
2 5 6 1 3 4
Step 4: Move the fourth one. Because 1 <2 <5 <6, 1 is placed at the beginning.
1 2 5 6 3 4
Step 5: Move the fifth entry. Because 2 <3 <5 <6, 3 is placed behind 2.
1 2 3 5 6 4
Step 6: Move the sixth one. Because 3 <4 <5 <6, 4 is placed after 3.
1 2 3 4 5 6

In this way, all the data is sorted.

The algorithm is as follows:

/// <Summary>
/// The central idea of insertion sorting is to insert unordered data into an ordered table.
/// Put the first data in an ordered table for the first time, which has only one data
/// Insert the second data into an ordered table for the second time, so that the data size is smaller than the previous data size.
/// Insert the third data into the ordered table for the third time
/// Sort all data in an ordered table
/// </Summary>
/// <Param name = "myarray"> </param>
Public static void insertsort (INT [] myarray)
{
Int I, j, temp;

For (I = 1; I <myarray. length; I ++)
{
Temp = myarray [I]; // Save the current data
J = I-1;

// Insert data into an ordered table. If the data in the table is larger than the data,
// Move the data in the ordered table backward until the first data smaller than it is found,
// Put it behind the data
While (j> = 0 & myarray [J]> temp)
{
Myarray [J + 1] = myarray [J];
J --;
}

Myarray [J + 1] = temp; // insert data into an ordered table
}
}

4. Efficiency Comparison

After these three algorithms are implemented, these three algorithms are O (N ^ 2) algorithms. Efficiency is not high. Let's make a small experiment to verify the efficiency, or use the random number in 1 + 1 to test the efficiency. We can see the following table to see the efficiency differences between these algorithms. Unit: milliseconds

500 Random Number 5000 Random Number 20000 Random Number
Basic bubble 1.5625 160.9375 2457.8125
Exchange sorting 1.5625 118.75 1895.3125
Select sort 1.5625 68.75 1081.25
Insert sort 1.5625 34.375 543.75

As shown in the preceding table, insert sorting is much faster than several other sorting methods. The larger the data volume, the more obvious. Is there a faster sorting method? The answer is yes.

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.