| 12
 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
 
 |  #include <stdio.h>#include <stdlib.h>
 #include <assert.h>
 #include <time.h>
 #include <string.h>
 
 #include "Stack.h"
 
 void Swap(int *m, int *n)
 {
 int tmp = *m;
 *m = *n;
 *n = tmp;
 }
 
 int GetMid(int *a, int left, int right)
 {
 assert(a);
 int mid = left + ((right - left) >> 2);
 if (a[left] <= a[right]){
 if (a[mid] < a[left])
 return left;
 else if (a[mid]>a[right])
 return right;
 else
 return mid;
 }
 else{
 if (a[mid] > a[left])
 return left;
 else if (a[mid] < a[right])
 return right;
 else
 return mid;
 }
 }
 
 int PartionOne(int *a, int begin, int end)
 {
 
 int mid = GetMid(a, begin, end);
 Swap(&a[mid], &a[end]);
 int key = a[end];
 int index = end;
 
 while (begin < end){
 
 while (begin < end && a[begin] <= key)
 begin++;
 
 while (begin < end && a[end] >= key)
 end--;
 
 Swap(&a[begin], &a[end]);
 }
 Swap(&a[begin], &a[index]);
 return begin;
 }
 
 int PartionTwo(int *a, int begin, int end)
 {
 
 int key = a[end];
 while (begin < end){
 while (begin < end && a[begin] <= key)
 begin++;
 a[end] = a[begin];
 while (begin < end && a[end] >= key)
 end--;
 a[begin] = a[end];
 }
 a[begin] = key;
 return begin;
 }
 
 int PrationThree(int *a, int begin, int end)
 {
 assert(a);
 if (begin >= end)
 return -1;
 int key = a[end];
 int prev = begin - 1;
 int next = begin;
 while (next < end){
 
 if (a[next] < key && ++prev != next)
 Swap(&a[prev], &a[next]);
 next++;
 }
 
 Swap(&a[end], &a[++prev]);
 return prev;
 }
 
 void QuickSort(int *a, int left, int right){
 if (left >= right)
 return;
 assert(a);
 
 int div = PartionOne(a, left, right);
 
 
 
 QuickSort(a, left, div - 1);
 
 QuickSort(a, div + 1, right);
 }
 
 void QuickSortTwo(int *a, int left, int right){
 if (left >= right)
 return;
 assert(a);
 
 if (right - left <= 3){
 for (int i = 0; i < right-1; i++){
 int index = i + 1;
 int cur = a[index];
 if (a[i]>a[index]){
 while (index > 0 && cur > a[index + 1]){
 a[index + 1] = a[index];
 ++index;
 }
 }
 a[index] = cur;
 }
 }
 
 
 
 int div = PrationThree(a, left, right);
 
 QuickSort(a, left, div - 1);
 
 QuickSort(a, div + 1, right);
 }
 
 void QuickSortRecNone(int *a, int left, int right){
 if (left >= right)
 return;
 assert(a);
 Stack s;
 StackInit(&s);
 StackPush(&s, left);
 StackPush(&s, right);
 while (StackEmpty(&s) != 0){
 int end = StackTop(&s);
 StackPop(&s);
 int begin = StackTop(&s);
 StackPop(&s);
 int div = PartionOne(a, begin, end);
 
 if (begin < div - 1){
 StackPush(&s, begin);
 StackPush(&s, div - 1);
 }
 if(end > div + 1){
 StackPush(&s, div + 1);
 StackPush(&s, end);
 }
 }
 }
 
 void PrintArray(int *a, int len)
 {
 assert(a);
 for (int i = 0; i < len; i++)
 printf("%d ", a[i]);
 printf("\n");
 }
 int main()
 {
 int a[] = { 58, 25, 49, 25, 16, 34 };
 QuickSort(a, 0, sizeof(a) / sizeof(int)-1);
 
 
 PrintArray(a, sizeof(a) / sizeof(int));
 system("pause");
 return 0;
 }
 
 |