Several Simple sorting algorithms

Source: Internet
Author: User
// Bubble sort
Public static void PopSort (int [] arry)
{
For (int I = 0; I <arry. Length-1; I ++)
{
For (int j = I; j <arry. Length-1; j ++)
{
If (arry [I]> arry [j + 1])
{
Int temp = 0;
Temp = arry [I]; arry [I] = arry [j + 1]; arry [j + 1] = temp;
}
}
}
}
// Select sorting
Public static void SelectSort (int [] arry)
{
Int temp = 0, min = 0; // min stores the minimum subscript. arry [min] is the minimum value.
For (int I = 0; I <arry. Length; I ++)
{
Min = I; // The initial minimum value is an integer arry [I].
// Traverse the array, locate the minimum value, and assign the value to min
For (int j = I + 1; j <arry. Length; j ++)
{
If (arry [j] <arry [min])
{
Min = j;
}
}
// Move the minimum value arry [min] to the initial minimum value location arry [I]
Temp = arry [I];
Arry [I] = arry [min];
Arry [min] = temp;
}
}
// Insert sorting
Public static void InsertSort (int [] arry)
{
Int min = 0, temp = 0;
For (int I = 0; I <arry. Length; I ++)
{
Temp = arry [I];
Min = I;
While (min> 0 & arry [min-1]> temp)
{
Arry [min] = arry [min-1];
Min --;
}
Arry [min] = temp;
}
}

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.