gpt4 book ai didi

c - 选择排序不排序

转载 作者:行者123 更新时间:2023-11-30 14:32:42 25 4
gpt4 key购买 nike

刚刚测试排序方法,我遇到了选择排序。我已经理解了选择排序背后的逻辑,但我没有从这个程序中得到我希望看到的预期结果。看起来根本就没有排序。有人可以告诉我哪里出了问题吗?

#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;
}

我交换的地方有问题吗?

最佳答案

排序功能中存在拼写错误。

而不是

int n = 0;

应该有

int n = 10;

关于c - 选择排序不排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com