voidSelectSort(int arr[], int num) { int i, j, Mindex; for (i = 0; i < num; i++) { Mindex = i; for (j = i + 1; j < num; j++) { if (arr[j] < arr[Mindex]) Mindex = j; }
//将有二个有序数列a[first...mid]和a[mid+1...last]合并。 voidmergearray(int a[], int first, int mid, int last) { int i = first, j = mid + 1; int m = mid, n = last; vector<int> temp; while (i <= m && j <= n) { if (a[i] <= a[j]) temp.push_back(a[i++]); else temp.push_back(a[j++]); }
while (i <= m) temp.push_back(a[i++]);
while (j <= n) temp.push_back(a[j++]);
for (i = 0; i < temp.size(); i++) a[first + i] = temp[i]; } voidmergesort(int a[], int first, int last) { if (first < last) { int mid = (first + last) / 2; mergesort(a, first, mid); //左边有序 mergesort(a, mid + 1, last); //右边有序 mergearray(a, first, mid, last); //再将二个有序数列合并 } }
boolMergeSort(int a[], int n) { // int *p = new int[n]; // if (p == NULL) // return false; mergesort(a, 0, n - 1); // delete[] p; returntrue; }