Algorithm-Sorting

Sorting Algorithms

Introduction

Sorting is of great importance when learning algorithms for beginners! This blog shows the basic principles of several sorting algorithms and its implementation of C++.

Merge Sort

Quick Sort

Heap Sort

The basic principle of Heap sort is from the data structure: Heap, where all nodes (or elements) are formed in order (ascending or descending). Heap maintain this properties, so every time you pop out the top element in the heap (or priority queue), it must be the biggest or the smallest element with highest priority in the queue.

For more information, you can search Binary Heap

We can use several ways to construct a heap (Binary heap, leftist heap, skew heap, binomial heap), the code demonstrated below shows the basic implementation of Binary Heap:

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
#include <iostream>
#include <cstdlib>
#include <ctime>

template <class T>
class priorityQueue {
private:
size_t currentsize;
T* array;
size_t maxsize;

void doublespace() {
T* tmp = array;
maxsize *= 2;
array = new T[maxsize];
for (size_t i = 0; i <= currentsize; ++i) {
array[i] = tmp[i];
}
delete[] tmp;
}

void buildheap() {
for (size_t i = currentsize / 2; i >= 1; --i) {
percolateDown(i);
}
}

void percolateDown(size_t hole) {
size_t child = 0;
T tmp = array[hole];

while (hole * 2 <= currentsize) {
child = hole * 2;
if (child < currentsize && array[child + 1] < array[child]) {
++child;
}

if (array[child] < tmp) {
array[hole] = array[child];
hole = child;
} else {
break;
}
}
array[hole] = tmp;
}

public:
priorityQueue(size_t capacity = 100) : currentsize(0), maxsize(capacity) {
array = new T[maxsize];
}

~priorityQueue() {
delete[] array;
}

priorityQueue(const T* data, size_t size) : currentsize(size), maxsize(size + 10) {
array = new T[maxsize];
for (size_t i = 0; i < size; ++i) {
array[i + 1] = data[i];
}
buildheap();
}

bool empty() const {
return currentsize == 0;
}

void enQueue(const T& x) {
if (currentsize == maxsize - 1) {
doublespace();
}

size_t hole = ++currentsize;
while (hole > 1 && x < array[hole / 2]) {
array[hole] = array[hole / 2];
hole /= 2;
}
array[hole] = x;
}

T deQueue() {
if (empty()) {
throw std::underflow_error("Priority queue is empty");
}

T minItem = array[1];
array[1] = array[currentsize--];
percolateDown(1);
return minItem;
}

T getHead() const {
if (empty()) {
throw std::underflow_error("Priority queue is empty");
}
return array[1];
}
};

template <typename T>
void pqsort(T* unsorted, T* sorted, size_t size) {
priorityQueue<T> pq(unsorted, size);
for (size_t i = 0; i < size; ++i) {
sorted[i] = pq.deQueue();
}
}

template <typename T>
void heapSort(T*& arr, size_t size) {
//default for ascending order
priorityQueue<T> pq(arr, size);
T* sorted = new T [size];
for(size_t i = 0; i < size; ++i) {
sorted[i] = pq.deQueue();
}
delete[] arr;
arr = sorted;
}

int main() {
int size = 50;
int *arr = new int [size];

//using the random seed
std::srand(static_cast<int>(std::time(nullptr)));
for(int i = 0; i < size; i++){
arr[i] = std::rand();
}

std::cout << "Before the sort" << std::endl;

for(int i = 0; i < size; i++){
std::cout << arr[i] << std::endl;
}

heapSort(arr, size);

std::cout << "After the sort" << std::endl;
for(int i = 0; i < size; ++i){
std::cout << arr[i] << std::endl;
}
return 0;
}

Counting Sort


Algorithm-Sorting
https://xiyuanyang-code.github.io/posts/Algorithm-Sorting/
Author
Xiyuan Yang
Posted on
March 29, 2025
Updated on
March 29, 2025
Licensed under