Comparison of Three sorting methods

Source: Internet
Author: User

/**
* Comparison of the three sorting methods: Bubble sorting, select sorting, and insert sorting
* This process is required for sorting the three types of data.
* Compare the two values, and then copy or swap the location (in the program, the swap location is more performance-consuming because it involves moving in the memory)
*
* For an array with a capacity of N:
*
* Bubble Sorting:
* First need to compare the number of times for N-1, N-2, N-3, N-4,... 1, total is N * (N-1)/2
* If the number of exchanges in a random case is basically half of the number of comparisons,

* Therefore, the number of bubble sort comparisons and the number of exchanges are proportional to the square of N.

* If an exchange condition is added during the traversal and comparison of each bubble, if exchange = 0 is not exchanged, if exchange = 1 is exchanged;

* The minimum number of comparisons with bubbling can be changed to a N-1.

*
* Select sorting
* First select the sort the number of times to be compared is N * (N-1)/2
* The number of times to be exchanged is less than N,
*
* Insert sorting
* For insertion sorting, the first time is required at most once, the second time is required at most twice, the third time is required at most three times, and the N-1 is required at most
* In a random case, insert sorting basically requires n * (N-1)/4, which is twice faster than Bubble Sorting in terms of the number of comparisons
* In terms of the number of exchanges, the number of exchanges is equal to the number of comparisons, also N * (N-1)/4, so the above, insert sort is a little faster than the selected sort
* But for insert sorting, if an array is in reverse order, the efficiency will decrease because the number of comparisons and the number of exchanges will change to N * (N-1)/2.
*
* For the above three sorts, they are nested inner loops in the outer loop, so it is easy to suspect that their efficiency is at the O (N square) level.
*
*/

/*
* Bubble Sorting
*/
Public static int [] Sort (INT [] arrays)
{
Int length = arrays. length;

Int Max;

Int exchange = 1; // If you find

For (INT I = length-1; I> 0, exchange = 0; I --)

{

Exchange = 0;

For (Int J = 0; j <I; j ++)
{
If (arrays [J]> arrays [J + 1])
{
Max = arrays [J];
Arrays [J] = arrays [J + 1];

Arrays [J + 1] = max;

Exchange = 1;

}
}
}
Return arrays;
}

/**
* Select sorting
* @ Param Arrays
* @ Return
*/
Public static int [] sort (int [] arrays)
{
Int length = arrays. length;
Int min;
Int index;
For (int I = 0; I <length-1; I ++)
{
Min = arrays [I];
Index = I;
For (int j = I; j <length; j ++)
{
If (min> arrays [j])
{
Min = arrays [j];
Index = j;
}
}
Arrays [index] = arrays [I];
Arrays [I] = min;
}
Return arrays;
}

/**
* Insert sorting
*/
Public static int [] sort (int [] arrays)
{
Int length = arrays. length;
Int min;
For (int I = 1; I <length; I ++)
{
Min = arrays [I];
For (int j = I-1; j> = 0; j --)
{
If (min <arrays [j])
{
Arrays [j + 1] = arrays [j];
Arrays [j] = min;
}
}
}
Return arrays;
}

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.