-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort1.c
65 lines (55 loc) · 1.22 KB
/
QuickSort1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include "QuickSort.h"
void ArrayQuickSort(SDataType* arr, int left, int right) //基于数组的快速排序
{
int i, j;
SDataType temp;
if(left == right)
{
return;
}
i = left + 1;
j = right;
while(i < j)
{
while((i < j) && (arr[j].totalcount < arr[left].totalcount))
{
j--;
}
while((i < j) && (arr[i].totalcount >= arr[left].totalcount))
{
i++;
}
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
if(arr[left].totalcount < arr[i].totalcount)
{
temp = arr[left];
arr[left] = arr[i];
arr[i] = temp;
}
ArrayQuickSort(arr, left, i - 1);
ArrayQuickSort(arr, i, right);
}
void QuickSort(PNode head) //快速排序
{
int len = 0;
SDataType* arr = NULL;
if(head == NULL)
{
printf("排序失败,链表为空!\n");
}
len = ListLength(head);
arr = (SDataType*)malloc(sizeof(SDataType) * len);
if(arr == NULL)
{
printf("申请内存失败!\n");
}
List2Array(head, arr);
ArrayQuickSort(arr, 0, len - 1);
head = Array2List(head, arr, len);
free(arr);
}