From 78575490510c1edd8f02a1f5843afd5c0a3013e1 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Fri, 15 Oct 2021 15:20:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E6=98=8E=E7=99=BD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex3/ex3.cpp | 11 ++++++----- ex3/vec.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++---- ex3/vec.h | 10 ++++++---- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/ex3/ex3.cpp b/ex3/ex3.cpp index 39fbeff..8bfa5a1 100644 --- a/ex3/ex3.cpp +++ b/ex3/ex3.cpp @@ -13,8 +13,9 @@ int main() Vec v(len); v.printall(); system("pause"); - printf("排序\n"); - v.sort(); + printf("排序:秩为%d\n",v.getused()); + // v.bubbleSort(); + v.mergeSort(0,v.getused()); v.printall(); while (1) { @@ -26,7 +27,7 @@ int main() case 1: printf("输入待插入数值\n"); scanf("%d", &temp[0]); - (v.insert(v.search(temp[0]), temp[0]) != -1) ? (printf("输出数组\n")) : (printf("无此元素或超出范围\n")); + (v.insert(v.search(temp[0]) + 1, temp[0]) != -1) ? (printf("输出数组\n")) : (printf("无此元素或超出范围\n")); v.printall(); break; @@ -47,11 +48,11 @@ int main() case 4: return 0; - + default: printf("返回菜单\n"); } flag = 0; } return 0; -} \ No newline at end of file +} diff --git a/ex3/vec.cpp b/ex3/vec.cpp index 67428b8..1afd98e 100644 --- a/ex3/vec.cpp +++ b/ex3/vec.cpp @@ -62,6 +62,12 @@ void Vec::shrink() //位置 数值 int Vec::insert(int locate, int value) { + if (locate == _used + 1) + { + _used++; + _v[_used] = value; + return 0; + } if (locate < 0 || locate > _used) return -1; @@ -151,11 +157,11 @@ int Vec::getused() void Vec::printall() { - for (int i = 0; i < _used; i++) + for (int i = 0; i <= _used; i++) printf("%d\n", _v[i]); } -void Vec::sort() +void Vec::bubbleSort() { int n = _used; bool sorted = false; @@ -164,7 +170,7 @@ void Vec::sort() { sorted = true; - for (int i = 1; i < n; i++) + for (int i = 1; i <= n; i++) { if (_v[i - 1] > _v[i]) { @@ -178,6 +184,17 @@ void Vec::sort() _sorted = true; } +void Vec::mergeSort(int lo, int hi) +{ + if (lo >= hi) + { + return; + } + int mi = lo + (hi - lo) / 2; + mergeSort(lo, mi); + mergeSort(mi + 1, hi); + merge(lo, mi, hi); +} int Vec::search(int e) { if (_sorted == false) @@ -188,4 +205,25 @@ int Vec::search(int e) ; return i - 1; -} \ No newline at end of file +} + +void Vec::merge(int lo, int mi, int hi) +{ + + int i = lo, j = mi + 1, k = 0; + int *temp = new int[hi - lo + 1]; + while (i <= mi && j <= hi) + { + if (_v[i] <= _v[j]) + temp[k++] = _v[i++]; + else + temp[k++] = _v[j++]; + } + while (i <= mi) + temp[k++] = _v[i++]; + while (j <= hi) + temp[k++] = _v[j++]; + for (i = lo, k = 0; i <= hi; i++, k++) + _v[i] = temp[k]; + delete[] temp; +} diff --git a/ex3/vec.h b/ex3/vec.h index 0e8cb6e..caa7eb8 100644 --- a/ex3/vec.h +++ b/ex3/vec.h @@ -39,7 +39,7 @@ public: int find(int value); - void sort(); + void bubbleSort(); void printall(); @@ -48,7 +48,9 @@ public: int getused(); int count(int value); + + void mergeSort(int lo, int hi); + +private: + void merge(int lo, int mi, int hi); }; - - -