Final
Final
Lab 3 Project
GROUP 07
21127092 - Trần Hoàng Lâm
21127239 - Lê Xuân Đạt
21127157 - Dương Phước Sang
21127328 - Cao Tuấn Kiệt
Table of contents
1 Introduction 1
2 Algorithm presentation 1
2.1 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.2 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 Shaker Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.5 Shell Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.6 Heap Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.7 Merge Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.8 Quick Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.9 Counting Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.10 Radix Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.11 Flash Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 Experimental 10
3.1 Randomized data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2 Nearly sorted data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.3 Sorted data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.4 Reverse sorted data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 References 18
List of tables
1 Randomize Order Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2 Randomize Order Line Graph . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Randomize Order Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4 Nearly Sorted Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5 Nearly Sorted Line Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
6 Nearly Sorted Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7 Sorted Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
8 Sorted Line Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
9 Sorted Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
10 Reverse Sorted Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
11 Reverse Sorted Line Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
12 Reverse Sorted Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Data Structures and Algorithms
1 Introduction
Sorting is the process of arranging data into meaningful order so that you can
analyze it more effectively. Sorting algorithm is a set of instructions helping us sort a
specific data. This report will analysis each sorting algorithm of 11 algorithms such as:
Selection Sort, Insertion Sort, Bubble Sort, Shaker Sort, Shell Sort, Heap Sort, Merge
Sort, Quick Sort, Counting Sort, Radix Sort, and Flash Sort. Data collected from
experiments with each algorithms for a given data order, data size will be collected
and there are graphs for each data order as well. As a result, we will find the algorithm
which is the best in each case.
2 Algorithm presentation
2.1 Selection Sort
• Idea:
The list is divided into two sublists, sorted and unsorted, by an imaginary wall.
Find the smallest element from the unsorted part and swap it with the element at
the beginning of the unsorted data. After each selection and swapping, the size of
the sorted region grows by 1 and the size of the unsorted region shrinks by 1.
• Step-by-step description:
• Complexity evaluations:
n(n−1)
∎ The number of comparisons: (n − 1) + (n − 2) + ... + 1 = 2
The inner loop executes the size of the unsorted part minus 1, and in each
iteration, there is one key comparison.
1
Data Structures and Algorithms
• Step-by-step description:
• Complexity evaluations:
n(n−1)
∎ The number of comparisons: (n − 1) + (n − 2) + ... + 1 = 2
The inner loop executes the size of the sorted part, and in each iteration, there
is one key comparison.
∎ The number of assignments: n(n2−1) + 2(n − 1)
The inner loop moves data items at most the same number of times for com-
parisons. The outer loop moves data items twice per iteration.
2
Data Structures and Algorithms
• Step-by-step description:
• Complexity evaluations:
n(n−1)
∎ The number of comparisons: (n − 1) + (n − 2) + ... + 1 = 2
The inner loop executes the size of the unsorted part minus 1, and in each
iteration, there is one key comparison.
∎ The number of assignments: the same as above.
3
Data Structures and Algorithms
• Step-by-step description:
• Complexity evaluations:
4
Data Structures and Algorithms
• Step-by-step description:
• Complexity evaluations:
5
Data Structures and Algorithms
array and repeatedly move the largest element in the heap to the end of the array.
Elements are moved from the heap in descending order and placed into sequentially
decreasing positions in the array.
• Step-by-step description:
• Complexity evaluations:
• Step-by-step description:
– Divide the unsorted list into n sublists, each containing one element (a list of
one element is considered sorted).
– Repeatedly merge sublists to produce new sorted sublists until there is only
one sublist remaining. This will be the sorted list.
• Complexity evaluations:
6
Data Structures and Algorithms
• Variants/improvements:
– In-place merge sort: this algorithm doesn’t use extra memory while functioning
just like the above one.
– 3-way merge sort: instead of dividing the array into 2 parts like normal merge
sort, this one divides the array into 3 parts, and thus the complexity os
O(nlog3n).
• Step-by-step description:
• Complexity evaluations:
7
Data Structures and Algorithms
• Variants/improvements:
– 3-way quick sort: based on the Dutch National Flag algorithm, this sort divide
the array into 3 parts: elements smaller than pivot, elements equal to pivot
and elements larger than pivot.
– Quick sort with bubble sort and Quick sort with insertion sort: the other
sorting algorithm will be used under a specific condition of the number of
elements in a partition.
– Enhanced quick sort: the pivot is now moved to the end of the array to make
the sorting more efficient.
• Step-by-step description:
– Compute the frequency of each of those values and store them in array: Create
an array to store the frequencies of elements, the array’s size should cover all
possible elements in the range [0, u], which u is the value of maximum number.
f[i] return the frequency of key i, which is an element in the array.
– Distribution counting: add up sums of frequencies f[i] = f[i] + f[i-1], where
f[i-1] is the last position of (i-1) if it exists. If (i-1) does not exist, the position
of the smaller existing item will be maintained.
– Distribute values to their final positions (The distribution values decide the
proper positions for the last occurrences of their elements in the sorted array.)
– Copy array b[ ] back to array a[ ]: a[1..n] = b[1..n].
• Complexity evaluations:
8
Data Structures and Algorithms
• Step-by-step description:
• Complexity evaluations:
9
Data Structures and Algorithms
• Step-by-step description:
– Test 1. Find the minimum value and the index of the maximum value in the
array. If the value in the max index equals to the min value, it means that the
array has only one kind of number and thus is already sorted.
– Test 2. Declare m - the number of buckets. There are many ways to calculate
m, but in this algorithm, it equal to int(0.45 * n), with n is the length of the
given array. Then create an array with the size of m.
– Test 3. Make a pass over the given array and count the number of elements
in each bucket.
– Test 4. Change the count number in each bucket into a preflix sum, where
bucket[i] = bucket[i] + bucket[i-1].
– Test 5. Apply a formula to rearrange the given array, in which every element
is in its correct bucket.
– Test 6. With those below steps, the array is nearly sorted. Thus, insertion
sort is used to do the rest of the work and return the sorted array.
• Complexity evaluations:
3 Experimental
3.1 Randomized data
• TABLE :
10
Data Structures and Algorithms
• LINE GRAPH :
11
Data Structures and Algorithms
• BAR CHART :
• LINE GRAPH :
12
Data Structures and Algorithms
• BAR CHART :
13
Data Structures and Algorithms
• LINE GRAPH :
14
Data Structures and Algorithms
• BAR CHART :
• LINE GRAPH :
15
Data Structures and Algorithms
• BAR CHART :
16
Data Structures and Algorithms
• ProjectLab3.cpp: the main .cpp file, used to run the program and store the input
arguments.
• File.cpp and File.h: provide functions to work with file (create a file, write data
to a file, read data from a file).
• Open Command Prompt and locate the folder containing all those .cpp and .h
files.
• An a.exe file should be created, then in Command Prompt, run that file together
with an appropriate command line.ialize an array in a total of four different modes
(random, sorted, nearly sorted and reversed).
17
Data Structures and Algorithms
• The <time.h> library (used in DataGenerator.cpp): this library was used to create
random factors while creating an array (eg. random array or nearly sorted array).
• At the beginning of the program, a random array with the size of 10000 will always
be created and written down to input.txt.
• When the time counter starts, there is a very small amount of time belonging to
the switch case in RunAlgorithm function (CommandLine.cpp). But overall, that
duration of time is insignificant and thus won’t affect the final result.
• While checking the input size to distinguish between command 3 and command 1
(see checkIsCommand function in CommandLine.cpp), if the input size is larger
than 6 characters or the number converted is bigger than 1000000 or smaller than
1, it will be treated as a file name for command 1.
5 References
• Flash sort source code: https ∶ //github.com/HaiDuc0147/sortingAlgorithm/blob/main
• Quick sort implementation and source code: Pr. Nguyen Thanh Phuong (2020)
“Lecture notes of CS163 – Data structures” University of Science - Vietnam Na-
tional University HCM.
• All other sorting algorithms’ source code: Lecturer Nguyen Ngoc Thao’s lecture
notes: https ∶ //drive.google.com/drive/f olders/16IIrM 22−ubN enD4pjprCDGoa7Bn
18