Use C language to Implement Bubble sorting, select sorting, and fast sorting

Source: Internet
Author: User

The Code is as follows:
Copy codeThe Code is as follows :/*
* Bubble Sorting
*/
Void BubbleSort (int arr [], int n)
{
Int temp;
For (int I = 0; I <n-1; I ++)
{
For (int j = I + 1; j <n; j ++)
{
If (arr [I]> arr [j])
{
Temp = arr [I];
Arr [I] = arr [j];
Arr [j] = temp;
}
}
}
}
/*
* Select sort
*/
Void ChooseSort (int arr [], int n)
{
Int temp, k;
For (int I = 0; I <n-1; I ++)
{
K = I;
For (int j = I + 1; j <n; j ++)
{
If (arr [k]> arr [j])
{
K = j;
}
}
If (k! = I)
{
Temp = arr [I];
Arr [I] = arr [k];
Arr [k] = temp;
}
}
}
/*
* Quick sorting, official Original Version
*/
Void q_sort (int numbers [], int left, int right)
{
Int hold, l_hold, r_hold;
Rochelle hold = left;
R_hold = right;
Numbers = numbers [left];
While (left <right)
{
While (numbers [right]> = numbers) & (left <right ))
{
Right --;
}
If (left! = Right)
{
Numbers [left] = numbers [right];
Left ++;
}
While (numbers [left] <= numbers) & (left <right ))
{
Left ++;
}
If (left! = Right)
{
Numbers [right] = numbers [left];
Right --;
}
}
Numbers [left] = numeric;
Rows = left;
Left = l_hold;
Right = r_hold;
If (left <ignore)
{
Q_sort (numbers, left, limit-1 );
}
If (right> else)
{
Q_sort (numbers, numbers + 1, right );
}
}
/*
* Quick sorting
*/
Void quick_sort (int * x, int low, int high)
{
Int I, j, t;
If (low {
I = low;
J = high;
T = * (x + low);/* Number of temporary points */
While (I <j)/* cyclic scan */
{
While (I <j & * (x + j)> t)/* If the value on the right is larger than the value on the reference point, it is still placed on the right */
{
J --;/* move a forward position */
}
If (I <j)
{
* (X + I) = * (x + j);/* loop exit above: A number smaller than the benchmark value. Replace the benchmark value */
I ++;/* move a position behind and use this as the benchmark */
}
While (I <j & * (x + I) <= t)/* on the left, as long as the value is less than or equal to the benchmark, it is still placed on the left */
{
I ++;/* move a location behind */
}
If (I <j)
{
* (X + j) = * (x + I);/* loop exit above: A number larger than the benchmark value. Put it on the right */
J --;/* move a forward position */
}
}
* (X + I) = t;/* after scanning, place it in the appropriate position */
Quick_sort (x, low, I-1);/* sort the number on the left of the reference point quickly */
Quick_sort (x, I + 1, high);/* sort the number on the right of the benchmark */
}
}
// Output array elements
Void outArray (int arr [], int n)
{
For (int I = 0; I <n; I ++)
{
Cout <arr [I] <"";
}
Cout <endl;
}
Void main ()
{
Const int N = 5;
Int arr1 [N] = {4, 3, 5, 2, 1 };
Int arr2 [N] = {4, 3, 5, 2, 1 };
Int arr3 [N] = {4, 3, 5, 2, 1 };
Cout <"Before bubble sort" <endl;
OutArray (arr1, N );
BubbleSort (arr1, N );
Cout <"After bubble sort" <endl;
OutArray (arr1, N );
Cout <"/nBefore chooose sort" <endl;
OutArray (arr2, N );
ChooseSort (arr2, N );
Cout <"After chooose sort" <endl;
OutArray (arr2, N );
Cout <"/nBefore quick sort" <endl;
OutArray (arr3, N );
// Q_sort (arr3, 0, N-1 );
Quick_sort (arr3, 0, N-1 );
Cout <"After quick sort" <endl;
OutArray (arr3, N );
System ("pause ");
}

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.