ok. a few things.
start = 0.
if start == end which == size. this will not happen.
start++ does not work like that. every recursive call makes a new copy of the start variable at zero. to make it work like you want, you need the keyword static in front of it.
also, you are trying to do things to array as if it were a pointer. it is not a pointer. this is bad. It may work here, but I changed it because its late here and it bothered me.
sec
I have fixed it for you. changes
-minindex now actually returns the INDEX of the smallest, not the smallest value.
-swapped the values in the sort. if you have 321 and find 1 and put it at the top without swapping, your array is now 121. then you find 1 and put it at the new top and get 111. this is not useful.
- fixed the pointer abuse
-fixed minindex to iterate correctly (needs size passed in, and size is not the original array size, its the CURRENT size as you peel one off size every recursive call, see?)
-static
-removed smallest, not needed.
etc... I think its right now. If not, its a lot closer :) Take anything I say with a grain of salt, I am tired and am only trying to help -- this is complicated, and I felt you would be better off seeing it fixed than struggling to fix all these issues at once.
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
|
#include <iostream>
#include <algorithm>
using namespace std;
void ascendingSort(int *array);
int minindex(int *a, int size);
const int SIZE = 10;
int main()
{
int digits[SIZE]{ 10, 5, 200, 100, 50, 20, 1, 0, 45, 90 };
int *safe = digits;
ascendingSort(safe);
//cout << minIndex(digits) << endl;
for(int i = 0; i < SIZE; i++)
cout << digits[i] << endl;
//system("pause");
return 0;
}
void ascendingSort(int *array)
{
static int start = 0;
if (start == SIZE)
return;
int temp = array[0];
int index = minindex(array, SIZE-start);
*array = array[index];
array[index] = temp;
start++;
ascendingSort(array+1);
}
int minindex(int *a, int size)
{
int smallest = 0;
for (int i = 0; i < size; i++)
{
if (a[i] < a[smallest])
smallest = i;
}
return smallest;
}
|
this is more code, more complicated code, and significantly slower than even a crude shell sort. You may want to check that one out (its one of my favorite algorithms).