首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择排序不排序

问选择排序不排序
EN

Stack Overflow用户
提问于 2020-01-12 20:25:46
回答 1查看 60关注 0票数 0

在测试排序方法时,我遇到了selection sort。我已经理解了选择排序背后的逻辑,但是我没有从这个程序中得到我希望看到的结果。它看起来一点都不排序。谁能告诉我我哪里搞错了。

代码语言:javascript
复制
#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;
}

我换的地方有问题吗?

EN

回答 1

Stack Overflow用户

发布于 2020-01-12 20:35:48

排序函数中有一个拼写错误。

而不是

代码语言:javascript
复制
int n = 0;

应该有

代码语言:javascript
复制
int n = 10;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59703780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档