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
|
#include "stdafx.h"
#include <iostream>
#include <ctime>
const int size = 1000;
int insertionSort(int myArray[], int com, int swa)
{
int index, index1, temp;
for (index = 1; index < size; index++)
{
for (index1 = index; index1 >= 1; index1--)
{
if (myArray[index1] < myArray[index1 - 1])
{
temp = myArray[index1]; //stores value in the temp variable
myArray[index1] = myArray[index1 - 1];
myArray[index1 - 1] = temp;
swa++;
}
else
break;
}
com++; //increments the comparison variable
}
std::cout << "The number of swaps performed in the insertion sort were: " << swa << '\n';
std::cout << "The number of comparisons performed in the insertion sort were: " << com << '\n';
return 0;
};
int selectionSort(int myArray[], int com, int swa)
{
int dLocation;
for (int index = 0; index < size; ++index)
{
dLocation = index;
for (int index1 = index + 1; index1 < size; ++index1)
{
if (myArray[index] < myArray[dLocation])
{
dLocation = index1;
com++;
}
}
std::swap(myArray[index], myArray[dLocation]); //performs the swap
swa++;
}
std::cout << "The number of swaps performed in selection sort were: " << swa << '\n';
std::cout << "The number of comparrisons performed in the selection sort were: " << com << '\n';
return 0;
};
int binarySearch(int myArray[], int com, int myKey)
{
int mid = 0;
int min = 1;
int max = size;
if (min <= max)
{
int mid = (min + max) / 2; // finds the mid point of the array
if (myKey == myArray[mid])
{
com++;
std::cout<<"Found The Number in the Array!" <<'\n';
std::cout << "The number of comparisons performed were: " << com << '\n';
}
else if (myKey < myArray[mid])
{
com++;
mid = mid - 1;
return binarySearch(myArray, myKey, com); //min
}
else
{
com++;
mid = mid + 1;
return binarySearch(myArray, myKey, com); //max
}
}
std::cout<<"Sorry, the number was not found in the Array!"<<'\n'; // failed to find key
std::cout << "The number of comparisons performed were: " << com << '\n';
};
/*void binarySearch(int myArray[], int myKey, int com)
{
int min = 1;
int max = size;
do
{
int mid = (min + max) / 2;
if (myKey < myArray[mid])
max = mid - 1;
else if (myKey > myArray[mid])
min = mid + 1;
} while (myKey != myArray[min + max / 2] && min <= max);
com++;
if (myKey == myArray[min + max] / 2)
{
std::cout << "The Number was found!" << '\n';
std::cout << "The Number of comparisons were: " << com << '\n';
}
else
{
std::cout << "Sorry, the number wasn't found!" << '\n';
std::cout << "The number of comparisons were: " << com << '\n';
}
};*/
int main()
{
int numToRun = 0;
int com = 0;
int swa = 0;
std::cout << "How many times would you like to run the algorithms?: " << '\n';
std::cin >> numToRun;
while (numToRun != 0)
{
int mid;
int myKey;
int myArray[size];
srand((unsigned)time(0));
for (int index = 0; index < size; index++)
{
myArray[index] = (rand() % 1000) + 1;
}
insertionSort(myArray, com, swa);
selectionSort(myArray, com, swa);
std::cout << "Please enter the number that you would like to search for: " << '\n';
std::cin >> myKey;
if (myKey == 0)
{
exit(1);
}
else
{
binarySearch(myArray, myKey, com);
}
numToRun--;
}
return 0;
}
|