#include <stdio.h>
#define SIZE 10
/* Terry Harvey CISC105 Section TA*/

/* unfinished */
void printArray(int data[], int size);
void swapMaxWithEnd(int data[], int end);
int findIndexOfMax(int unsorted[], int end);
void ssort(int data[], int currentLast);

int main(){
    int max;
    int data[SIZE] = {1,6,9,3,2,0,5,6,4,7};
    printArray(data,SIZE);
    
    printf("\n");
    ssort(data, SIZE - 1);
    printArray(data,SIZE);    
    printf("\n");

    return 0;
}

/*
 * This function swaps values between two indices of array data.
 * parameter maxIndex is the index of the greatest value in data.
 * parameter end is index whose value swaps with greatest value.
 */
void swapMaxWithEnd(int data[], int end){
    int temp;
    int maxIndex = findIndexOfMax(data, end);
    temp = data[maxIndex];
    data[maxIndex] = data[end];
    data[end] = temp;
    return;
}

/* currentLast is end index of unsorted array*/
void ssort(int data[], int currentLast){
    if (currentLast == 0) return;
    swapMaxWithEnd(data, currentLast);
    ssort(data, currentLast - 1); //recursive call
    return;
}

/* finds index of max value between 0 and current end of array */
int findIndexOfMax(int unsorted[], int end){
    int i, 
	//max = unsorted[end];
	maxIndex = end;
    for(i = end; i >= 0; i--){
	if (unsorted[maxIndex] < unsorted[i])
	    maxIndex = i;
    }
    return maxIndex;
}

void printArray(int data[], int size){
    int i;
    for (i = 0; i < size; i++)
	printf("%d ", data[i]);
    return;
}
