I heard that there are ten sorting algorithms, first of all to learn the fast sorting and counting sorting algorithm.
One: Quick sort
The quick sort is basically the position of each of the number of rulers that are currently taken out, then the number less than it on the left (from small to large rows), the number greater than it on the right. Then using recursion to continue to find the location of the number of positions, the process is a bit similar to the former according to the pre-order and the sequence of the order of the topic. The exit of recursion is that when there is only one number, there is no need to row. The position taken out is his position.
Code:
Find the location for each time:
int findpos (int l,int r) {int temp = Data[l];int i = l, j = r;while (i! = j) {while (Data[j] >= Temp&&i < J) J- -;d Ata[i] = Data[j];while (Data[i] <= temp&&i < j) i++;d ata[j] = data[i];} Data[i] = Temp;return i;}
Recursive sub-region find location:
void quicksort (int l,int r) {if (L >= r) Return;int pos = Findpos (l, R); quicksort (l,pos-1); quicksort (pos+1,r);}
There are a number of things to note:
1. A recursive exit should be placed at the top of the quicksort, not on the top, if only one number is going to find his position, it may get into a dead loop.
This symmetric while statement in 2.while (I!=J) can be written with a note on the back.
while (Data[j] >= temp&&i < j) j--;d ata[i] = Data[j];while (Data[i] <= temp&&i < j) i++;d Ata[j] = Data[i];
Two: Counting sort
The count sort does not have to be compared, and the number of occurrences of the current array is recorded in an array during the reading process (in fact, the sorting process is implemented with the subscript of the Count array). Note that the value of each loop variable is:
The first loop is for count zeroing, when the loop variable should be equal to the number of subscripts of Count, which is the range of the array to be sorted; (the subscript of thearray element is greater than the range of the number to be sorted)
When the second loop is counted, the loop variable and the number of elements of the array to be sorted are the same;
The third loop is the operation of the Count array, summing up the position where the current element should be placed in the sorted array. That is, how many numbers are smaller than him, and the loop variable should match the length of count;
The fourth cycle is the assignment of the ordered result, and the loop variable is the same as the number of arrays to be sorted.
Code:
int count[1001];int ans[10];void Countsort () {for (int i = 0; i <; i++) {count[i] = 0;} for (int i = 0; i < i++) {count[data[i]]++;} for (int i = 1; i < i++) {Count[i] = Count[i] + count[i-1];} for (int i = 0; i < ten; i++) {ans[--count[data[i]]] = Data[i];}}
Complete test Code:
#include <stdio.h>int data[10] = {2, 3, 1, 5, 7, 6, 4, 8, 2, 0};int findpos (int l,int r) {int temp = Data[l];int I = L, j = r;while (i! = j) {while (Data[j] >= temp&&i < j) j--;d ata[i] = Data[j];while (Data[i] <= temp& &i < J) i++;d ata[j] = data[i];} Data[i] = Temp;return i;} void quicksort (int l,int r) {if (L >= r) Return;int pos = Findpos (l, R); quicksort (l,pos-1); quicksort (pos+1,r);} int count[1001];int ans[10];void Countsort () {for (int i = 0; i <; i++) {count[i] = 0;} for (int i = 0; i < i++) {count[data[i]]++;} for (int i = 1; i < i++) {Count[i] = Count[i] + count[i-1];} for (int i = 0; i < ten; i++) {ans[--count[data[i]]] = Data[i];}} int main () {countsort (); return 0;}
Quick sort and Count sort API