#include <stdio.h>
#define SIZE 5
/*
 * Program to demonstrate selection sort
 * find max
 * swap max with end
 * decrease size of array
 *
 * Terry Harvey CISC 105 section 98 TA Janet
 */
void printArray(int input[], int size);
void selectionSort(int input[], int size);
int findMaxIndex(int input[], int size);

int main(){

    int data[SIZE] = {30,8,7,1,2};//,5,400,9,0,116};
    printArray(data, SIZE);
    selectionSort(data, SIZE);
    printArray(data, SIZE);
    return 0;
}

int findMaxIndex(int input[], int size){
    int i;
    int maxIndex = 0;
    for(i = 0; i < size; i++)
	if (input[i] > input[maxIndex])
	    maxIndex = i;
    return maxIndex;
}

void selectionSort(int input[], int size){
    
    if (size == 1) return;

    int maxIndex = findMaxIndex(input, size);
    printf("found max: %d    ", input[maxIndex]);
    printArray(input, size);

    //perform swap with last element
    int temp = input[size - 1];
    input[size - 1] = input[maxIndex];
    input[maxIndex] = temp;

    selectionSort(input, size-1);
    return;
}

void printArray(int input[], int size){
    int i;

    for(i = 0; i < size; i++){
        printf("%d ", input[i]);
    }
    printf("\n");

    return;
}
