dimanche 12 janvier 2020

Selection sort not sorting

just testing out sorting methods and I've come across selection sort. I've understood the logic behind the selection sort but I don't get the desired result i wish to see from this program. It doesn't seem to sort at all. Could someone tell me where i went wrong.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int StudentCreation(int StudentRecordArray[10]){
    for(int i = 0; i < 10; i++){
        StudentRecordArray[i] = rand() % 100;       //limiting the marks range from 0 - 100
    }
}

int SelectionSort(int SelectionSortarray[]) {
    int n = 0;
    int tmp = 0;
    for(int j = 0; j < 10-1; j++){
        int TempMinimum = j;

        for(int i = j+1; i < n; i++)
            if(SelectionSortarray[i] < SelectionSortarray[TempMinimum])
                TempMinimum = i;

        if(TempMinimum != j){
            tmp = SelectionSortarray[j];
            SelectionSortarray[j] = SelectionSortarray[TempMinimum];
            SelectionSortarray[TempMinimum] = tmp;
        }
    }

    for (int f = 0; f < 10; f++){
        printf("Student %d - %d\n", f+1, SelectionSortarray[f]);
    }
}

int main() {
    int StudentRecord[10];
    int MenuChoice;

    srand(time(NULL));      //random number seed generator

    StudentCreation(StudentRecord);

    printf("the unsorted list:\n");
    for (int f = 0; f < 10; f++){
        printf("Student %d - %d\n", f+1, StudentRecord[f]);
    }

    printf("\nthe sorted list:\n\n");

    SelectionSort(StudentRecord);

    return 0;
}

Is it something wrong with where i have swapped?




Aucun commentaire:

Enregistrer un commentaire