void Msort(ElementType A[], ElementType TmpArray[], int Left, int Right) { int Center; if(Left < Right) { Center = (Left+Right)/2; Msort(A, TmpArray, Left, Center); Msort(A, TmpArray, Center+1, Right); Merge(A, TmpArray, Left, Center+1, Right); } }
void Msort(ElementType A[], ElementType TmpArray[], int Left, int Right) { int center; if (Left < Right) { // 分治法,递归调用合并 center = (Left + Right) / 2; Msort(A, TmpArray, Left, center); Msort(A, TmpArray, center+1, Right); Merge(A, TmpArray, Left, center+1, Right); // 调用参数对应函数形参A[], TmpArray[], Lpos, LeftEnd, Rpos } }arch
void CTrackContrl::Msort( int *SR, int *TR1, int s, int t ){ // 对SR[s..t]进行归并排序,排序后的记录存入TR1[s..t] if (s==t) { TR1[s] = SR[s]; } else { int TR2[10] ; int m = (s+t)/2; // 将 SR[s..t] 平分为 SR[s..m] 和 SR[m+1..t] Msort(SR,TR2,s,m); // 递归地将 SR[s..m] 归并为有序的 TR2[s..m] Msort(SR,TR2,m+1, t); // 递归地将SR[m+1..t]归并为有序的TR2[m+1..t] Merge(TR2,TR1,s,m,t); // 将TR2[s..m]和TR2[m+1..t] 归并到 TR1[s..t] }// else } // Msort
void Msort(vector<int> &v,int first,int last){ if (first >= last){ return; } int mid = (last - first) / 2 + first; Msort(v, first, mid); Msort(v, mid + 1, last); Merge(v, first, mid, last); cout << v << endl; return; }
void MergeSort(ElementType A[], int N) { ElementType * TmpArray = (ElementType *)malloc(N*sizeof(ElementType)); if(TmpArray == NULL) { fprintf(stderr, "not enough memory\n"); exit(1); } Msort(A, TmpArray, 0, N-1); free(TmpArray); }
void Mergesort(ElementType A[], int N) { ElementType *TmpArray; TmpArray = (ElementType *)malloc(sizeof(ElementType)*N); if (TmpArray == NULL) { // 内存分配光时,提示 printf("Out of space"); } else // 调用Msort排序数组 { Msort(A, TmpArray, 0, N - 1); Print(TmpArray, N); // 打印数组内元素 free(TmpArray); } }
int main(int argn, char** argv) { std::vector< int> nums; nums.resize(1); const int num_nums = atoi(argv[1]); const int max_size = 1000; for (size_t i = 0; i < num_nums; ++i) { nums.push_back(rand() % max_size); } int max_num = 0; for (size_t i = 0; i < nums.size(); ++i) { if (nums[i] > max_num) max_num = nums[i]; } std::cout << "max num " << max_num << std::endl; print(nums); std::queue<Msort> qu; qu.push(Msort(nums.size()/2, 0, nums.size() - 1)); size_t ops = 0; size_t count = 0; while ((qu.size() > 0) && (count < 500)) { count += 1; } print(nums); std::cout << "finished after " << count << " sorts, " << ops << " ops " << std::endl; size_t logn = 0; size_t num = nums.size(); while (num >>= 1) { logn += 1; } std::cout << " n logn = " << nums.size() << " * " << logn << " = " << nums.size() * logn << std::endl; return 0; }
void CTrackContrl::AlterDivideLeft(const int nAlterDivideWid,int coor[],const int nSize) { //先排序 Msort(coor,coor,0,nSize-1); CArray<CControlRect*,CControlRect*> sortTracker; for(int i=0;i<nSize;i++) { for(int j=0;j<m_selectRectTracker.GetSize();j++) { if(coor[i] == m_selectRectTracker[j]->m_rect.left) { sortTracker.Add(m_selectRectTracker[j]); break; } } } //然后移动 for(int i=1;i<sortTracker.GetSize();i++) { CRect rect; rect.left = sortTracker[i-1]->m_rect.right + nAlterDivideWid; rect.right = rect.left + sortTracker[i]->m_rect.Width(); rect.top = sortTracker[i]->m_rect.top; rect.bottom = sortTracker[i]->m_rect.bottom; CRect oldRect;//获取没有移动前的rect sortTracker[i]->GetTrueRect(&oldRect); sortTracker[i]->m_rect = rect;//把新的位置付给控件 sortTracker[i]->MoveWindow(&rect,FALSE); CRect newRect;//获取新的rect sortTracker[i]->GetTrueRect(&newRect); //重绘 sortTracker[i]->GetParent()->InvalidateRect(oldRect,TRUE); sortTracker[i]->GetParent()->InvalidateRect(newRect,TRUE); } }