From 6482b5ea08f58b5875b560e60300c9be4cf54a6d Mon Sep 17 00:00:00 2001 From: Ranger Date: Sun, 9 Aug 2015 22:41:32 +0800 Subject: [PATCH 1/8] add insert sort --- btsearch.cpp | 76 ------------------------------------- graphSearch.cpp | 99 ------------------------------------------------- insertSort.cpp | 40 ++++++++++++++++++++ 3 files changed, 40 insertions(+), 175 deletions(-) delete mode 100644 btsearch.cpp delete mode 100644 graphSearch.cpp create mode 100644 insertSort.cpp diff --git a/btsearch.cpp b/btsearch.cpp deleted file mode 100644 index f908d6f..0000000 --- a/btsearch.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include -#include -#include -using namespace std; - -int index = 0; - -typedef struct Node -{ - char data; - struct Node *lchild; - struct Node *rchild; -}* Tree; - -void treeConstructor(Tree &node, char data[]) -{ - char e = data[index++]; - if(e == '#') - node = nullptr; - else - { - node = new Node; - node->data = e; - treeConstructor(node->lchild, data); - treeConstructor(node->rchild, data); - } -} - -void DFS(Tree root) -{ - stack nodeStack; - nodeStack.push(root); - Node *node; - while(!nodeStack.empty()) - { - node = nodeStack.top(); - cout << node->data; - nodeStack.pop(); - if(node->rchild) - nodeStack.push(node->rchild); - if(node->lchild) - nodeStack.push(node->lchild); - } -} - -void BFS(Tree root) -{ - queuenodeQueue; - nodeQueue.push(root); - Node *node; - while(!nodeQueue.empty()) - { - node = nodeQueue.front(); - cout << node->data; - nodeQueue.pop(); - if(node->lchild) - nodeQueue.push(node->lchild); - if(node->rchild) - nodeQueue.push(node->rchild); - } -} - -int main() -{ - char data[16] = {'A', 'B', 'D', '#', '#', 'E', '#', '#', 'C', 'F', '#', '#', 'G', '#', '#', '\0'}; - Tree root; - treeConstructor(root, data); - cout << "DFS: "; - DFS(root); - cout << endl; - cout << "DFS: "; - BFS(root); - cout << endl; - - return 0; -} diff --git a/graphSearch.cpp b/graphSearch.cpp deleted file mode 100644 index ff144a0..0000000 --- a/graphSearch.cpp +++ /dev/null @@ -1,99 +0,0 @@ -#include -#include -#include -using namespace std; - -const int vnum = 5; -int visited[vnum]; - -struct Graph -{ - int vertex[vnum]; - int arc[vnum][vnum]; -}; - -void CreatGraph(Graph &G, int data[], int maxtrix[][vnum]) -{ - for(int i = 0; i < vnum; i++) - G.vertex[i] = data[i]; - for(int i = 0; i < vnum; i++) - for(int j = 0; j < vnum; j++) - G.arc[i][j] = maxtrix[i][j]; -} - -void DFS(Graph *G, int v) -{ - if(!visited[v]) - { - cout << G->vertex[v]; - visited[v] = 1; - } - for(int i = 0; i < vnum; i++) - if(G->arc[v][i] && visited[i] != 1) - DFS(G, i); -} - -void BFS(Graph *G, int v) -{ - int f; - queue q; - if(!visited[v]) - { - cout << G->vertex[v]; - visited[v] = 1; - q.push(v); - } - while(!q.empty()) - { - f = q.front(); - q.pop(); - for(int i = 0; i < vnum; i++) - if(G->arc[f][i] && visited[i] != 1) - { - cout << G->vertex[i]; - visited[i] = 1; - q.push(i); - } - } -} - -int main() -{ - Graph *G = new Graph; - - int data[vnum] = {1, 2, 3, 4, 5}; - int arcMatrix[vnum][vnum] = - { - {0, 1, 1, 0, 0}, - {1, 0, 0, 1, 0}, - {1, 0, 0, 1, 1}, - {0, 1, 1, 0, 1}, - {0, 0, 1, 1, 0}, - }; - - CreatGraph(*G, data, arcMatrix); - cout << "vertex: "; - for(int i = 0; i < vnum; i++) - cout << G->vertex[i]; - cout << endl << "arc: " << endl; - for(int i = 0; i < vnum; i++) - { - cout << " "; - for(int j = 0; j < vnum; j++) - cout << G->arc[i][j]; - cout << endl; - } - cout << endl; - - cout << "DFS: " << endl; - DFS(G, 0); - cout << endl; - - memset(visited, 0, sizeof(visited)); - - cout << "BFS: " << endl; - BFS(G, 0); - cout << endl; - - return 0; -} diff --git a/insertSort.cpp b/insertSort.cpp new file mode 100644 index 0000000..792062d --- /dev/null +++ b/insertSort.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +const int N = 8; + +void Print(int data[], int index) +{ + cout << index << ": "; + for(int i = 0; i < N; i++) + cout << data[i] << " "; + cout << endl; +} + +void InsertSort(int data[]) +{ + int j, tmp; + for(int i = 1; i < N; i++) //i means the point + { + tmp = data[i]; + j = i - 1; + + while(tmp < data[j] && j >= 0) + { + data[j + 1] = data[j]; + j--; + } + data[j + 1] = tmp; // insert + + Print(data,i); + } + +} + +int main() +{ + int data[N] = {49, 38, 65, 97, 76, 13, 27, 49}; + InsertSort(data); + + return 0; +} \ No newline at end of file From 3dc810a4823a93ab5293f07e6ad6013e7bebb945 Mon Sep 17 00:00:00 2001 From: Ranger Date: Mon, 10 Aug 2015 10:45:00 +0800 Subject: [PATCH 2/8] ajust output --- insertSort.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/insertSort.cpp b/insertSort.cpp index 792062d..c7b6d1e 100644 --- a/insertSort.cpp +++ b/insertSort.cpp @@ -34,6 +34,7 @@ void InsertSort(int data[]) int main() { int data[N] = {49, 38, 65, 97, 76, 13, 27, 49}; + Print(data, 0); InsertSort(data); return 0; From 9b4eddd93932b710032b2459f270bea6488045b9 Mon Sep 17 00:00:00 2001 From: Ranger Date: Mon, 10 Aug 2015 10:46:27 +0800 Subject: [PATCH 3/8] add select sort --- selectSort.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 selectSort.cpp diff --git a/selectSort.cpp b/selectSort.cpp new file mode 100644 index 0000000..50854d7 --- /dev/null +++ b/selectSort.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +const int N = 8; + +void Print(int data[], int index) +{ + cout << index << ": "; + for(int i = 0; i < N; i++) + cout << data[i] << " "; + cout << endl; +} + +void SelectSort(int data[]) +{ + int k, tmp; + for(int i = 0; i < N; i++) + { + k = i; + for(int j = i + 1; j < N; j++) //select the min one + if(data[j] < data[k]) + k = j; + + if(k != i) + { + tmp = data[i]; + data[i] = data[k]; + data[k] = tmp; + } + + Print(data,i + 1); + } + +} + +int main() +{ + int data[N] = {49, 38, 65, 97, 76, 13, 27, 49}; + Print(data, 0); + SelectSort(data); + + return 0; +} \ No newline at end of file From 56e1249c3ac916b684779499b0df7d81750dccbb Mon Sep 17 00:00:00 2001 From: Ranger Date: Mon, 10 Aug 2015 12:13:50 +0800 Subject: [PATCH 4/8] add bubble sort --- bubbleSort.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 bubbleSort.cpp diff --git a/bubbleSort.cpp b/bubbleSort.cpp new file mode 100644 index 0000000..4e0125c --- /dev/null +++ b/bubbleSort.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +const int N = 8; + +void Print(int data[], int index) +{ + cout << index << ": "; + for(int i = 0; i < N; i++) + cout << data[i] << " "; + cout << endl; +} + +void Swap(int data[], int j) +{ + int tmp = data[j]; + data[j] = data[j + 1]; + data[j + 1] = tmp; +} + +void BubbleSort_1(int data[]) //simplest one +{ + for(int i = 0; i < N - 1; i++) + { + for(int j = 0; j < N - 1 - i; j++) + { + if(data[j] > data[j + 1]) + Swap(data, j); + } + Print(data,i + 1); + } + +} + +void BubbleSort_2(int data[]) // find the min one and the max one every time +{ + int low = 0; + int high = N -1; + int i = 0, j; + while(low < high) + { + for(j = low; j < high; ++j) + if(data[j] > data[j + 1]) + Swap(data, j); + --high; + + for(j = high; j > low; --j) + if(data[j - 1] > data[j]) + Swap(data, j - 1); + ++low; + + Print(data, ++i); + } +} + +int main() +{ + int data[N] = {49, 38, 65, 97, 76, 13, 27, 49}; + Print(data, 0); + //BubbleSort_1(data); + BubbleSort_2(data); + + return 0; +} \ No newline at end of file From 7ba48c25a71576f449ec6bd8786182cd3eec5b48 Mon Sep 17 00:00:00 2001 From: Ranger Date: Mon, 10 Aug 2015 14:10:41 +0800 Subject: [PATCH 5/8] modified Swap function --- bubbleSort.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/bubbleSort.cpp b/bubbleSort.cpp index 4e0125c..05a967e 100644 --- a/bubbleSort.cpp +++ b/bubbleSort.cpp @@ -11,11 +11,11 @@ void Print(int data[], int index) cout << endl; } -void Swap(int data[], int j) +void Swap(int &a, int &b) { - int tmp = data[j]; - data[j] = data[j + 1]; - data[j + 1] = tmp; + int tmp = a; + a = b; + b = tmp; } void BubbleSort_1(int data[]) //simplest one @@ -25,7 +25,7 @@ void BubbleSort_1(int data[]) //simplest one for(int j = 0; j < N - 1 - i; j++) { if(data[j] > data[j + 1]) - Swap(data, j); + Swap(data[j], data[j+1]); } Print(data,i + 1); } @@ -41,12 +41,12 @@ void BubbleSort_2(int data[]) // find the min one and the max one every time { for(j = low; j < high; ++j) if(data[j] > data[j + 1]) - Swap(data, j); + Swap(data[j], data[j+1]); --high; for(j = high; j > low; --j) if(data[j - 1] > data[j]) - Swap(data, j - 1); + Swap(data[j-1], data[j]); ++low; Print(data, ++i); @@ -57,8 +57,8 @@ int main() { int data[N] = {49, 38, 65, 97, 76, 13, 27, 49}; Print(data, 0); - //BubbleSort_1(data); - BubbleSort_2(data); + BubbleSort_1(data); + //BubbleSort_2(data); return 0; } \ No newline at end of file From e0a995f4c4225e18f45a9a534f2339465601915b Mon Sep 17 00:00:00 2001 From: Ranger Date: Mon, 10 Aug 2015 16:17:39 +0800 Subject: [PATCH 6/8] add basic quick sort --- quickSort.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 quickSort.cpp diff --git a/quickSort.cpp b/quickSort.cpp new file mode 100644 index 0000000..dc8bb4a --- /dev/null +++ b/quickSort.cpp @@ -0,0 +1,57 @@ +#include +using namespace std; + +const int N = 8; +int x = 0; + +void Output(int data[], int index) +{ + cout << index << ": "; + for(int i = 0; i < N; i++) + cout << data[i] << " "; + cout << endl; +} + +void Swap(int &a, int &b) +{ + int tmp = a; + a = b; + b = tmp; +} + +int Partition(int data[], int low, int high) +{ + int pivot = data[low]; + while(low < high) + { + while(low < high && data[high] >= pivot) + --high; + Swap(data[low], data[high]); + + while(low < high && data[low] <= pivot) + ++low; + Swap(data[low], data[high]); + } + + Output(data, ++x); + return low; +} + +void QuickSort(int data[], int low, int high) //recursion +{ + if(low < high) + { + int pivot = Partition(data, low, high); + QuickSort(data, low, pivot - 1); + QuickSort(data, pivot + 1, high); + } +} + +int main() +{ + int data[N] = {49, 38, 65, 97, 76, 13, 27, 49}; + Output(data, 0); + QuickSort(data,0, N-1); + + return 0; +} \ No newline at end of file From 58d4344c886bf81293c5ed07d028ae99afe62354 Mon Sep 17 00:00:00 2001 From: Ranger Date: Fri, 9 Oct 2015 22:34:21 +0800 Subject: [PATCH 7/8] add non-recursive method --- quickSort.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/quickSort.cpp b/quickSort.cpp index dc8bb4a..de4ba4b 100644 --- a/quickSort.cpp +++ b/quickSort.cpp @@ -47,6 +47,35 @@ void QuickSort(int data[], int low, int high) //recursion } } +void QuickSort2(int nums[],int low,int high) +{ + stack st; + int pivot; + do{ + while(low < high) + { + pivot = partition(nums, low, high); + if((pivot - low) < (high - pivot)) + { + st.push(pivot + 1); + st.push(high); + high = pivot - 1; + } + else + { + st.push(low); + st.push(pivot - 1); + low = pivot + 1; + } + } + if(st.empty()) return; + high = st.top(); + st.pop(); + low = st.top(); + st.pop(); + }while(1); +} + int main() { int data[N] = {49, 38, 65, 97, 76, 13, 27, 49}; From 58f36d6818fd1dc09d7468d5cf5190366ec1c3e6 Mon Sep 17 00:00:00 2001 From: Ranger Date: Mon, 12 Oct 2015 20:48:40 +0800 Subject: [PATCH 8/8] merge sort --- mergeSort.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 mergeSort.cpp diff --git a/mergeSort.cpp b/mergeSort.cpp new file mode 100644 index 0000000..d93c5a5 --- /dev/null +++ b/mergeSort.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +const int N = 20; + +void output(int nums[]) +{ + for(int i = 0; i < N; i++) + cout << nums[i]<< " "; + cout << endl; +} + +void merge(int nums[], int p, int q, int r) +{ + int i, j, k; + int n1 = q - p + 1; + int n2 = r - q; + int *left = new int[n1]; + int *right = new int [n2]; + + for(i = 0; i < n1; i++) + left[i] = nums[p + i]; + for(j = 0; j < n2; j++) + right[j] = nums[q + 1 + j]; + + i = j = 0; + k = p; + while(i < n1 && j < n2) + { + if(left[i] <= right[j]) + nums[k++] = left[i++]; + else + nums[k++] = right[j++]; + } + + while(i < n1) + nums[k++] = left[i++]; + while(j < n2) + nums[k++] = right[j++]; + + delete []left; + delete []right; +} + + +void mergeSort(int nums[], int p, int r) +{ + if(p < r) + { + int q = (p + r)/2; + mergeSort(nums, p, q); + mergeSort(nums, q+1, r); + + merge(nums, p, q, r); + + output(nums); + } +} + +int main() +{ + int nums[N] = {4, 11, 6, 33, 3, 99, 9, 22, 1, 55, 2, 44, 7, 88, 0, 77, 2, 66, 5, 34}; + + mergeSort(nums, 0, N-1); + + return 0; +} \ No newline at end of file