## 归并排序 转

汪纬

`````` 1 #include <iostream>
2 #include <vector>
3 using namespace std;
4 template <typename Comparable>
5 void mergeSort(vector<Comparable> & a)
6 {
7     vector<Comparable> tmpArray(a.size());
8     mergeSort(a,tmpArray,0,a.size()-1);
9 }
10 template <typename Comparable>
11 void mergeSort(vector<Comparable> & a,vector<Comparable> & tmpArray,int left,int right)
12 {
13     if(left<right)
14     {
15         int center = (left+right)/2;
16         mergeSort(a,tmpArray,left,center);
17         mergeSort(a,tmpArray,center+1,right);
18         merge(a,tmpArray,left,center+1,right);
19     }
20 }
21 template <typename Comparable>
22 void merge(vector<Comparable> & a,vector<Comparable> & tmpArray,int leftPos,int rightPos,int rightEnd)
23 {
24     int leftEnd = rightPos-1;
25     int tmpPos = leftPos;
26     int numElements = rightEnd - leftPos + 1;
27
28     while(leftPos <= leftEnd && rightPos <= rightEnd)
29         if(a[leftPos] <= a[rightPos])
30             tmpArray[tmpPos++] = a[leftPos++];
31         else
32             tmpArray[tmpPos++] = a[rightPos++];
33
34     while(leftPos <= leftEnd)
35         tmpArray[tmpPos++] = a[leftPos++];
36     while(rightPos <= rightEnd)
37         tmpArray[tmpPos++] = a[rightPos++];
38
39     for(int i=0;i<numElements;i++,rightEnd--)
40         a[rightEnd] = tmpArray[rightEnd];
41 }
42 int main()
43 {
44     vector<int> ivec;
45     ivec.push_back(1);
46     ivec.push_back(9);
47     ivec.push_back(2);
48     ivec.push_back(10);
49     ivec.push_back(3);
50     ivec.push_back(11);
51     ivec.push_back(4);
52     ivec.push_back(12);
53     ivec.push_back(5);
54     ivec.push_back(13);
55     ivec.push_back(6);
56     ivec.push_back(14);
57     ivec.push_back(7);
58     ivec.push_back(15);
59     ivec.push_back(8);
60     ivec.push_back(16);
61     mergeSort(ivec);
62     for(int j = 0;j<ivec.size();j++)
63         cout<<ivec[j]<<endl;
64     return 0;
65 }``````

### 汪纬

#### 暂无文章

CREATE_17

5
0
CSS盒子模型

CSS盒子模型 组成： content --> padding --> border --> margin 像现实生活中的快递： 物品 --> 填充物 --> 包装盒 --> 盒子与盒子之间的间距 content ：width、height组成的 内容区域 padd......

studywin

7
0

locbytes

8
0
jquery 添加和删除节点

8
0

ZeroBit

10
0