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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>
using std::rand;
using std::srand;
using namespace std;
//FUNCTIONS START
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 heapify(int numbers[],int array_size);
int siftDown(int numbers[], int start,int end);
int copy(int a[],int numbers[],int arraysize);
//FUNCTIONS END
ofstream outf("f:Proj1-Output.dat", ios::out);
ifstream inf("f:Proj1-Input.dat", ios::in);
//-------------------MAIN-----------------------------------------------------------
int main()
{
float t1, t2, t3, t4;
long int start,end;
int array_cnt=0;
int results1;
int results2;
int results3;
int results4;
srand(time(0));
int arraysize = 10;
int numbers[arraysize];
int copiedarray[arraysize];
outf<<"SIZE "<<"QUICK "<<"HEAP "<<"BUBBLE "<<"INSERTION "<<endl;
for (int global=1; global<=20; global++)
{
for (int i = 0; i <=arraysize; i++)
{
array_cnt++;
}
outf<<array_cnt<<" ";
copy(copiedarray,numbers,arraysize);
start= clock();
quickSort(copiedarray,arraysize);
end= clock();
outf<<(end-start)/CLOCKS_PER_SEC<<" ";
start= clock();
results2= insertion_sort(numbers,arraysize);
end= clock();
start= clock();
results3= BubbleSort(numbers,arraysize);
end= clock();
start= clock();
results4= heapSort(numbers,arraysize);
end= clock();
}
return 0;
}
//END OF MAIN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//BUBBBLE START
int BubbleSort(int a[], int array_size)
{
int i, j, temp;
for (i = 0; i < (array_size - 1); ++i)
{
for (j = 0; j < array_size - 1 - i; ++j )
{
if (a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
//BUBBLE END
//INSERTION START
int insertion_sort(int a[], int array_size)
{
int i, j, key;
for(j = 1; j < array_size; j++)
{
key = a[j];
for(i = j - 1; (i >= 0) && (a[i] > key); i++)
{
a[i+1] = a[i];
}
a[i+1] = key;
}
}
//INSERTION END
//QUICK START
int quickSort(int a[], int array_size)
{
int i;
q_sort(a, 0, array_size - 1);
}
int q_sort(int a[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = a[left];
while (left < right)
{
while ((a[right] >= pivot) && (left < right))
right--;
if (left != right)
{
a[left] = a[right];
left++;
}
while ((a[left] <= pivot) && (left < right))
left++;
if (left != right)
{
a[right] = a[left];
right--;
}
}
a[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(a, left, pivot-1);
if (right > pivot)
q_sort(a, pivot+1, right);
}
//QUICK END
//HEAP START
int heapify(int a[],int array_size)
{
int start= (array_size- 2) / 2;
for(; start >= 0; start--)
{
siftDown(a, start, array_size-1);
}
return 0;
}
int 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 0;
}
}
int heapSort(int a[], int array_size)
{
int i;
int end;
heapify(a, array_size);
for (end = array_size - 1; end >= 1; end--)
{
swap(a[end], a[0]);
siftDown(a, 0, end - 1);
}
}
//HEAP END
int copy(int a[],int b[],int arraysize)
{
for (int i = 1; i <=arraysize; i++)
{
int a[arraysize];
int b[arraysize];
a[arraysize]= b[arraysize];
}
return a[arraysize];
}
|