Hsort.h
/************************************************************************* > File name:heapsort.h > Author:zhoulin > Mail: [email protected] > Created time:wed Apr 13 12:52:15 2016 ****************** ******************************************************/#ifndef _heapsort_h#define_heapsort_htypedefstruct_heap{int*Array; intHeapSize;//Heap Size intLen//Array Len}heap;//swap A and BvoidSwapint*a,int*b);voidAdjust (heap *p,inti);voidHeap_sort (Heap *p);//-------------Split--------------intParttion (int*arr,intLeftintRight );voidQuit_sort (int*arr,intStartintend);#endif
HSORT.C:
/************************************************************************* > File name:heapsort.c > Author:zhoulin > Mail: [email protected] > Created time:wed Apr 13 12:55:18 2016 ****************** ******************************************************/#include"hsort.h"#include<stdio.h>voidSwapint*a,int*b) { *a = *a^*b; *b = *a^*b; *a = *a^*b;}voidAdjust (heap *p,inti) { if(p = =NULL)return; int*array = p->Array; intlargest; intleft = (i+1) <<1-1; intright = (i+1) <<1; intCurmax = p->heapsize; if(Left < Curmax && Array[left] >Array[i]) {Largest=Left ; }Else{Largest=i; } if(Right < Curmax && Array[right] >Array[largest]) {Largest=Right ; } if(Largest! =i) {swap (&array[largest],&Array[i]); Adjust (p,largest); }}voidHeap_sort (Heap *p) { int*array = p->Array; intI,len = p->Len; for(I= (len>>1)-1; i>=0; i--) {fprintf (stdout,"Build%d\n", i); Adjust (p,i); } while(P->heapsize >0) {Swap (&array[0],&array[p->heapsize]); P->heapsize--; Adjust (p,0); }}intParttion (int*arr,intLeftintRight ) { intCur =Arr[left]; while(Left < right && Arr[right] >=cur) { Right--; } if(Left <Right ) {Arr[left++] =Arr[right]; } while(Left < right && Arr[left] <=cur) { Left++; } if(Left <Right ) {Arr[right--] =Arr[left]; } Arr[left]=cur; returnLeft ;}voidQuit_sort (int*arr,intStartintend) { intPOS; if(Start <=end) {POS=parttion (arr,start,end); Quit_sort (arr,0, pos-1); Quit_sort (Arr,pos+1, end); }}intMainvoid) {heap p; inti; intarr[9]={ -,1, -, About, the, $, the, +, the}; /*P.array = (int *) &arr; */ intLen =9; for(i =0;i< len;i++) {fprintf (stdout,"arr[%d] =%d\n", I,arr[i]); } fprintf (stdout,"-----------------------------------------\ n"); Quit_sort (arr,0, len-1); /*P.len = len; P.heapsize = p.len-1; Heap_sort (&P); */ for(i =0;i< len;i++) {fprintf (stdout,"arr[%d] =%d\n", I,arr[i]); } return 0;}
Operation Result:
[Email protected]:~/code/cwork/demo:./Hsort arr[0] = -arr[1] =1arr[2] = -arr[3] = Aboutarr[4] = thearr[5] = $arr[6] = thearr[7] = +arr[8] = the-----------------------------------------arr[0] =1arr[1] = +arr[2] = thearr[3] = $arr[4] = Aboutarr[5] = thearr[6] = thearr[7] = -arr[8] = -
Quick Sort/Heap sort