Changeset 15072
- Timestamp:
- 05/22/13 02:49:13 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/shared/Bamg/HeapSort.h
r14949 r15072 4 4 /*Sort a list of size n*/ 5 5 template<class T> inline void HeapSort(T *c,long n){ /*{{{*/ 6 int l,j,r,i; 6 7 /*Intermediaries*/ 8 int i,j,l,r; 7 9 T crit; 8 c--; //the array must starts at 1 and not 0 9 if(n<=1) return; //return if size <=1 10 l=n/2+1; //initialize l and r 10 11 /*return if size <=1*/ 12 if(n<=1) return; 13 14 /*Initialize variables*/ 15 l=n/2+1; 11 16 r=n; 17 c--; //the array must starts at 1 and not 0 18 19 /*Sorting algorithm*/ 12 20 for(;;){ 13 21 if(l<=1){ 14 crit =c[r]; 15 c[r--]=c[1]; 16 if (r==1){c[1]=crit; return;} 22 crit = c[r]; 23 c[r--] = c[1]; 24 if(r==1){ 25 c[1]=crit; 26 return; 27 } 17 28 } 18 else crit = c[--l]; 29 else{ 30 crit = c[--l]; 31 } 19 32 j=l; 20 33 for(;;){ 21 i =j;22 j =2*j;23 if (j>r) 34 i = j; 35 j = 2*j; 36 if (j>r){c[i]=crit;break;} 24 37 if ((j<r) && (c[j] < c[j+1])) j++;//c[j+1]> c[j] -> take j+1 instead of j (larger value) 25 if (crit < c[j]) c[i]=c[j]; //c[j] > crit -> stockthis large value in i(<j)26 else{c[i]=crit;break;} //c[j] < crit -> stockcrit in i (<j)38 if (crit < c[j]) c[i]=c[j]; //c[j] > crit -> put this large value in i(<j) 39 else{c[i]=crit;break;} //c[j] < crit -> put crit in i (<j) 27 40 } 28 41 } … … 32 45 /*Sort a list of size n and returns ordering*/ 33 46 template<class T> inline void HeapSort(int** porder,T* c,int n){ /*{{{*/ 34 int l,j,r,i; 47 48 /*Intermediaries*/ 49 int i,j,l,r; 35 50 T crit; 36 51 int pos; 37 int* order = new int[n]; 52 int* order = NULL; 53 54 /*return if size <=1*/ 55 if(n<=1) return; 56 57 /*Initialize variables*/ 58 l=n/2+1; 59 r=n; 60 c--; //the array must starts at 1 and not 0 61 order = new int[n]; 38 62 for(i=0;i<n;i++) order[i]=i+1; 39 c--; //the array must starts at 1 and not 0 40 order--; 41 if(n<=1) return; //return if size <=1 42 l=n/2+1; //initialize l and r 43 r=n; 63 64 /*Sorting algorithm*/ 44 65 for(;;){ 45 66 if(l<=1){
Note:
See TracChangeset
for help on using the changeset viewer.