1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>
//using std::rand;
//using std::srand;
using namespace std;
int BubbleSort(int numbers[],int arraysize);
int insertion_sort(int numbers[], int arraysize);
int quickSort(int numbers[], int array_size);
int q_sort(int a[], int left, int right);
int heapSort(int numbers[], int array_size);
//int siftDown(int a[], int root, int bottom);
int heapify(int numbers[],int array_size);
void siftDown(int numbers[], int start, int end);
ofstream outf("e:Proj1-Output.dat",ios::out);
ifstream inf("e:Proj1-Input.dat",ios::in);
int heapify(int a[],int array_size)
{
int start = (array_size / 2) - 1;
for(; start >= 0; start--)
{
siftDown(a, start, array_size-1);
}
return 0;
}
void siftDown(int a[], int start,int end)
{
int root;
int child;
root=start;
while (root * 2 <= end)
{
if (root*2 == end)
child = root * 2;
else if(a[root * 2] > a[root * 2 + 1])
child = root * 2;
else
child = root * 2 + 1;
if(a[root] < a[child])
{
swap(a[root], a[child]);
root = child;
}
else
return;
/**
child= root * 2 + 1;
if ((end= child + 1) && (a[child] < a[child + 1]))
{
child = child + 1 ;
}
if (a[root] < a[child])
{
swap(a[root], a[child]);
root = child;
}
else
{
return;
}
*/
}
}
int heapSort(int a[], int array_size)
{
int end;
heapify(a, array_size);
//while (end > 0)
for (end = array_size - 1; end >= 1; end--)
{
swap(a[end], a[0]);
siftDown(a, 0, end - 1);
}
return 0;
}
int main()
{
int arraysize = 10;
int numbers[10];
for (int i = 0; i < arraysize; i++)
{
numbers[i] = 1 + rand() % 100000;
}
cout<<"HEAP:: ";
heapSort(numbers,arraysize);
cout<<endl;
return 0;
}
|