为什么我写的这个选择排序程序不起作用
它不对数组进行排序。我究竟做错了什么?
//This program uses selection sort algorithm to sort an array
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
void main() {
int arr[10], i, j, small, pos, a;
printf("SELECTION SORT\n");
srand(time(NULL));
i = 0;
while (i < 10) {
arr[i] = rand() % 100;
printf("%d ", arr[i]);
i++;
}
j = 0;
while (j < 10){
i = j;
small = arr[j];
while (i < 10) {
if (arr[i] < small) {
small = arr[i];
pos = i;
}
i++;
}
a = arr[pos];
arr[pos] = arr[j];
arr[j] = a;
j++;
}
printf("\nThe array has been sorted\n");
i = -1;
while (++i < 10)
printf("%d ", arr[i]);
getch();
}
找到最小元素的算法部分有效 外循环是我认为我犯了错误的地方,但我无法弄清楚什么
一点建议将不胜感激
谢谢