Ice Sheet System Model  4.18
Code documentation
Functions
HeapSort.h File Reference

Go to the source code of this file.

Functions

template<class T >
void HeapSort (T *c, long n)
 
template<class T >
void HeapSort (int **porder, T *c, int n)
 

Function Documentation

◆ HeapSort() [1/2]

template<class T >
void HeapSort ( T *  c,
long  n 
)
inline

Definition at line 5 of file HeapSort.h.

5  { /*{{{*/
6 
7  /*Intermediaries*/
8  int i,j,l,r;
9  T crit;
10 
11  /*return if size <=1*/
12  if(n<=1) return;
13 
14  /*Initialize variables*/
15  l=n/2+1;
16  r=n;
17  c--; //the array must starts at 1 and not 0
18 
19  /*Sorting algorithm*/
20  for(;;){
21  if(l<=1){
22  crit = c[r];
23  c[r--] = c[1];
24  if(r==1){
25  c[1]=crit;
26  return;
27  }
28  }
29  else{
30  crit = c[--l];
31  }
32  j=l;
33  for(;;){
34  i = j;
35  j = 2*j;
36  if (j>r){c[i]=crit;break;}
37  if ((j<r) && (c[j] < c[j+1])) j++;//c[j+1]> c[j] -> take j+1 instead of j (larger value)
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)
40  }
41  }
42 }

◆ HeapSort() [2/2]

template<class T >
void HeapSort ( int **  porder,
T *  c,
int  n 
)
inline

Definition at line 46 of file HeapSort.h.

46  { /*{{{*/
47 
48  /*Intermediaries*/
49  int i,j,l,r;
50  T crit;
51  int pos;
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];
62  for(i=0;i<n;i++) order[i]=i;
63  order--;
64 
65  /*Sorting algorithm*/
66  for(;;){
67  if(l<=1){
68  crit =c[r]; pos=order[r];
69  c[r--]=c[1]; order[r+1]=order[1];
70  if (r==1){
71  c[1]=crit; order[1]=pos;
72  order++;
73  *porder=order;
74  return;
75  }
76  }
77  else {crit=c[--l]; pos=order[l];}
78  j=l;
79  for(;;){
80  i=j;
81  j=2*j;
82  if (j>r) {c[i]=crit;order[i]=pos;break;}
83  if ((j<r) && (c[j] < c[j+1]))j++;
84  if (crit < c[j]){
85  c[i]=c[j];
86  order[i]=order[j];
87  }
88  else{
89  c[i]=crit;order[i]=pos;
90  break;
91  }
92  }
93  }
94 
95  /*Make cppcheck happy*/
96  *porder=order;
97 }/*}}}*/