假设给定排序码序列为{39,33,13,15,58,41,27,46,23}以单链表为存储结构,请给出对应的直接选择排序算法、直接插入排序算法的C语言实现
时间: 2024-06-13 07:08:48 浏览: 15
以下是直接选择排序算法和直接插入排序算法的C语言实现,针对给定的排序码序列{39,33,13,15,58,41,27,46,23}:
直接选择排序算法:
```c
#include <stdio.h>
void selectSort(int arr[], int len) {
int i, j, minIndex, temp;
for (i = 0; i < len - 1; i++) {
minIndex = i;
for (j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j; }
}
if (minIndex != i) {
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
int main() {
int arr[] = {39, 33, 13, 15, 58, 41, 27, 46, 23};
int len = sizeof(arr) / sizeof(arr[0]);
selectSort(arr, len);
int i;
for (i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
直接插入排序算法:
```c
#include <stdio.h>
void insertSort(int arr[], int len) {
int i, j, temp;
for (i = 1; i < len; i++) {
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
int main() {
int arr[] = {39, 33, 13, 15, 58, 41, 27, 46, 23};
int len = sizeof(arr) / sizeof(arr[0]);
insertSort(arr, len);
int i;
for (i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)