Sorting Algorithm Analysis

Among the previous algorithms, quick sort performs worst in the worst case, while the others are always $O(n \log n)$. However, quick sort is known to be the fastest in practice for most inputs.

Algorithm Average speed Worst speed Extra memory usage
Quick sort Fastest ($O(n \log n)$) Slowest ($O(n^2)$) None ($O(1)$)
Merge sort $O(n \log n)$ $O(n \log n)$ Auxiliary array ($O(n)$)
Heap sort $O(n \log n)$ $O(n \log n)$ None ($O(1)$)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <math.h>
#include <time.h>
#include <vector>
#include <algorithm>
#include <fstream>
#include <chrono>

void swap(int& a, int& b){
    int c = a;
    a = b;
    b = c;
}

void downHeap(int lst[], int length, int idx){
    if (2 * idx + 2 < length){
        if (lst[idx] < lst[2 * idx + 1]){
            if(lst[2 * idx + 1] < lst[2 * idx + 2]){
                swap(lst[idx], lst[2 * idx + 2]);
                downHeap(lst, length, 2 * idx + 2);
            }
            else{
                swap(lst[idx], lst[2 * idx + 1]);
                downHeap(lst, length, 2 * idx + 1);
            }
        }
        else if (lst[idx] < lst[2 * idx + 2]){
            swap(lst[idx], lst[2 * idx + 2]);
            downHeap(lst,length, 2 * idx + 2);
        }
    }
    else if (2 * idx + 1 < length){
        if (lst[idx] < lst[2 * idx + 1]){
            swap(lst[idx], lst[2 * idx + 1]);
            downHeap(lst, length, 2 * idx + 1);
        }
    }
}

void listToHeap(int lst[], int length){
    int height = 0;
    int sumE   = 0;
    while (sumE < length){
        height += 1;
        sumE   *= 2;
        sumE   += 1;
    }
    for(int i = height - 2; i > -1; i -= 1){
        for(int j = pow(2,i) -1; j < pow(2,i+1) -1; j++){
            downHeap(lst, length, j);
        }
    }
}

void heapSort(int lst[], int length){
    listToHeap(lst, length);
    for(int i = length-1; i > 0; --i){
        swap(lst[0], lst[i]);
        downHeap(lst, i, 0);
    }
}

int partition(int lst[], int fromIdx, int toIdx){
    int selectPoint = (toIdx + fromIdx)/2;
    swap(lst[fromIdx], lst[selectPoint]);
    int criteria = lst[fromIdx];
    int lIdx = fromIdx;
    int rIdx = toIdx - 1;
    while(lIdx < rIdx){
        while(lIdx < rIdx){
            if(lst[lIdx] > criteria)
                break;
            lIdx += 1;
        }
        if(lIdx == rIdx)
            break;
        while (lIdx < rIdx){
            if (lst[rIdx] <= criteria)
                break;
            rIdx -= 1;
        }
        swap(lst[lIdx], lst[rIdx]);
    }
    if (lst[lIdx] > lst[fromIdx])
        lIdx -= 1;
    swap(lst[lIdx], lst[fromIdx]);
    return lIdx;
}

void _qsort(int lst[], int fromIdx, int toIdx){
    if(toIdx - fromIdx <= 1)
        return;
    int mid = partition(lst, fromIdx, toIdx);
    _qsort(lst, fromIdx, mid);
    _qsort(lst, mid + 1, toIdx);
}
    
void qsort(int lst[], int length){
    _qsort(lst, 0, length);
}

void _mergeSort(int lst[], int fromIdx, int toIdx){
    if (toIdx - fromIdx <= 1)
        return;
    int mid = (toIdx + fromIdx)/2;
    _mergeSort(lst, fromIdx, mid);
    _mergeSort(lst, mid, toIdx);

    int auxList[toIdx - fromIdx];
    int idx1 = fromIdx;
    int idx2 = mid;
    for(int i = 0; i < (toIdx - fromIdx); ++i){
        if (idx2 >= toIdx || idx1 < mid && lst[idx1] <= lst[idx2]){
            auxList[i] = lst[idx1];
            idx1 += 1;
        }
        else if (idx1 >= mid || idx2 < toIdx && lst[idx2] < lst[idx1]){
            auxList[i] = lst[idx2];
            idx2 += 1;
        }
        else{
            std::cout << "Invalid situation" << std::endl;
            exit(1);
        }
    }
    for(int i = 0; i < (toIdx - fromIdx); ++i){
        lst[i + fromIdx] = auxList[i];
    }
}
        
void mergeSort(int lst[], int length){
    return _mergeSort(lst, 0, length);
}


int main(){
    using namespace std;

    srand(time(NULL));
    std::chrono::duration<double> mergeTime;
    std::chrono::duration<double> qTime;
    std::chrono::duration<double> heapTime;
    
    ofstream qDat("qSort.dat"), mDat("mergeSort.dat"), hDat("heapSort.dat");
    for(int i = 0; i < 1000; ++i){
        int length = i * 9 + 100;
        int lst1[length];
        int lst2[length];
        for(int j = 0; j < length; ++j)
            lst1[j] = rand()%10000;

        for(int j = 0; j < length; ++j)
            lst2[j] = lst1[j];
        auto from = std::chrono::system_clock::now();
        mergeSort(lst2, length);
        auto to = std::chrono::system_clock::now();
        mergeTime = (to - from);
        
        for(int j = 0; j < length; ++j)
            lst2[j] = lst1[j];
        from = std::chrono::system_clock::now();
        qsort(lst2, length);
        to = std::chrono::system_clock::now();
        qTime = (to - from);
        
        for(int j = 0; j < length; ++j)
            lst2[j] = lst1[j];
        from = std::chrono::system_clock::now();
        heapSort(lst2, length);
        to = std::chrono::system_clock::now();
        heapTime = (to - from);
        qDat << length << " " << qTime.count()     << "\n";
        hDat << length << " " << heapTime.count()  << "\n";
        mDat << length << " " << mergeTime.count() << "\n";
    }
    qDat.close();
    mDat.close();
    hDat.close();
}

One notable result is that the worst case of quick sort is severe enough to be interesting. (The worst case for quick sort occurs when all elements have the same value.)