我用这选择排序法输入6,7,1,3,5可是排序结果确实1,5,3,6,7请问代码哪里出了问题?
程序代码:
#include <stdio.h> void out(int array_1[], int n); int main (void) { int array_1[5]; int n = sizeof(array_1) / sizeof(int); int i; printf("please input the number of array: "); for ( i = 0; i < n; i++) { scanf("%d",&array_1[i]); } int a, b; for (a = 0; a < n-1; a++){ int pos = a; for (b = a + 1; b < n; b++){ if (array_1[pos]>array_1[b]){ pos = b; } if (pos != a){ int temp = array_1[a]; array_1[a] = array_1[pos]; array_1[pos] = temp; } } } out(array_1, n); return 0; } void out(int array_1[], int n) { int i; for (i = 0; i < n; i++){ printf("%d ", array_1[i]); } printf("\n"); return; }
错误已经找到,第二个if应该在外层循环里,而不是在内层循环
[此贴子已经被作者于2016-2-6 23:51编辑过]