-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
196 lines (170 loc) · 4.28 KB
/
main.c
File metadata and controls
196 lines (170 loc) · 4.28 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/********************************************
1,快排的关键在于定位部分
2,QuickSort参数(0,length —1)
3,同时给出linkedlist model
考虑到方向问题,这里采用双向链表
********************************************/
#include <stdio.h>
#include <stdlib.h>
struct DataType
{
int number;
struct DataType* previous;
struct DataType* next;
};
void QuickSort(int arr[], int first, int last);
int Partition(int arr[], int first, int last);
void PrintArray(int arr[], int length);
void QuickSortLinkedListModel(struct DataType* head, struct DataType* tail);
struct DataType* TranslateArrayToLinkedList(int arr[], int length);
void PrintLinkedList(struct DataType* head);
void FreeLinkedList(struct DataType* head);
struct DataType* FindTail(struct DataType* head);
int main()
{
int arr[10] = {2, 33, 3432, 44, 66, 235, 23, 788, 3, 24};
QuickSort(arr, 0, 9);
PrintArray(arr, 10);
printf("\n\n");
struct DataType* head = TranslateArrayToLinkedList(arr, 10);
struct DataType* tail = FindTail(head);
QuickSortLinkedListModel(head, tail);
PrintLinkedList(head);
FreeLinkedList(head);
return 0;
}
void QuickSort(int arr[], int first, int last)
{
if(first >= last)
{
return;
}
int position;
position = Partition(arr, first, last);
QuickSort(arr, first, position - 1);
QuickSort(arr, position + 1, last);
return;
}
int Partition(int arr[], int first, int last)
{
int key = arr[first];
while(first < last)
{
while(first < last && arr[last] >= key)
{
last--;
}
arr[first] = arr[last];
while(first < last && arr[first] <= key)
{
first++;
}
arr[last] = arr[first];
}
arr[first] = key;
return first;
}
void PrintArray(int arr[], int length)
{
int i;
int count = 0;
for(i = 0; i < length; i++)
{
if(count != 0 && count%10 == 0)
{
printf("\n");
}
printf("%d ", arr[i]);
count++;
}
return;
}
void QuickSortLinkedListModel(struct DataType* head, struct DataType* tail)
{
/*这里要注意一下head->previous != tail情况*/
if(head == NULL || tail == NULL || head == tail || head->previous != tail)
{
return;
}
struct DataType* subHead = head;
struct DataType* subTail = tail;
int key = subHead->number;
while(subHead != subTail)
{
while(subTail->number >= subHead->number && subHead != subTail)
{
subTail = subTail->previous;
}
subHead->number = subTail->number;
while(subHead->number <= subTail->number && subHead != subTail)
{
subHead = subHead->next;
}
subTail->number = subHead->number;
}
subHead->number = key;
QuickSortLinkedListModel(head, subHead->previous);
QuickSortLinkedListModel(subHead->next, tail);
return;
}
struct DataType* TranslateArrayToLinkedList(int arr[], int length)
{
int i;
struct DataType* head = NULL;
struct DataType* ptr = NULL;
if(length <= 0)
{
return NULL;
}
ptr = (struct DataType*)malloc(sizeof(struct DataType));
ptr->previous = NULL;
ptr->next = NULL;
ptr->number = arr[0];
head = ptr;
for(i = 1; i < length; i++)
{
struct DataType* temp = NULL;
temp = (struct DataType*)malloc(sizeof(struct DataType));
temp->previous = NULL;
temp->next = NULL;
temp->number = arr[i];
ptr->next = temp;
temp->previous = ptr;
ptr = temp;
}
return head;
}
void PrintLinkedList(struct DataType* head)
{
struct DataType* ptr = NULL;
int count = 0;
for(ptr = head; ptr != NULL; ptr = ptr->next)
{
if(count != 0 && count%10 == 0)
{
printf("\n");
}
printf("%d ", ptr->number);
}
return;
}
void FreeLinkedList(struct DataType* head)
{
struct DataType* ptr = NULL;
struct DataType* temp = NULL;
for(ptr = head; ptr != NULL; ptr = temp)
{
temp = ptr->next;
free(ptr);
}
return;
}
struct DataType* FindTail(struct DataType* head)
{
struct DataType* tail = NULL;
for(tail = head; tail != NULL && tail->next != NULL; tail = tail->next)
{
/*no content here!*/
}
return tail;
}