用指针方法将n个整数按升序排列(选择排序)
时间: 2023-12-25 17:30:33 浏览: 146
排序链表.md
以下是使用指针方法进行选择排序的示例代码:
```c
#include <stdio.h>
void sort(int *arr, int n) {
int i, j, min_idx, temp;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++) {
if (*(arr+j) < *(arr+min_idx)) {
min_idx = j;
}
}
temp = *(arr+i);
*(arr+i) = *(arr+min_idx);
*(arr+min_idx) = temp;
}
}
int main() {
int number, i;
printf("请输入要排序的整数个数:");
scanf("%d", &number);
int data[number];
int *pstr[number];
for (i = 0; i < number; i++) {
pstr[i] = &data[i];
scanf("%d", pstr[i]);
}
sort(pstr, number);
printf("排序后的结果为:");
for (i = 0; i < number; i++) {
printf("%d ", *pstr[i]);
}
printf("\n");
return 0;
}
```
阅读全文