create quicksort.cpp, all tests passed.

This commit is contained in:
Shine wOng
2019-10-10 16:57:34 +08:00
parent 2674e6ef9e
commit 94a21bd32c
3 changed files with 159 additions and 5 deletions

144
thu_dsa/chp12/quicksort.cpp Normal file
View File

@@ -0,0 +1,144 @@
#include <iostream>
#include <cassert>
#include <random>
#include <ctime>
#define PARTITION3
#define SCALE ((int)2048)
using namespace std;
int partition1(int *src, int n, int pivot);
int partition2(int *src, int n, int pivot);
int partition3(int *src, int n, int pivot);
time_t test_partition();
time_t test_worst_case();
int partition(int *src, int n, int pivot){
#ifdef PARTITION1
return partition1(src, n, pivot);
#else
#ifdef PARTITION2
return partition2(src, n, pivot);
#else
return partition3(src, n, pivot);
#endif
#endif
}
int partition1(int *src, int n, int pivot){
assert(n > 1 && pivot == src[0]);
int pos = 0, tail = n, head = 0;
while (1) {
while (--tail > pos && src[tail] >= pivot);
if (tail == pos) break;
src[pos] = src[tail];
pos = tail;
while (++head < pos && src[head] <= pivot);
if (head == pos) break;
src[pos] = src[head];
pos = head;
}
src[pos] = pivot;
return pos;
}
int partition2(int *src, int n, int pivot){
assert(n > 1 && pivot == src[0]);
int pos = 0, tail = n, head = 0;
while (1) {
while (--tail > pos && src[tail] > pivot);
if (tail == pos) break;
src[pos] = src[tail];
pos = tail;
while (++head < pos && src[head] < pivot);
if (head == pos) break;
src[pos] = src[head];
pos = head;
}
src[pos] = pivot;
return pos;
}
int partition3(int *src, int n, int pivot){
assert(n > 1 && pivot == src[0]);
int mid = 1, current = 0, temp;
while(++current != n){
if(src[current] < pivot){
//swap
temp = src[current];
src[current] = src[mid];
src[mid] = temp;
++mid;
}
}
src[0] = src[mid - 1];
src[mid - 1] = pivot;
return mid - 1;
}
void quickSort(int *src, int n){
if (n == 0 || n == 1) return;
//init a pivot
int randpos = rand() % n;
int pivot = src[randpos];
src[randpos] = src[0];
src[0] = pivot;
int pos = partition(src, n, pivot);
quickSort(src, pos);
quickSort(src + pos + 1, n - pos - 1);
}
int main(){
srand((int)time(NULL));
clock_t time;
cout << "begin tests..." << endl;
time = test_partition();
cout << "test partition passed, running time: " << time << endl;
time = test_worst_case();
cout << "test_worst_case passed, running time:" << time << endl;
cout << "all tests passed" << endl;
system("pause");
return 0;
}
time_t test_partition(){
time_t start, end;
//generate data
int *src = new int[SCALE];
for (int ix = 0; ix != SCALE; ++ix)
src[ix] = rand();
start = clock();
quickSort(src, SCALE);
end = clock();
for (int ix = 0; ix != SCALE - 1; ++ix) {
assert(src[ix] <= src[ix + 1]);
}
return end - start;
}
//this func may cause stack overflow if SCALE if too BIG
time_t test_worst_case(){
time_t start, end;
int *src = new int[SCALE];
for (int ix = 0; ix != SCALE; ++ix)
src[ix] = 0;
start = clock();
quickSort(src, SCALE);
end = clock();
for (int ix = 0; ix != SCALE - 1; ++ix) {
assert(src[ix] <= src[ix + 1]);
}
return end - start;
}

View File

@@ -126,7 +126,7 @@ static __noinline void __up(semaphore_t *sem, uint32_t wait_state) {
`ucore`中,是使用`AND`信号量来实现哲学家就餐问题的,关于哲学家就餐问题和`AND`信号量,在[哲学家就餐问题及其实现](philosopher.md)中已经有过了描述和伪代码实现。这里就不再赘述,只是把代码贴在下面:
```
```c
int state_sema[N]; /* 记录每个人状态的数组 */
/* 信号量是一个特殊的整型变量 */
semaphore_t mutex; /* 临界区互斥 */

View File

@@ -659,7 +659,7 @@ Some Words
- Thousands of ex-army officers have found lucrative jobs in private security firms.
+ upstart
> (n)someone who behaves as it they were more important than they really are and who shows a lack of respect towards people who are more experienced and or older.
> (n)someone who behaves as if they were more important than they really are and who shows a lack of respect towards people who are more experienced and or older.
- Many prefer a familiar authority figure to a yound upstart.
@@ -943,7 +943,7 @@ Some Words
+ accountable
> (adj)responsible for the effects of your actions and willing to explain or be criticised for them
- The hospital should be held accountable for the quality of care if gives.
- The hospital should be held accountable for the quality of care it gives.
- Managers must be accountable to their decisions.
- The government should be accountable to all the people of the country.
@@ -978,7 +978,7 @@ Some Words
> (v)used to say that someone dislikes someone or something very much</br>
> (v)abide by: to accept and obey a decision, agreement etc, even though you may not agree with it
- I can's abide that man -- he's so self-satisfied.
- I can't abide that man -- he's so self-satisfied.
- You have to abide by the referee's decision.
## 3rd, October
@@ -1071,7 +1071,7 @@ Some Words
+ clamp
> (v)to hold two things together using a clamp</br>
> (v)to put or hold something in a position so that it cannot move</br>
> (v)to put limits on what some is allowed to do</br>
> (v)to put limits on what someone is allowed to do</br>
> (v)clamp down: to take firm action to stop a particular type of crime
- She clamped her hands over her ears.
@@ -1115,3 +1115,13 @@ Some Words
> (adj)more than is needed or wanted, unnecessary
- a modern building with no superfluous decoration
## 4th, October
+ sanction
> (n)official orders or laws stopping trade, communication etc with another country, as a way of forcing its leaders to make political changes</br>
> (n)official permission, approval, or acceptance
- a resolution to impose sanctions on DPRK
- the threat of trade sanctions
- a newspaper run by citizens without the sanction of the government