Bubble sort, select sort, fast sort _c language in C language

Source: Internet
Author: User
The code looks like this:
Copy Code code as follows:

/*
* Bubble Sort
*/
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 Sort, official original
*/
void Q_sort (int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
L_hold = left;
R_hold = right;
Pivot = Numbers[left];
while (left < right)
{
while ([numbers[right] >= pivot) && (left < right))
{
right--;
}
if (left!= right)
{
Numbers[left] = Numbers[right];
left++;
}
while ([Numbers[left] <= pivot) && (left < right))
{
left++;
}
if (left!= right)
{
Numbers[right] = Numbers[left];
right--;
}
}
Numbers[left] = pivot;
Pivot = left;
left = L_hold;
right = R_hold;
if (left < pivot)
{
Q_sort (Numbers, left, pivot-1);
}
if (Right > Pivot)
{
Q_sort (Numbers, pivot+1, right);
}
}
/*
* Quick Sort
*/
void Quick_sort (int *x, int low, int high)
{
int I, j, T;
if (Low < high)/* The element to be sorted starts and starts subscript, ensuring that the small is placed on the left and the large on the right. Here the following elements marked as low are reference points.
{
i = low;
j = high;
t = * (X+low); /* The number of temporary datum points * *
while (I&LT;J)/* Cyclic scan * *
{
while (i<j && * (x+j) >t)/* On the right as long as the reference point is still large on the right/
{
j--; /* Move forward one position * *
}
if (I&LT;J)
{
* (x+i) = * (X+J); /* The above loop exit: That is, the number of smaller than the Datum point, replace the number of reference points * *
i++; /* Move one position back, and use this as the reference point * *
}
while (i<j && * (x+i) <=t)/* On the left as long as the reference point is less than or equal to the left * * *
{
i++; /* Move one position back/*
}
if (I&LT;J)
{
* (X+J) = * (X+i); /* The above loop exit: That is, the number of larger than the Datum point, put to the right/
j--; /* Move forward one position * *
}
}
* (x+i) = t; * * Once again after scanning, put to the appropriate position/
Quick_sort (x,low,i-1); /* Do a quick sort on the left side of the datum point * *
Quick_sort (X,i+1,high); /* Do a quick sort on the number to the right of the datum point.
}
}
Output array element
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.