// Sort. cpp: defines the entry point of the console application. <Br/> // </P> <p> # include "stdio. H "<br/> # include" math. H "<br/> # include" tchar. H "<br/> # define max_of_array 10 </P> <p> int itemp = 0; <br/> int itimes = 0; </P> <p> ///////////////////////////////// /// // <br/> // exchange data <br/> void change (int * a, int * B) <br/>{< br/> itemp = * A; <br/> * A = * B; <br/> * B = itemp; <br/> itimes ++; <br/>}</P> <p> //////////////////////////// /// // <br // Print data <br/> void printarray (int * arrays) <br/>{< br/> for (INT I = 0; I <max_of_array; I ++) <br/>{< br/> _ tprintf (_ T ("% d"), * arrays); <br/> arrays ++; <br/>}< br/> _ tprintf (_ T ("/ntimes: % d/N"), itimes ); <br/>}</P> <p> //////////////////////////// /// // sort by insertion <br/> // <br/> void insetsort (int * arrays) <br/>{< br/> int itemp = 0; <br/> for (INT I = 1; I <max_of_array; I ++) <br/>{< br/> itemp = I; <br/> // search for smaller elements than yourself from the I-th element. <br/> // if it is found, the elements and subscripts are exchanged, then look for <br/> for (Int J = I-1; arrays [itemp]> arrays [J] <br/> & J> = 0; j --, itemp --) <br/>{< br/> change (& arrays [itemp], & arrays [J]); <br/>}</P> <p> ////////////////// /// // <br // select sorting <br/> void selectionsort (int * intarray) <br/>{</P> <p> for (INT I = 0; I <max_of_array; I ++) <br/>{< br/> int Imin = I; <br/> for (in T j = I + 1; j <max_of_array; j ++) <br/>{< br/> If (intarray [J] <intarray [Imin]) <br/>{< br/> Imin = J; // find the minimum number <br/>}< br/> if (I! = Imin) <br/>{< br/> change (& intarray [Imin], & intarray [I]); <br/>}< br/> printarray (intarray ); <br/>}</P> <p> /////////////////////// /// // <br/> // bubble method <br/> void bubblesort (int * intarray) <br/>{< br/> bool bisneedroop = false; <br/> for (INT I = 0; I <max_of_array; I ++) <br/>{< br/> bisneedroop = false; <br/> for (Int J = 0; j <MAX_OF_ARRAY-i-1; j ++) <br/>{< br/> If (intarray [J]> Intarray [J + 1]) <br/>{< br/> change (& intarray [J], & intarray [J + 1]); <br/> bisneedroop = true; <br/>}< br/> // if one query result does not require exchange, the description has been sorted <br/> // you can exit directly <br/> If (! Bisneedroop) break; <br/>}< br/> ////////////////////////// //// // <br/> // fast sorting <br /> void quicksort (int * intarray, int arraylen) <br/>{< br/> If (arraylen> 1) <br/>{< br/> int num = 0, I = 0, j = ArrayLen-1, itemp = 0; <br/> while (I! = J) <br/> {<br/> I = 0, j = ArrayLen-1, itemp = 0; <br/> // search for a number greater than the key number from the back to the end, and exchange <br/> for (J; j> num; j --) <br/>{< br/> If (intarray [num] <intarray [J]) <br/>{< br/> change (& intarray [num], & intarray [J]); <br/> num = J; <br/> break; <br/>}< br/> // search for a number smaller than the key number from the past to the next. <br/> for (I; I <num; I ++) <br/>{< br/> If (intarray [num]> intarray [I]) <br/>{< br/> change (& intarray [num], & intarray [I]); <br/> num = I; <br/> break; <br />}< Br/> // The array is divided into two parts (Parts greater than the key number and smaller than the key number) <br/> quicksort (intarray, num); <br/> quicksort (intarray + num + 1, ArrayLen-num-1 ); <br/>}</P> <p> /////////////////////// //////////////////////////////////////// ////////// <br/> // heap sorting <br/> // maintain heap consistency on a single node <br/> void heapkeepmax (int * intarray, int inode, int ilength) <br/>{< br/> int ichildnode = 0; <br/> // The Node keeps moving down, until the length is greater than the total length <br/> for (INT I = inode; 2 * inode + 2 <= ilength; inode = ichildnode) <br/> {<br/> ichildnode = inode * 2 + 1; <br/> // keep ichildnode pointing to a large subnode. <Br/> If (ichildnode + 1 <ilength) & intarray [ichildnode] <intarray [ichildnode + 1]) <br/>{< br/> ichildnode ++; <br/>}< br/> // If the node is smaller than the child node, It is not exchanged. <br/> If (intarray [inode] <intarray [ichildnode]) <br/>{< br/> change (& intarray [inode], & intarray [ichildnode]); <br/>} else break; // If the node is larger than the child node, exit <br/>}</P> <p> // intarray [0... ilength] heap adjustment, ilength decline <br/> void heapadjust (int * intarray, int ilength) <br/>{< br/> for (INT I = (iLength-2) /2; I> = 0; I --) <br/>{< br/> heapkeepmax (intarray, I, ilength ); <br/>}</P> <p> change (& intarray [0], & intarray [iLength-1]); <br/>}</P> <p> // heap sorting <br/> void heapsort (int * intarray, int ilength) <br/>{< br/> for (INT I = ilength; I> 1; I --) <br/>{< br/> heapadjust (intarray, I ); <br/>}</P> <p> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> int intarray [max_of_array] = }; <br/> // int intarray [max_of_array] = {,}; <br/> // int intarray [max_of_array] = {9, 8, ,}; <br/> // int intarray [max_of_array] = {9, 8 }; <br/> // insetsort (intarray); <br/> // bubblesort (intarray); <br/> // quicksort (intarray, max_of_array ); <br/> // selectionsort (intarray); <br/> heapsort (intarray, 10); <br/> printarray (intarray); <br/> return 0; <br/>}< br/>