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
|
#include <iostream>
//using namespace std; // <--- Best not to use this. No real need for it with what you have here.
constexpr int MAXSIZE{ 8 }; // <--- Needs to be a constant. Changed.
int BubbleSort(int array[], int low, int high);
void SelectionSort(int array[], int size);
int main()
{
//constexpr int MAXSIZE = 8; // <--- Not needed here with the global variable.
int A[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
int B[MAXSIZE] = { 50, 30, 66, 10, 168, 45, 99, 20 };
SelectionSort(A, 0, MAXSIZE - 1); // <--- Sending 3 parameters, but calls for two.
BubbleSort(B, 0, MAXSIZE - 1);
}
int BubbleSort(int array[], int low, int high)
{
// Next two lines not needed. Values passed in from function call. Those should be used.
low = 10;
high = 168;
int i, pos = low;
for (i = low; i <= high; i++) // <--- Will loop 158 times, but the array has a size of 8. Most of the
{ // loop will be outside of the boundry of the array and "low" should
// start at zero not 10. Even 10 is outside the boundary of tha array.
if (array[i] < array[pos]) pos = i;
}
return pos;
}
// Have not tested the selection sort yet. I do believe that it looks like your last version which did work.
void SelectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
|