dsalglib  1.0.0
dsalglib is a ready to use data structures and algorithms library written in C++ . Object Oriented Template implementations are written.
heapsort.h
Go to the documentation of this file.
1 //
2 // Created by moghya_s on 12/26/2016.
3 //
4 
5 #ifndef DSALGLIB_HEAPSORT_H
6 #define DSALGLIB_HEAPSORT_H
7 
8 #include "array.h"
9 namespace dsa
10 {
11 
12  template<typename type>
13  void _reheapup(array<type> objs,long long int index)
14  {
15  if(index>0)
16  {
17  long long int parent=(index-1)/2;
18  if(objs[parent]<objs[index])
19  {
20  swapit(objs[parent],objs[index]);
21  _reheapup(objs,parent);
22  }
23  }
24  return ;
25  }
26 
27 
28  template<typename type>
29  void _reheapdown(array<type> objs,long long index,long long int last)
30  {
31 
32  if(index>=0&&index<last)
33  {
34  long long int lc=2*index+1,rc=2*index+2,large;
35  if(lc<last)
36  {
37  large=lc;
38  if(rc<last)
39  {
40  if(objs[lc]<objs[rc])
41  large=rc;
42  }
43  if(objs[index]<objs[large])
44  {
45  swapit(objs[index],objs[large]);
46  _reheapdown(objs,large,last);
47  }
48  }
49  }
50  return ;
51  }
52 
53  template<typename type>
55  {
56  long long int i,last = arr.size(),done;
57  for(i=0;i<last;i++)
58  _reheapup(arr,i);
59 
60  done = last;
61  while(done>0)
62  {
63  swapit(arr[0],arr[done-1]);
64  done--;
65  _reheapdown(arr,0,done);
66  }
67 
68  return ;
69 
70  }
71 
72 }
73 #endif //DSALGLIB_HEAPSORT_H
void swapit(T &x, T &y)
Definition: alginc.h:17
Definition: alginc.h:12
void _reheapup(array< type > objs, long long int index)
Definition: heapsort.h:13
void _reheapdown(array< type > objs, long long index, long long int last)
Definition: heapsort.h:29
long long int size()
Definition: array.h:98
void heapsort(array< type > arr)
Definition: heapsort.h:54