自己写出一个qsort函数
时间: 2024-05-14 08:13:53 浏览: 11
很高兴能回答您的问题!
以下是一个简单的 qsort 函数的示例代码:
```c
void qsort(int arr[], int left, int right)
{
int i, j, pivot, temp;
if (left < right) {
pivot = left;
i = left;
j = right;
while (i < j) {
while (arr[i] <= arr[pivot] && i < right)
i++;
while (arr[j] > arr[pivot])
j--;
if (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = temp;
qsort(arr, left, j - 1);
qsort(arr, j + 1, right);
}
}
```
希望这可以帮助您! 如果您有任何其他问题,都可以随时问我。
相关问题
帮我用sort写一个对字典键排序的函数
好的,我可以回答这个问题。以下是一个使用sort函数对字典键进行排序的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_KEY_LEN 100
#define MAX_VALUE_LEN 100
typedef struct {
char key[MAX_KEY_LEN];
char value[MAX_VALUE_LEN];
} DictEntry;
int compare_keys(const void *a, const void *b) {
const DictEntry *entry_a = (const DictEntry *)a;
const DictEntry *entry_b = (const DictEntry *)b;
return strcmp(entry_a->key, entry_b->key);
}
void sort_dict_keys(DictEntry *entries, int num_entries) {
qsort(entries, num_entries, sizeof(DictEntry), compare_keys);
}
int main() {
DictEntry entries[] = {
{"apple", "red"},
{"banana", "yellow"},
{"cherry", "red"},
{"date", "brown"},
{"elderberry", "purple"}
};
int num_entries = sizeof(entries) / sizeof(DictEntry);
sort_dict_keys(entries, num_entries);
for (int i = ; i < num_entries; i++) {
printf("%s: %s\n", entries[i].key, entries[i].value);
}
return ;
}
```
这个程序定义了一个DictEntry结构体,包含一个键和一个值。sort_dict_keys函数使用qsort函数对字典条目按键进行排序。compare_keys函数用于比较两个字典条目的键。在main函数中,我们创建了一个包含5个条目的字典,并对其键进行排序,最后打印出排序后的结果。
用C语言写一个总成绩排序
好的,以下是一个用C语言实现的按照总成绩从高到低排序的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char name[20];
int score1, score2, score3;
int total;
};
int cmp(const void* a, const void* b) {
return (*(struct Student*)b).total - (*(struct Student*)a).total;
}
int main() {
int n, i;
printf("请输入学生人数: ");
scanf("%d", &n);
struct Student* students = (struct Student*)malloc(sizeof(struct Student) * n);
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的信息(姓名 成绩1 成绩2 成绩3): ", i + 1);
scanf("%s %d %d %d", students[i].name, &students[i].score1, &students[i].score2, &students[i].score3);
students[i].total = students[i].score1 + students[i].score2 + students[i].score3;
}
qsort(students, n, sizeof(struct Student), cmp);
printf("按照总成绩从高到低排序后的结果为:\n");
for (i = 0; i < n; i++) {
printf("%s %d %d %d 总成绩:%d\n", students[i].name, students[i].score1, students[i].score2, students[i].score3, students[i].total);
}
free(students);
return 0;
}
```
程序中使用了结构体来存储每个学生的姓名和成绩,并计算出总成绩。使用qsort函数来进行排序,并按照指定格式输出排序后的结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)