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
|
//bubbleSorting 4 variations and selectionSort Comparson //with time
#include <iostream>
#include <ctime>
#include <cstdlib>
void basicBubble(int* arr,int size);
void OPs(int* arr,int size);
void flagBreakBubble(int * arr, int size);
void indexBubble(int* arr, int size);
void selectionSort (int ar[], int size);
using namespace std;
int main()
{
srand(time(NULL));
int size = 12000; ////////////// size ///
cout << "elements = " << size << "\n\n";
int *ar1= new int[size];
int *ar2= new int[size];
int *ar3= new int[size];
int *ar4= new int[size];
int *ar5= new int[size];
clock_t t0, t1, t2, t3, t4, t5, t6;
t0 = clock();
for (int i = 0, n; i < size; i++)
{
n = rand()% 25000;
ar1[i] = n;
ar2[i] = n;
ar3[i] = n;
ar4[i] = n;
ar5[i] = n;
}
t1 = clock();
basicBubble(ar1, size);
t2 = clock();
OPs(ar2, size);
t3 = clock();
flagBreakBubble(ar3, size);
t4 = clock();
indexBubble(ar4, size);
t5 = clock();
selectionSort(ar5, size);
t6 = clock();
cout << "\nTime Test:\n"
<< "settingTime " << double(t1-t0) / CLOCKS_PER_SEC << " sec\n"
<< "basicBubble " << double(t2-t1) / CLOCKS_PER_SEC << " sec\n"
<< "OPs " << double(t3-t2) / CLOCKS_PER_SEC << " sec\n"
<< "flagBreakBubble " << double(t4-t3) / CLOCKS_PER_SEC << " sec\n"
<< "indexBubble " << double(t5-t4) / CLOCKS_PER_SEC << " sec\n"
<< "selectionSort " << double(t6-t5) / CLOCKS_PER_SEC << " sec\n";
delete[] ar1;
delete[] ar2;
delete[] ar3;
delete[] ar4;
delete[] ar5;
return 0;
}
void basicBubble(int* arr,int size) //conventional
{
int comparisons(0),swaps(0);
for( int i = 0; i < size; i++ )
{
for(int j=0; j<size; j++) ///if j<size-1; comparison= comparison-size;
{
comparisons++;
if(arr[j] < arr[i])
{
std::swap(arr[j],arr[i]);
swaps++;
}
}
}
cout << "basicBubble: Comparisons: " << comparisons << ", Swaps: " << swaps << "\n";
return;
}
void OPs(int* arr,int size) //as OPs
{
int flag=0,comparisons(0),swaps(0);
while (flag < size)
{
for (int i=1; i < size; i++)
{
comparisons++;
if (arr[i-1] > arr[i])
{
std::swap(arr[i-1],arr[i]);
swaps++;
}
}
flag++;
}
cout << "OPs: Comparisons: " << comparisons << ", Swaps: " << swaps << "\n";
return;
}
void flagBreakBubble(int * arr, int size) //PCrumley48 (1)
{
int comparisons(0),swaps(0);
bool swapped = true;
while (swapped)
{
swapped = false;
for (int i = 0; i < size-1;i++)
{
comparisons++;
if (arr[i+1] < arr[i])
{
std::swap(arr[i],arr[i+1]);
swaps++;
swapped = true;
}
}
}
cout << "flagbreakbubble: Comparisons: " << comparisons << ", Swaps: " << swaps << "\n";
return;
}
void indexBubble(int* arr, int size) //PCrumley48 (2)
{
int comparisons(0),swaps(0),swapindex(size),index(size);
bool swapped = true;
while (swapped)
{
swapped = false;
swapindex = index;
for (int i = 0; i < swapindex-1;i++)
{
comparisons++;
if (arr[i+1] < arr[i])
{
std::swap(arr[i],arr[i+1]);
swaps++;
swapped = true;
index = i+1;
}
}
}
cout << "indexBubble: Comparisons: " << comparisons << ", Swaps: " << swaps << "\n";
return;
}
void selectionSort (int ar[], int size) /// selectionSort()
{
int comparisons= 0, swaps= 0;
for ( int i = 0; i < size; i++ )
{
int indexPos = i;
for(int j=i+1; j<size; j++)
{
comparisons++;
if (ar[j] < ar[indexPos])
indexPos = j;
}
if(ar[indexPos] != ar[i])
{
swaps++;
std::swap(ar[indexPos] , ar[i]);
}
}
cout << "selectionSort: Comparisons: " << comparisons << ", Swaps: " << swaps << "\n";
return;
}
|