Select and Bubble
#include <stdio.h> void Maopao (int a[],int len) {int i,j,temp; for (i = 0;i < len-1 i + +) {//from the first to the penultimate for (j = 0; J < len-1-I; j + +)/is sorted after {if
(A[j] > a[j + 1])//large number in exchange for the back to go {temp = a[j];
A[J] = a[j + 1];
A [j + 1] = temp;
}} void Xuanze (int a[],int len) {int I, J, T, temp;
for (i = 0; i < len-1 i + +) {t = i;
for (j = i + 1; j < Len; j + +)//front of the real line {if (a[t] > A[j]) {t = j;//Note the number of the minimum number of the trip
} if (t!= i)//If the serial number does not change do nothing {temp = a[t];//otherwise element exchange a[t] = A[i];
A[i] = temp;
}} void Main () {int i;
int a[] = {5,4,6,7,2,5,4,6,8,9,1,2};
Maopao (A, 12);
Xuanze (A, 12);
for (i = 0; i < i + +) {printf ("%d", a[i]); }
}
&NBSP
Quick Sort vs. bubbling, selecting comparison:
#include <stdio.h> #include <time.h> #include <windows.h>//Quick Sort, parameters are array, lowest index, highest index (starting from 0) void Qsort (int a[], int low, int high)
{int temp; int mid = low;//defines a medium index that is used to record an element index that determines the position after a sort; int right = high;//record the leftmost element index//default median is the left value, now place the element that is larger than the middle value to the median left while ( Right > Mid) {//therefore traverse from the middle to the median, traversing the end if (A[right] < A[mid])//If the right value is smaller than the median, it means that he should not be on the right side and put it on the left {temp = a[ mid];//so first the median of the site to vacate a[mid] = a[right];//put the number of smaller than the median there//there is no place to put the value, you must move the right shift, but directly to the right to cover the original value of his right a[right] = a [++mid];//But the right index space has been vacated, the median value of the original to the right-hand side of the value of the a[mid] = temp;//Then you can move mid to the right one} else{right--;//otherwise
Say right is already greater than mid, do not move, continue to judge right left that number}}//so right and mid coincide with, exit the loop, at this time the position of mid has been determined is the row after the order of his position//because his left side is smaller than him, the right-hand side is bigger than him
if (Mid-Low > 1)//If there are two or more elements between low and mid, also sort them by {qsort (A, low, mid-1);
if (High-mid > 1)//The right half is the same. {Qsort (A, mid + 1, high); } void Ssort (int a[], int len) {//select sort, parameter is array name and elementnumber int I, j, M, temp; for (i = 0; i < len-1 i++)//from start to last second element {m = i;//first record leftmost element index, assuming he is the smallest for (j = i + 1; j < Len; j +)/ Iterate from left to right until the last {if (A[j] < a[m])//If a smaller element {m = j;//is found to note the index of this element}} if (M!=
i)//if the smallest element is not the beginning of that, you need to change the smallest to the leftmost {temp = A[i];
A[i] = a[m];
A[M] = temp;
}} void Msort (int a[], int len) {//bubble sort, parameter is array name, number of elements int i, j, temp;
for (i = 0; i < len-1 i + +)/from start to penultimate element {for (j = 0; J < len-1-i)//start over to ordered queue forward second {
if (A[j] > a[j + 1])//If the element is larger than the latter one, then exchange {temp = A[j];
A[J] = a[j + 1];
A[j + 1] = temp;
}//the maximum element after one traversal is put to the last face} int checksorted (int a[], int len) {//Check whether the array sort is correct int i;
for (i = 0; i < len-1 i++) {if (A[i] > a[i + 1])//If an element is larger than his next one, the error {return 0;
} return 1;
} void Main () {int i, J; int a[99999];
clock_t begin, end;
Double cost;
for (j = 0 J < 6 j + +)/do 6 test {srand ((int) time (0))///////////////////////////////////////////
{A[i] = rand ();//Generate random number into array} begin = Clock ();
Qsort (A, 0, 99998);//1 seconds or so//ssort (a, 99999);//20 seconds or so Msort (a, 99999);//40 seconds or so, clock ();
for (i = 0; i < i++)/output The first few sorted elements {printf ("%d", a[i]); The cost = (double) (end-begin)/clocks_per_sec;//calculates the sort time printf ("... \ t sort spents%lf seconds", cost), or the time that the output is sorted if
(Checksorted (A, 99999))//check whether the order is correct {printf ("correct!\n");
}else{printf ("Wrong!\n");
Sleep (1200);//pause, make each time seed different}}
1. Quick Sort Results:
99,999 random numbers in general no more than 0.05 seconds, soon
2. Sorting results by Selection method:
99,999 random numbers in general no more than 0.05 seconds, soon
2. Sorting results by Selection method:
Generally in more than 20 seconds;
3. Bubble method, in general, the number of exchanges will be many, the result:
The sorting time is generally about 50 seconds, the slowest.
In C + +, you can also define a function template
Because fast sorting is usually quick, it is used to sort different data types
#include <iostream.h> template<class t> void qsort (t a[], int low, int high) {//in C + +, you can define the function template T temp;
int mid = low;
int right = high;
while (right > mid) {if (A[right] < A[mid]) {temp = A[mid];
A[mid] = A[right];
A[right] = A[++mid];
A[mid] = temp;
}else{right--;
} if (Mid-Low > 1) {qsort (A, low, mid-1);
} if (High-mid > 1) {qsort (A, mid + 1, high);
} void Main () {int a[10] = {1, 9, 6, 3, 5, 7, 1};//has a function template that can be sorted on an integral type qsort (A, 0, 9);
for (int i = 0; i < i++) {cout<<a[i]<< "";
} cout<<endl;
Float B[10] = {2.0f, 1.2f, 5.5f, 6.63f, 9.11f, 1.32f, 3.44f, 5.0f, 5.22f, 0.02f};
Qsort (b, 0, 9);//To sort floating-point numbers for (i = 0; i < i++) {cout<<b[i]<< "";
} cout<<endl;
Char c[10] = {' d ', ' e ', ' f ', ' n ', ' J ', ' C ', ' E ', ' B ', ' F ', ' a '};//to sort the array of characters qsort (c, 0, 9); for (i = 0; I < 10;
i++) {cout<<c[i]<< "";
} cout<<endl;
}//can be sorted by the type of ' < ' that can be used to compare size.