50 排序的工程应用示例
原文:https://www.cnblogs.com/wanmeishenghuo/p/9688158.html 参考狄泰软件相关教程
我们要使Srot能排序Array数组类。
Sort应该既能排序静态数组类又能排序动态数组类。
这个函数返回原生数组的首地址。
数组类需要新增成员函数array,排序类需要新增六个静态成员函数。
Array.h添加array函数:
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 | #ifndef ARRAY_H #define ARRAY_H #include "Object.h" #include "Exception.h" namespace DTLib { template < typename T> class Array : public Object { protected : T* m_array; public : virtual bool set( int i, const T&e) //O(1) { bool ret = ((0 <= i) && (i < length())); if ( ret ) { m_array[i] = e; } return ret; } virtual bool get( int i, T& e) const //O(1) { bool ret = ((0 <= i) && (i < length())); if ( ret ) { e = m_array[i]; } return ret; } T& operator[] ( int i) //O(1) { if ((0 <= i) && (i < length())) { return m_array[i]; } else { THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid ..." ); } } T operator[] ( int i) const //O(1) { return ( const_cast <Array<T>>(* this ))[i]; } T* array() const { return m_array; } virtual int length() const = 0; }; } #endif // ARRAY_H |
Sort.h改进如下:
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | #ifndef SORT_H #define SORT_H #include "Object.h" #include <Array.h> namespace DTLib { class Sort : public Object { private : Sort(); Sort( const Sort&); Sort& operator = ( const Sort&); template < typename T> static void Swap(T& a, T& b) { T c(a); a = b; b = c; } template < typename T > static void Merge(T src[], T helper[], int begin, int mid, int end, bool min2max= true ) { int i = begin; int j = mid + 1; int k = begin; //代表辅助空间起始位置 while ( (i <= mid) && (j <= end) ) { if ( min2max ? (src[i] < src[j]) : (src[i] > src[j]) ) { helper[k++] = src[i++]; } else { helper[k++] = src[j++]; } } while ( i <= mid) { helper[k++] = src[i++]; } while ( j <= end ) { helper[k++] = src[j++]; } for (i = begin; i <= end; i++) { src[i] = helper[i]; } } template < typename T > static void Merge(T src[], T helper[], int begin, int end, bool min2max) { if ( begin < end ) { int mid = (begin + end) / 2; Merge(src, helper, begin, mid, min2max); Merge(src, helper, mid+1, end, min2max); Merge(src, helper, begin, mid, end, min2max); //真正的归并操作 } } template < typename T > static int Partition(T array[], int begin, int end, bool min2max) { T pv = array[begin]; while ( begin < end ) { while ( (begin < end) && (min2max ? (array[end] > pv) : (array[end] < pv)) ) { end--; } Swap(array[begin], array[end]); while ( (begin < end) && (min2max ? (array[begin] <= pv) : (array[begin] >= pv)) ) { begin++; } Swap(array[begin], array[end]); } array[begin] = pv; //基准就位 return begin; } template < typename T > static void Quick(T array[], int begin, int end, bool min2max) { if ( begin < end ) { int pivot = Partition(array, begin, end, min2max); Quick(array, begin, pivot - 1, min2max); Quick(array, pivot + 1, end, min2max); } } public : template < typename T > static void Select(T array[], int len, bool min2max= true ) { for ( int i = 0; i < len; i++) { int min = i; for ( int j = i + 1; j < len; j++) { if ( min2max ? (array[min] > array[j]) : (array[min] < array[j]) ) { min = j; } } if ( min != i) { Swap(array[i], array[min]); } } } template < typename T > static void Insert(T array[], int len, bool min2max= true ) { for ( int i=1; i < len; i++) //从1开始,第0个元素没有必要插入操作 { int k = i; T e = array[i]; for ( int j=i-1; (j>=0) && (min2max ? (array[j] > e) : (array[j] < e)); j--) { array[j+1] = array[j]; k = j; } if ( k != i ) //赋值比“比较操作耗时” { array[k] = e; } } } template < typename T > static void Bubble(T array[], int len, bool min2max= true ) { bool exchange = true ; for ( int i=0; (i<len) && exchange; i++) { exchange = false ; for ( int j=len-1; j>i; j--) { if (min2max ? (array[j] < array[j-1]) : (array[j] > array[j-1])) { Swap(array[j], array[j-1]); exchange = true ; } } } } template < typename T > static void Shell(T array[], int len, bool min2max= true ) { int d = len; do { d = d / 3 + 1; //d的减小方式(实践证明这样做效果比较好) for ( int i = d; i < len; i+=d) { int k = i; T e = array[i]; for ( int j=i-d; (j>=0) && (min2max ? (array[j] > e) : (array[j] < e)); j-=d) { array[j+d] = array[j]; k = j; } if ( k != i ) //赋值比“比较操作耗时” { array[k] = e; } } } while ( d > 1 ); } template < typename T > static void Merge(T array[], int len, bool min2max= true ) { T* helper = new T[len]; if ( helper != NULL ) { Merge(array, helper, 0, len - 1, min2max); } delete [] helper; } template < typename T > static void Quick(T array[], int len, bool min2max= true ) { Quick(array, 0, len - 1, min2max); } template < typename T > static void Select(Array<T>& array, bool min2max= true ) { Select(array.array(), array.length(), min2max); } template < typename T > static void Insert(Array<T>& array, bool min2max= true ) { Insert(array.array(), array.length(), min2max); } template < typename T > static void Bubbble(Array<T>& array, bool min2max= true ) { Bubble(array.array(), array.length(), min2max); } template < typename T > static void Shell(Array<T>& array, bool min2max= true ) { Shell(array.array(), array.length(), min2max); } template < typename T > static void Merge(Array<T>& array, bool min2max= true ) { Merge(array.array(), array.length(), min2max); } template < typename T > static void Quick(Array<T>& array, bool min2max= true ) { Quick(array.array(), array.length(), min2max); } }; } #endif // SORT_H |
无代理时的测试程序:
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 | #include <iostream> #include <ctime> #include "Sort.h" using namespace std; using namespace DTLib; struct Test : public Object { int id; int data1[1000]; double data2[500]; bool operator < ( const Test& obj) { return id < obj.id; } bool operator >= ( const Test& obj) { return id >= obj.id; } bool operator > ( const Test& obj) { return id > obj.id; } bool operator <= ( const Test& obj) { return id <= obj.id; } }; Test t[1000]; int main() { clock_t begin = 0; clock_t end = 0; for ( int i=0; i < 1000; i++) { t[i].id = i; } begin = clock (); Sort::Bubble(t, 1000, false ); end = clock (); cout << "Time : " << (end - begin) << endl; for ( int i=0; i < 1000; i++) { //cout << t[i]. << endl; } return 0; } |
结果如下:
使用代理类:
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 | #include <iostream> #include <ctime> #include "Sort.h" using namespace std; using namespace DTLib; struct Test : public Object { int id; int data1[1000]; double data2[500]; bool operator < ( const Test& obj) { return id < obj.id; } bool operator >= ( const Test& obj) { return id >= obj.id; } bool operator > ( const Test& obj) { return id > obj.id; } bool operator <= ( const Test& obj) { return id <= obj.id; } }; class TestProxy : public Object { protected : Test* m_pTest; public : //原始对象能干的事代理对象必须也要能干,因此要实现以下函数 int id() { return m_pTest->id; } int * data1() { return m_pTest->data1; } double * data2() { return m_pTest->data2; } Test& test() const //请出委托者的函数 { return *m_pTest; } bool operator < ( const TestProxy& obj) { return test() < obj.test(); //代理类对象的比较就是原始对象的比较 } bool operator >= ( const TestProxy& obj) { return test() >= obj.test(); //代理类对象的比较就是原始对象的比较 } bool operator > ( const TestProxy& obj) { return test() > obj.test(); //代理类对象的比较就是原始对象的比较 } bool operator <= ( const TestProxy& obj) { return test() <= obj.test(); //代理类对象的比较就是原始对象的比较 } Test& operator = (Test& test) { m_pTest = &test; return test; } }; Test t[1000]; TestProxy pt[1000]; int main() { clock_t begin = 0; clock_t end = 0; for ( int i=0; i < 1000; i++) { t[i].id = i; pt[i] = t[i]; //一一映射 } begin = clock (); Sort::Bubble(pt, 1000, false ); end = clock (); cout << "Time : " << (end - begin) << endl; for ( int i=0; i < 1000; i++) { //cout << t[i]. << endl; } return 0; } |
结果如下:
小结:
posted on 2020-11-11 13:11 lh03061238 阅读(50) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2018-11-11 echo > 和 echo >>的区别
2018-11-11 Linux下动态库(.so)和静态库(.a) 的区别