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
|
#include<iostream>
#include<iomanip>
#include<cstdlib>
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 main()
{
int arraysize = 11;
int numbers[arraysize];
srand(time(0));
for (int i = 1; i < arraysize; i++)
{
numbers[i] = 1 + rand() % 10;
}
int results1;
int results2;
int results3;
int results4;
cout<<"QUICK:: ";
results1= quickSort(numbers,arraysize);
cout<<endl;
cout<<"INSERTION:: ";
results2= insertion_sort(numbers,arraysize);
cout<<endl;
cout<<"BUBBLE:: ";
results3= BubbleSort(numbers,arraysize);
cout<<endl;
cout<<"HEAP:: ";
results4= heapSort(numbers,arraysize);
cout<<endl;
cin>>results1;
cout<<results1<<endl;
cin>>results2;
cout<<results2<<endl;
cin>>results3;
cout<<results3<<endl;
cin>>results4;
cout<<results4<<endl;
return 0;
}
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;
}
}
}
for(i=0; i<array_size; i++)
cout<<setw(5)<<a[i];
}
int insertion_sort(int a[], int array_size)
{
int i, j, key;
for(j = 1; j < array_size; j++) //Notice starting with 1 (not 0)
{
key = a[j];
for(i = j - 1; (i >= 0) && (a[i] > key); i++) //Move smaller values up one position
{
a[i+1] = a[i];
}
a[i+1] = key; //Insert key into proper position
}
for(i=0; i<array_size; i++)
cout<<setw(5)<<a[i];
}
int quickSort(int a[], int array_size)
{
int i;
q_sort(a, 0, array_size - 1);
for(i=0; i<array_size; i++)
cout<<setw(5)<<a[i];
}
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);
}
int heapSort(int a[], int array_size)
{
int i, temp;
for (i = (array_size / 2)-1; i <= 1; i--)
siftDown(a, i, array_size);
for (i = array_size-1; i <= 1; i--)
{
temp = a[0];
a[0] = a[i];
a[i] = temp;
siftDown(a, 0, i-1);
}
for(i=0; i<array_size; i++)
cout<<setw(5)<<a[i];
}
int siftDown(int a[], int root, int bottom)
{
int done, maxChild, temp;
done = 0;
while ((root*2 <= bottom) && (!done))
{
if (root*2 == bottom)
maxChild = root * 2;
else if (a[root * 2] > a[root * 2 + 1])
maxChild = root * 2;
else
maxChild = root * 2 + 1;
if (a[root] < a[maxChild])
{
temp = a[root];
a[root] = a[maxChild];
a[maxChild] = temp;
root = maxChild;
}
else
done = 1;
}
}
| |