void selectionSort(int array2[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array2[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array2[index] < minValue)
{
minValue = array2[index];
minIndex = index;
}
}
array2[minIndex] = array2[startScan];
array2[startScan] = minValue;
}
}
int searchList (int array1[], int selection)
{
int index = 0;
int position = -1;
bool found = false;
while (index < 20 && !found)
{
if (array1[index] == selection)
{
found = true;
position = index;
}
index++;
}
return position;
}
int binarySearch (int array1[], int size, int selection)
{
int first =0,
last = size - 1,
middle,
position = -1;
bool found = false;
while(!found && first <= last)
{
middle = (first + last) / 2;
if (array1[middle] == selection)
{
found = true;
position = middle;
}
else if (array1[middle] > selection)
last = middle - 1;
else
first = middle + 1;
}
}
int main()
{
int result;
int selection;
int array1[20];
int array2[20];
fill(array1,array2);
for (int n=0; n < 20; n++)
{
array2[n]=array1[n]; //This might be simply assigning values, not copying them. Double-check.
}
bubbleSort(array1);
selectionSort(array2);
cout << "Enter a number you would like to search for within the array. " << endl;
cin >> selection;
result = searchList(array1,selection);
cout << "The element has been found. It is located in array one at position " << result << endl;
result = binarySearch(array1, selection);
cout << "The element has been found. It is located in array one at position " << result << endl;
cout << "The contents of array 1: " << endl;
for (int n=0; n < 20; n++)
{
cout << array1[n] << endl;
}
cout << "The contents of array 2: " << endl;
for (int n=0; n < 20; n++)
{
cout << array2[n] << endl;
}
return 0;
}