bubble sort vs selection sort
Sep 19, 2014 at 5:33pm UTC
Why do I get a different order for the different sorts? I pass the exact same array of strings into both of those functions, but they put the array in different orders.
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
void bubbleSort(string array[], int size)
{
string temp;
for (int i = 0; i < size - 1; i++)
{
if (array[i] > array[i + 1])
{
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
return ;
}
void selectionSort(string array[], int size)
{
int index;
string smallest;
for (int i = 0; i < size-1; i++)
{
index = i;
smallest = array[i];
for (int x = 1; x < size; x++)
{
if (array[x] < smallest)
{
smallest = array[x];
index = x;
}
}
array[index] = array[i];
array[i] = smallest;
}
Sep 19, 2014 at 5:38pm UTC
Last edited on Sep 19, 2014 at 5:39pm UTC
Topic archived. No new replies allowed.