C语言对a数组a[0]~a[n-1] 中存储的n个整数从小到大排序。排序算法是:第一趟通过比较将n个整数中的最小值放在a[0]中,最大值放在a[n-1] 中;第二趟通过比较将n个整数中的次小值放在a[1]中,次大值放在a[n-2] 中;……,以此类推,直到待排序序列为递增序列。
时间: 2024-03-18 17:40:00 浏览: 228
这是选择排序的算法,可以用以下代码实现:
```c
void selectionSort(int a[], int n) {
int i, j, minIndex, maxIndex, temp;
for (i = 0; i < n / 2; i++) {
minIndex = i;
maxIndex = i;
for (j = i + 1; j < n - i; j++) {
if (a[j] < a[minIndex]) {
minIndex = j;
}
if (a[j] > a[maxIndex]) {
maxIndex = j;
}
}
temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
if (maxIndex == i) {
maxIndex = minIndex;
}
temp = a[n - i - 1];
a[n - i - 1] = a[maxIndex];
a[maxIndex] = temp;
}
}
```
这个算法的时间复杂度为O(n^2),虽然不是最优的排序算法,但在某些情况下可能是最好的选择。
相关问题
)对a数组a[0]~a[n-1]中存储的n个整数从小到大排序。排序算法是:第一趟通过比较将n个整数中的最小值放在a[0]中,最大值放在a[n-1]中;第二趟通过比较将n个整数中的次小值放在a[1]中,C语言
以下是C语言实现对a数组a~a[n-1]中存储的n个整数从小到大排序的代码,排序算法是:第一趟通过比较将n个整数中的最小值放在a中,最大值放在a[n-1]中;第二趟通过比较将n个整数中的次小值放在a中:
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void select(int a[], int begin, int end) {
int i;
// 初始化时最小值位置为起始位置
int minPos = begin;
// 初始化时最小值为待排序数组的第一个元素
int min = a[begin];
// 寻找最小值
for(i = begin; i <= end; i++) {
if(a[i] < min) {
minPos = i;
min = a[i];
}
}
// 将最小值元素与待排序数组头元素进行交换
swap(&a[begin], &a[minPos]);
}
void sort(int a[], int n) {
int i;
for(i = 0; i < n; i++) {
select(a, i, n - 1);
}
}
int main() {
int a[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
int n = sizeof(a) / sizeof(int);
sort(a, n);
int i;
for(i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}
```
用c语言编写:对a数组a[0]~a[n-1] 中存储的n个整数从小到大排序。排序算法是:第一趟通过比较将n个整数中的最小值放在a[0]中,最大值放在a[n-1] 中;第二趟通过比较将n个整数中的次小值放在a[1]中,次大值放在a[n-2] 中;……,以此类推,直到待排序序列为递增序列。
这是选择排序的算法,可以用以下代码实现:
```c
void selectionSort(int a[], int n) {
int i, j, minIndex, maxIndex, temp;
for (i = 0; i < n / 2; i++) {
minIndex = i;
maxIndex = i;
for (j = i + 1; j < n - i; j++) {
if (a[j] < a[minIndex]) {
minIndex = j;
}
if (a[j] > a[maxIndex]) {
maxIndex = j;
}
}
temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
if (maxIndex == i) {
maxIndex = minIndex;
}
temp = a[n - i - 1];
a[n - i - 1] = a[maxIndex];
a[maxIndex] = temp;
}
}
```
这个算法的时间复杂度为O(n^2),虽然不是最优的排序算法,但在某些情况下可能是最好的选择。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)