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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
#include <iostream>
#include <ctime>
#include <chrono>
#include <stdio.h>
using namespace std;
void swapping(int& a, int& b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
void display(int* array, int size) {
for (int i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
void bubbleSort(int* array, int size) { //Bubble Sort
for (int i = 0; i < size; i++) {
int swaps = 0; //flag to detect any swap is there or not
for (int j = 0; j < size - i - 1; j++) {
if (array[j] > array[j + 1]) { //when the current item is bigger than next
swapping(array[j], array[j + 1]);
swaps = 1; //set swap flag
}
}
if (!swaps)
break; // No swap in this pass, so array is sorted
}
}
void selectionSort(int* array, int size) { //Selection Sort
int i, j, imin;
for (i = 0; i < size - 1; i++) {
imin = i; //get index of minimum data
for (j = i + 1; j < size; j++)
if (array[j] < array[imin])
imin = j;
//placing in correct position
swap(array[i], array[imin]);
}
}
void sleep(float seconds) { //Sleep to pause program
clock_t startClock = clock();
float secondsAhead = seconds * CLOCKS_PER_SEC;
// do nothing until the elapsed time has passed.
while (clock() < startClock + secondsAhead);
return;
}
int search(int arr[], int n, int x) //Linear Search
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
int binarySearch(int arr[], int l, int r, int x) // Binary Search
{
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
int main()
{
using chrono::high_resolution_clock;
using chrono::duration_cast;
using chrono::duration;
using chrono::milliseconds;
srand(static_cast<unsigned int>(time(nullptr)));
const size_t n { 100000 };
int arr1[n]{};
int arr2[n];
// RANDOM ARRAY PROCESS 1
// ========================================================================
cout << "Running Random Array in 10 seconds:\n";
sleep(10);
for (size_t i = 0; i < n; ++i) {
arr1[i] = rand() % 100 + 1;
cout << arr1[i] << '\t';
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// RANDOM ARRAY PROCCESS 2
//=========================================================================
copy(begin(arr1), end(arr1), begin(arr2)); // Copies Array to Array 2 for execution count
cout << "\n \n In 10 seconds array will repeat counting execution time";
sleep(10);
auto t1 = high_resolution_clock::now();
for (auto e : arr2)
cout << e << '\t';
cout << '\t';
auto t2 = high_resolution_clock::now();
/* Getting number of milliseconds as an integer. */
auto ms_int = duration_cast<milliseconds>(t2 - t1);
/* Getting number of milliseconds as a double. */
duration<double, std::milli> ms_double = t2 - t1;
cout << "\nTime to complete array:";
cout << ms_int.count() << "ms\n";
cout << ms_double.count() << "ms";
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Bubble Sort PROCESS 3
//==================================================================
cout << "\n \n Running bubblesort() algorithm in 10 seconds \n";
sleep(10);
auto t1 = high_resolution_clock::now();
bubbleSort(arr1, n);
auto t2 = high_resolution_clock::now();
/* Getting number of milliseconds as an integer. */
auto ms_int = duration_cast<milliseconds>(t2 - t1);
/* Getting number of milliseconds as a double. */
duration<double, std::milli> ms_double = t2 - t1;
cout << "Array after bubble sort:";
display(arr1, n);
cout << "\nTime to complete bubble sort:";
cout << ms_int.count() << "ms\n";
cout << ms_double.count() << "ms";
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Selection Sort PROCESS 4
//===================================================================
cout << "\n \n Running selectionsort() algorithm in 10 seconds \n";
sleep(10);
auto t1 = high_resolution_clock::now();
selectionSort(arr1, n);
auto t2 = high_resolution_clock::now();
/* Getting number of milliseconds as an integer. */
auto ms_int = duration_cast<milliseconds>(t2 - t1);
/* Getting number of milliseconds as a double. */
duration<double, std::milli> ms_double = t2 - t1;
cout << "Array after selection sort:";
display(arr1, n);
cout << "\nTime to complete selection sort:";
cout << ms_int.count() << "ms\n";
cout << ms_double.count() << "ms";
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Linear Sort PROCESS 5
// ===============================================================
cout << "\n Running a linear search on 250, 15000, 29000 in 10 seconds \n";
sleep(10);
auto t1 = high_resolution_clock::now();
int result = search(arr1, n, 250); // Search for 250
(result == -1)
? cout << "250 is not present in array"
: cout << "250 is present at index " << result;
int result = search(arr1, n, 15000); // Search for 15000
(result == -1)
? cout << "15000 is not present in array"
: cout << "15000 is present at index " << result;
int result = search(arr1, n, 29000); // Search for 29000
(result == -1)
? cout << "29000 is not present in array"
: cout << "29000 is present at index " << result;
auto t2 = high_resolution_clock::now();
/* Getting number of milliseconds as an integer. */
auto ms_int = duration_cast<milliseconds>(t2 - t1);
/* Getting number of milliseconds as a double. */
duration<double, std::milli> ms_double = t2 - t1;
cout << "\nTime to complete linear search:";
cout << ms_int.count() << "ms\n";
cout << ms_double.count() << "ms";
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Binary Sort PROCESS 6
// ===============================================================
cout << "\n Running a binary search on 250, 15000, 29000 in 10 seconds \n";
sleep(10);
auto t1 = high_resolution_clock::now();
int result = binarySearch(arr1, 0, n - 1, 250);
(result == -1) ? cout << "250 is not present in array"
: cout << "250 is present at index " << result;
int result = binarySearch(arr1, 0, n - 1, 15000);
(result == -1) ? cout << "15000 is not present in array"
: cout << "15000 is present at index " << result;
int result = binarySearch(arr1, 0, n - 1, 29000);
(result == -1) ? cout << "29000 is not present in array"
: cout << "29000 is present at index " << result;
auto t2 = high_resolution_clock::now();
/* Getting number of milliseconds as an integer. */
auto ms_int = duration_cast<milliseconds>(t2 - t1);
/* Getting number of milliseconds as a double. */
duration<double, std::milli> ms_double = t2 - t1;
cout << "\nTime to complete binary search:";
cout << ms_int.count() << "ms\n";
cout << ms_double.count() << "ms";
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cout << "End of Program" << endl;
cin.get();
return 0;
}
|