查找最小数下标
1 #include<stdio.h> 2 3 int main() 4 { 5 int a[10]; //定义一个数组 6 int n; //参加排序整数的个数,小于等于10 7 int temp; 8 9 scanf_s("%d", &n); 10 11 for (int i = 0; i < n; i++) 12 { 13 scanf_s("%d", &a[i]); 14 } 15 16 for (int i = 0; i < n - 1; i++) 17 { 18 int index = i; //最小值下标初始化 19 for (int j = i + 1; j < n; j++) //寻找最小值下标 20 { 21 if (a[j] < a[index]) 22 { 23 index = j; 24 } 25 } 26 if (index != i) 27 { 28 temp = a[i]; 29 a[i] = a[index]; 30 a[index] = temp; 31 } 32 } 33 34 for (int i = 0; i < n; i++) 35 { 36 printf("%6d", a[i]); 37 } 38 return 0; 39 }