c语言选择排序线程程序实现方法
时间: 2023-12-14 10:32:07 浏览: 39
以下是一个使用线程实现选择排序的C语言程序的示例:
```c
#include <stdio.h>
#include <pthread.h>
#define SIZE 5
int arr[SIZE] = {64, 25, 12, 22, 11};
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void* selectionSort(void* arg) {
int i, j, min_idx;
for (i = 0; i < SIZE-1; i++) {
min_idx = i;
for (j = i+1; j < SIZE; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
swap(&arr[min_idx], &arr[i]);
}
pthread_exit(NULL);
}
int main() {
pthread_t tid;
pthread_create(&tid, NULL, selectionSort, NULL);
pthread_join(tid, NULL);
printf("Sorted array: ");
for (int i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
该程序使用选择排序算法对数组进行排序。它创建了一个线程来执行排序算法,并在主线程中等待该线程完成。排序完成后,主线程输出排序后的数组。
编译该程序的命令是:`gcc -pthread filename.c`,其中`filename.c`是你保存程序的文件名。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)
![](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)