C # Sorting Algorithm

Source: Internet
Author: User

/*************** Bubble sort **************/
Public class bubblesorter
{
Public static void sort (INT [] list)
{
Int I, J;
For (I = 0; I <list. length; I ++)
{
For (j = 0; j <list. Length-1-I; j ++)
{
If (list [J]> list [J + 1])
{
Int temp = list [J];
List [J] = list [J + 1];
List [J + 1] = temp;
}
}
}
}
}

/**************** Selection sort *************/
Public class selectionsorter
{
Public static void sort (INT [] list)
{
Int I, J;
For (I = 0; I <list. length; I ++)
{
For (j = I + 1; j <list. length; j ++)
{
If (list [J] <list [I])
{
Int temp = list [J];
List [J] = list [I];
List [I] = temp;
}
}
}
}
}

/************* Insert sort ***********/
Public class insertsorter
{
Public static void sort (INT [] list)
{
Int I, J;
For (I = 1; I <list. length; I ++)
{
Int temp = list [I];
J = I;
While (j> 0 & list [J-1]> temp)
{
List [J] = list [J-1];
J --;
}
List [J] = temp;

}

}
}

/************* Quick Sort ***************/
Public class quicksorter
{
Public static void sort (INT [] list)
{
Quicksort (list, 0, list. Length-1 );
}
Public static void quicksort (INT [] list, int low, int high)
{
Int temp = list [(low + high)/2];
Int I = low, j = high;
Do
{
While (I <High & list [I] <temp)
I ++;
While (j> low & list [J]> temp)
J --;
If (I <= J)
{
Swap (Ref List [I], Ref List [J]);
I ++;
J --;
}

}
While (I <= J );
If (j> LOW)
Quicksort (list, low, J );
If (I Quicksort (list, I, high );

}

Public static void swap (ref int L, ref int R)
{
Int temp = L;
L = R;
R = 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.