Recently, because of the work of things have been dealt with almost, there is no task at hand, in is always a sort of algorithm only some vague concept, school that will not
Good learning and practical operation on the machine, so this deliberately organized the next few more classic sorting algorithm, but also in the machine tried several times no problem
, hope to be helpful in the future, if there are shortcomings, to be improved!
I don't want to say anything else, just go to the code.
/*function: Exchange two number functions */void swap (int *num1, int *num2) {int temp;temp = *NUM1;*NUM1 = *num2;*num2 = temp;}
/* Bubble Sorting algorithm Simple description: In order to sort the number of the current is not yet ranked in the range of the total number, the top-down to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. Bubble sort is, algorithm time complex charge 0 (n2)--[n squared] */void bobblesort (int *array, int len) {int i, J; for (i=0; i<len; i++) {for (j=len-1) j>=i; j--) {if (Array[i]>array[j]) {swap (&array[i], &array[j]);}}}
/* Select Sort algorithm simple description: In the set of numbers to be sorted, select the smallest number to exchange the number of the first position, and then in the remaining number to find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number to compare the selection of the order is not stable, algorithm complexity 0 (n2)- -[n Squared] */void selectsort (int *array, int len) {int I, J, K;for (i=0; i<len; i++) {k = i;/* assigns a value to the token */for (j=i+1; j<len; j+ +) {if (Array[k]>array[j]) {k = j;/* is k always points to the smallest element */}}if (k! = i) {swap (&array[k], &array[i]);}}}
/* Insertion Sorting algorithm Simple description: The insertion method is a more intuitive sorting method, first of all the array of the first two elements of the sequence, then insert the following elements into the appropriate position, the array elements are finished after the order is completed sorting direct insertion is stable. Algorithm time complexity 0 (N2)--[n squared] */void insertsort (int *array, int len) {int I, J, Tmp;for (I=1; i<len; i++) {j = i-1;tmp = Array[i]; /*tmp for the element to be inserted */while (j>=0 && array[j]>tmp)//* from a[i-1] to find a smaller number than a[i, while moving the array elements backwards/{array[j+1] = array[j];j-- ;} ARRAY[J+1] = tmp;/* Insert a slightly smaller number into the array */}}
/* Hill sorting Algorithm simple description: The first is to put the distance K (k>=1) The elements of the sequence, and then reduce the value of K (generally take half of it), and then sort, until k=1 when the sort of hill is not stable */void Shellsort (int *array, int len) {int i,j, tmp, k;k = len/ 2;/* spacing Value */while (k>=1) {for (i=k; i<len; i++) {tmp = ARRAY[I];J = I-k;while (j>=0 && array[j]>tmp) {array [J+k] = array[j];j-= k;} ARRAY[J+K] = tmp;} K = k/2;/* Reduced spacing value */}}
/* Quick Sort Algorithm Simple description: It is an essential improvement on bubble sort, based on the idea that the length of the sequencing sequence can be greatly reduced after a scan. In a bubbling sort, a scan can only ensure that the number of maximum values is moved to the correct position, while the length of the sequence to be sorted may only be reduced by 1, and a quick sort can ensure that the left number of a number (as it is the Datum point bar) is smaller than it, and the number on the right is larger than it. It then uses the same method to manipulate the left and right sides of the number until there is only one element to the left of the datum point. The quick sort can be implemented recursively, or the */void sort (int *array, int min, int len) {int left, right, middle;int itmp;left = Min;right = Len;m can be implemented by using the stack dissolve recursion Iddle = array[(min+len)/2];/* Middle Value */do{while ((array[left]<middle) && left<len)/* * Scan from left is greater than the number of intermediate values */left++; while ((Array[right]>middle) && right>min)/* Scan from the right is greater than the median value */right--;if (left<=right) {/*itmp = array[ Left];array[left] = Array[right];array[right] = Itmp;*/swap (&array[left], &array[right]); left++;right--;}} while (Left<=right), if (min<right) {sort (array, min, right);} if (len>left) {sort (array, left, Len);}} void QuickSort (int *array, int len) {sort (array, 0, len-1);}
The following is to provide a test algorithm program to verify the correctness of the above code:
#define MAX_LENGTH 20void printArray (int *array) {int i;for (i=0; i<max_length; i++) {if (array[i]! = 0) {printf ("%d", arr Ay[i]);}} printf ("\ n");} int main (int argc, char **argv) {int Array[max_length] = {9,2,3,1,10,5,8,7,6,4};p rintf ("The array is:");p Rintarray (array );//bobblesort (array, max_length); The Bubbling sort printf ("After bobble sort array is:");p Rintarray (array);//selectsort (array, max_length); Select Sort printf ("After select Sort array is:");p Rintarray (array);//insertsort (array, max_length); Insert sort printf ("After insert sort array is:");p Rintarray (array); Shellsort (array, max_length); Hill sort printf ("After Shell sort array is:");p Rintarray (array);//quicksort (array,max_length); Quick Sort printf ("After quict sort array is:");p Rintarray (array); return 0;}
Classic Sorting algorithm