11 9 17 5 12
5 9 17 11 12
5 9 17 11 12
5 9 11 17 12
5 9 11 12 17
Time now
Time before;
selection_sort(v);
Time after;
cout << "Elapsed time = " << after.seconds_from(before)
<< " seconds\n";
n |
seconds |
8000 |
3 |
16000 |
13 |
32000 |
55 |
64000 |
220 |
n (1/2) n2 (5/2) n - 3 1000 500 000 2 497 2000 2 000 000 4 997
void merge_sort(vector<int>& a, int from, int to)
{ if (from == to) return;
int mid = (from + to) / 2;
/* sort the first and second half */
merge_sort(a, from, mid);
merge_sort(a, mid + 1, to);
merge(a, from, mid, to);
}
Merge Sort (mergsort.cpp)
n |
seconds |
160000 |
1 |
320000 |
2 |
640000 |
4 |
1280000 |
9 |
v[5]
|
115
|