二维数组选择排序c语言
时间: 2023-12-02 21:41:46 浏览: 91
以下是C语言中二维数组选择排序的实现:
```c
#include <stdio.h>
void selectionSort(int arr[][3], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j][0] < arr[min_idx][0])
min_idx = j;
int temp[3];
for (int k = 0; k < 3; k++) {
temp[k] = arr[min_idx][k];
arr[min_idx][k] = arr[i][k];
arr[i][k] = temp[k];
}
}
}
int main() {
int arr[][3] = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i<n; i++) {
for (int j=0; j<3; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
return 0;
}
```
阅读全文