50 排序的工程应用示例
2021-03-18 03:25
阅读:788
标签:log end ima stp std alt sort virt null
原文:https://www.cnblogs.com/wanmeishenghuo/p/9688158.html 参考狄泰软件相关教程
我们要使Srot能排序Array数组类。
Sort应该既能排序静态数组类又能排序动态数组类。
这个函数返回原生数组的首地址。
数组类需要新增成员函数array,排序类需要新增六个静态成员函数。
Array.h添加array函数:
#ifndef ARRAY_H #define ARRAY_H #include "Object.h" #include "Exception.h" namespace DTLib { templateclass Array : public Object { protected: T* m_array; public: virtual bool set(int i, const T&e) //O(1) { bool ret = ((0 >(*this))[i]; } T* array()const { return m_array; } virtual int length() const = 0; }; } #endif // ARRAY_H
Sort.h改进如下:
#ifndef SORT_H #define SORT_H #include "Object.h" #include namespace DTLib { class Sort : public Object { private: Sort(); Sort(const Sort&); Sort& operator = (const Sort&); templatestatic void Swap(T& a, T& b) { T c(a); a = b; b = c; } template 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 src[j]) ) { helper[k++] = src[i++]; } else { helper[k++] = src[j++]; } } while( i static void Merge(T src[], T helper[], int begin, int end, bool min2max) { if( begin static int Partition(T array[], int begin, int end, bool min2max) { T pv = array[begin]; while( begin pv) : (array[end] = pv)) ) { begin++; } Swap(array[begin], array[end]); } array[begin] = pv; //基准就位 return begin; } template static void Quick(T array[], int begin, int end, bool min2max) { if( begin static void Select(T array[], int len, bool min2max=true) { for(int i = 0; i array[j]) : (array[min] static void Insert(T array[], int len, bool min2max=true) { for(int i=1; i =0) && (min2max ? (array[j] > e) : (array[j] static void Bubble(T array[], int len, bool min2max=true) { bool exchange = true; for(int i=0; (i i; j--) { if(min2max ? (array[j] array[j-1])) { Swap(array[j], array[j-1]); exchange = true; } } } } template static void Shell(T array[], int len, bool min2max=true) { int d = len; do { d = d / 3 + 1; //d的减小方式(实践证明这样做效果比较好) for(int i = d; i =0) && (min2max ? (array[j] > e) : (array[j] 1 ); } template 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 static void Quick(T array[], int len, bool min2max=true) { Quick(array, 0, len - 1, min2max); } template static void Select(Array & array, bool min2max=true) { Select(array.array(), array.length(), min2max); } template static void Insert(Array & array, bool min2max=true) { Insert(array.array(), array.length(), min2max); } template static void Bubbble(Array & array, bool min2max=true) { Bubble(array.array(), array.length(), min2max); } template static void Shell(Array & array, bool min2max=true) { Shell(array.array(), array.length(), min2max); } template static void Merge(Array & array, bool min2max=true) { Merge(array.array(), array.length(), min2max); } template static void Quick(Array & array, bool min2max=true) { Quick(array.array(), array.length(), min2max); } }; } #endif // SORT_H
无代理时的测试程序:
#include#include #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
结果如下:
使用代理类:
#include#include #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 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
结果如下:
小结:
50 排序的工程应用示例
标签:log end ima stp std alt sort virt null
原文地址:https://www.cnblogs.com/lh03061238/p/13958174.html
下一篇:创建NFC读卡python脚本
评论
亲,登录后才可以留言!