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
|
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
using namespace std;
int answer;
int answer2;
int size;
void bubbleSort(int[], int);
void selectionSort(int[], int);
int binarySearch(int unsorted[], int size, int value);
int linearSearch(int unsorted[], int numElements, int value);
int value;
int numElements;
int unsorted[] =
{
85, 64, 16, 18, 1, 88, 48, 63, 54, 83, 79, 50, 0, 55,
17, 99, 69, 53, 65, 22, 93, 86, 9, 37, 56, 23, 21, 52,
78, 29, 14, 58, 35, 47, 68, 87, 3, 34, 5, 74, 4, 45,
41, 49, 67, 89, 92, 60, 72, 20, 8, 15, 42, 71, 31, 36,
90, 84,70, 51, 28, 32, 81, 10, 82, 40, 57, 24, 25, 91,
44, 66, 30, 62, 94, 6, 7, 46, 43,38, 75, 11, 39, 80,
98, 27, 12, 76, 96, 2, 77, 19, 26, 59, 33, 73, 13,
61, 95, 97
};
int main()
{
cout <<
"Which algorithm should be used to sort?" <<
"(0: Bubble Sort, 1: Selection Sort)" << endl;
cin >> answer;
if (answer == 0)
{
bubbleSort(int unsorted[], int size);
}
else
{
selectionSort(int unsorted[], int size);
}
cout <<
"Which algorithm should be used to search?" <<
"(0: Linear Search, 1: Binary Search)" << endl;
cin >> answer2;
if (answer2 == 0) // <----- This should be answer2, not answer.
{
answer2 = linearSearch(unsorted, numElements, value);
}
else
{
answer2 = binarySearch(unsorted, size, value);
}
}
// Bubble Sort Fun
void bubbleSort(int unsorted[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (unsorted[count] > unsorted[count - 1]; count++)
{
temp = unsorted[count];
unsorted[count] = unsorted[count + 1];
unsorted[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// Selection Sort Function
void selectionSort(int unsorted[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = unsorted[startScan];
for (int index = startScan + 1; index < size; index++)
{
minValue = unsorted[index];
minIndex = index;
}
unsorted[minIndex] = unsorted[startScan];
unsorted[startScan] = minValue;
}
}
// Linear Search Function
int linearSearch(int unsorted[], int numElements, int value)
{
int index = 0;
int pos = -1;
bool found = false;
while (index < numElements && !found)
{
if (unsorted[index] == value)
{
found = true;
pos = index;
}
index++;
}
return pos;
}
// Binary Search Function
int binarySearch(int unsorted[], int size, int value)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found);
{
middle = (first + last) / 2; // Calculate the middle element
if (unsorted[middle] == value) // If value is the middle element
{
found = true;
position = middle;
}
else if (unsorted[middle] > value) // If value is in lower part
last = middle - 1;
else
first = middle + 1; // If value is in upper part
}
return position;
return 0;
}
|