Bubble sort, select sort, and insert sort

Source: Internet
Author: User

I. Bubble Sorting

Compare two adjacent numbers in sequence, place decimal places in front, and place large numbers in the back. That is, first compare the numbers of 1st and 2nd, and put the decimal places before and after the large numbers. Then compare the numbers of 2nd and 3rd, place the decimal places before and after the large number, and continue until the last two digits are compared. Place the decimal places before and after the large number. Repeat the preceding process until the sorting is completed.

 

Bubble Sorting is stable.AlgorithmThe time complexity is O (n ^ 2 ).

 

ProgramImplementation example

Sort arrays from small to large

Public void bubblesort (INT [] array)

{

Int length = array. length;

For (INT I = 0; I <= length-1; I ++)

{

For (Int J = length-1; j> I; j --)

{

If (array [J] <array [J-1])

{

Int temp = array [J];

Array [J] = array [J-1];

Array [J-1] = temp;

}

}

}

}

 

 

Ii. Select sorting

Each trip selects the smallest (or largest) element from the data element to be sorted, and places it at the end of the sorted series until all the data elements to be sorted are arranged. Selecting sorting is an unstable sorting method.

 

The sorting is unstable. The algorithm complexity is O (n ^ 2 ).

 

Program Implementation example

Sort arrays from small to large

 

Public void choicesort (INT [] array)

{

Int min;

For (INT I = 0; I <array. Length-1; I ++)

{

Min = I;

For (Int J = I + 1; j <array. length; j ++)

{

If (list [J] <list [Min])

Min = J;

}

Int T = list [Min];

List [Min] = list [I];

List [I] = T;

}

}

 

Iii. Insert sorting

 

Each time the first element is extracted from the unordered table, it is inserted to the proper position of the ordered table, so that the ordered table is still ordered.

 

Direct insertion and sorting are stable. The algorithm time complexity is O (n ^ 2)

 

Program Implementation example

Sort arrays from small to large
Public void insertsort (INT [] array)
{
For (INT I = 1; I <array. length; I ++)
{
Int temp = array [I]; // The number of retrieved from the unordered table, which is placed in temp.

// J> 0 & temp <array [J-1] conditions for stopping each round of sorting

For (Int J = I; j> 0 & temp <array [J-1]; j --)

{

Array [J] = array [J-1];

}

Array [J] = temp;
}
}

 

PS:

1) stable: if there are multiple records with the same sorting code and the relative sequence of these records remains unchanged after sorting, This sorting algorithm is called stable.
Insert sorting, Bubble sorting, Merge Sorting, and distributed sorting (bucket and base) are all stable sorting algorithms.
2) unstable: otherwise it is called unstable.
Direct selection of sorting, heap sorting, shell sorting, and quick sorting are unstable sorting algorithms.

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.