Changeset 15072


Ignore:
Timestamp:
05/22/13 02:49:13 (12 years ago)
Author:
Mathieu Morlighem
Message:

CHG: added some comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/shared/Bamg/HeapSort.h

    r14949 r15072  
    44/*Sort a list of size n*/
    55template<class T> inline void  HeapSort(T *c,long n){ /*{{{*/
    6         int l,j,r,i;
     6
     7        /*Intermediaries*/
     8        int i,j,l,r;
    79        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;
    1116        r=n;
     17        c--; //the array must starts at 1 and not 0
     18
     19        /*Sorting algorithm*/
    1220        for(;;){
    1321                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                        }
    1728                }
    18                 else  crit = c[--l];
     29                else{
     30                        crit = c[--l];
     31                }
    1932                j=l;
    2033                for(;;){
    21                         i=j;
    22                         j=2*j;
    23                         if  (j>r) {c[i]=crit;break;}
     34                        i = j;
     35                        j = 2*j;
     36                        if  (j>r){c[i]=crit;break;}
    2437                        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 -> stock this large value in i(<j)
    26                         else{c[i]=crit;break;}            //c[j]  < crit -> stock crit 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)
    2740                }
    2841        }
     
    3245/*Sort a list of size n and returns ordering*/
    3346template<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;
    3550        T    crit;
    3651        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];
    3862        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*/
    4465        for(;;){
    4566                if(l<=1){
Note: See TracChangeset for help on using the changeset viewer.