Professional Documents
Culture Documents
Lab3 Report-7 PDF
Lab3 Report-7 PDF
LABORATORY 3 REPORT
SORTING ALGORITHMS ANALYSIS
Lecturer in charge:
Ir. NGUYEN THANH PHUONG
Ir. BUI HUY THONG
Contents
1 Introduction 2
2 Algorithms 2
2.1 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 Shaker Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Shell Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.6 Heap Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.7 Merge Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.8 Quick Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.9 Counting Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.10 Radix Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.11 Flash Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3 Experiments 20
3.1 Result tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2 Line graphs of running time . . . . . . . . . . . . . . . . . . . . . 22
3.3 Bar charts of comparisons . . . . . . . . . . . . . . . . . . . . . . 25
3.4 Overall comment . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5 References 28
1
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
1 Introduction
Algorithm plays a vitally important in computer science. It provides consis-
tency in solving similar tasks with a predictable and desirable outcome. Among
algorithms, sorting algorithms are regarded as the gateway to the world of al-
gorithms in programming. Various sorting algorithms require demand about
considering about the feasibility and effectiveness of each algorithm. In order to
address that requirements, this scientific work studies on sorting algorithms in
terms of:
• Time complexity
• Space complexity
2 Algorithms
2.1 Selection Sort
1. Idea: In computer science, selection sort is an in-place comparison sort-
ing algorithm. Idea of this algorithm has derived from separating input
list into twos part: a left part of sorted items and the rest part containing
unsorted items. By finding the smallest(greatest or depending specific con-
dition), pushing back the left sorted sublist, repeat this process with the
right rest part, sorted list will be built up from left to right.
2. Pseudo Code:
2
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
• Time complexity
– Worst case: O(n2 )
When we sort in ascending (descending) order and the list is in
descending(ascending) order , the worst case occurs.
– Best case: O(n2 )
It occurs when the list is sorted
– Average case: O(n2 )
when elements in the list order randomly
• Space complexity(auxiliary)
O(1) because two extra variables minIndex and temp(swap) are used.
• Stability: No.
4. Application
5. Enhancement
• We can use min max selection sort to ensure that in each iteration we
swap a[i] with max or min elements in the rest unsorted list and use
two pointer technique to reduce range need sorting.
• Heap sort (in 2.11 ).
3
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
4. Application
• A list with small number of elements.
• The list nearly sorted (Just have a few elements placed in wrong posi-
tion)
• Auxiliary memory is limited
5. Enhancement
• Binary insertion sort: using binary search to find the correct position
of elements quickly and shift all elements to the right position.
• Shell sort
4
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
• Time complexity
– Worst case: O(n2 )
– Best case: O(n2 )
– Average case: O(n2 )
You can see here are two loops so time complexity is O(n2 )
• Space complexity(auxiliary)
O(1) because a few extra variables are used.
• Stability: Yes.
4. Application
5. Enhancement
5
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
• Time complexity
– Worst case: O(n2 )
– Best case: O(n)
When this list is sorted, the best case occurs
– Average case: O(n2 )
– if every element is at a position that differs by at most k (k ≥
1) from the position it is going to end up in, the complexity of
cocktail shaker sort becomes O(kn).
• Space complexity(auxiliary)
O(1) because a few extra variables are used.
• Stability: Yes
4. Application
6
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
• Time complexity
– Worst case: O(n2 )
Or O(n*log 2 n) with Pratt gap sequence
– Best case: O(n*log(n))
It occurs when the list is sorted. The total number of comparisons
for each gap is equal to the size of the list.
– Average case: O(n*log(n))
It is around O(n1.25 ).
– Besides, depending on the selected sequence we have different com-
plexity computations.
• Space complexity(auxiliary)
Space complexity is O(1) because a few extra variables are used.
• Stability: No
4. Application
• Shellsort performs more operations and has higher cache miss ratio
than quicksort. However, since it can be implemented using little code
and does not use the call stack, some implementations of the qsort
function in the C standard library targeted at embedded systems use
it instead of quicksort. Shellsort is, for example, used in the uClibc
7
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
library. For similar reasons, in the past, Shellsort was used in the
Linux kernel
• Shellsort can also serve as a sub-algorithm of introspective sort, to sort
short subarrays and to prevent a slowdown when the recursion depth
exceeds a given limit. This principle is employed, for instance, in the
bzip2 compressor
• When recursion exceeds a particular limit, we use shell sort.
5. Enhancement
• There are many recommends about the way we choose gaps. Latest
suggestion is Marcin Ciura’s gap sequence in 2001.
1: procedure heapify(a[],root,size)
2: j = root ∗ 2
3: key = a[root]
4: while j < size do
5: if j < size − 1 and a[j + 1] > a[j] then
6: j ←j+1
7: end if
8: if a[i] ≤ key then break
9: end if
10: swap(a[j], a[root])
11: root ← j
12: j ←j∗2
13: end while
14: end procedure
15: procedure heap sort(a[],size)
16: for i = size/2 downto 0 do
17: heapify(a,i,size)
18: end for
19: for i = size − 1 downto 0 do
20: swap(a[0], a[i])
21: heapify(a,0,i)
22: end for
23: end procedure
8
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
• Time complexity
– Worst case: O(n*log(n))
Because the height of heap tree is log(n), every time we get the
root node and put it in the sorted list, we have to rebuild the
max(min)-heap tree with path has length equal to height of tree.
– Best case: O(n)
It occurs when all elements in the list is identical.
– Average case: O(n*log(n))
• Space complexity(auxiliary)
Space complexity is O(1) because a few extra variables are used.
• Stability: No
4. Application
9
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
10
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
4. Application
• Merge Sort is useful for sorting linked lists in O(N log N) time. In the
case of linked lists, the case is different mainly due to the difference in
memory allocation of arrays and linked lists. Unlike arrays, linked list
nodes may not be adjacent in memory.
• Drawbacks: Slower compared to the other sort algorithms in smaller
input, require O(n) addition memory space, loop over all list even if it
is sorted
5. Enhancement
11
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
2. Pseudo Code:
1: procedure partition(a[],size)
2: pivot = a[size/2]
3: lef t = 0
4: right = size − 1
5: while lef t < right do
6: while a[lef t] < pivot do
7: lef t ← lef t + 1
8: end while
9: while a[right] > pivot do
10: right ← right − 1
11: end while
12: if lef t < right then
13: swap(a[lef t], a[right])
14: right ← right − 1
15: lef t ← lef t + 1
16: end if
17: end while
18: return size/2
19: end procedure
20: procedure sort(a[],size)
21: if size ≤ 1 then return
22: end if
23: pos = parition(a, size)
24: sort(a,pos)
25: sort(a + pos + 1, size - pos - 1)
26: end procedure
12
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
• Time complexity
– Worst case: O(n2 )
When pivot always the minimum(maximum) element in the sort-
ing list, the worst case occurs
– Best case: O(n*log(n))
The best-case occurs when the pivot element is the middle element
or near to the middle element of sorted list afterwards.
– Average case: O(n*log(n))
when elements in the list order randomly
• Space complexity(auxiliary)
Space complexity is O(n), sum of partition position variables returned
in sort function .
• Stability: No
4. Application
• The quick sort is in place as it does not require any additional storage.
• Numerical computations and in scientific research, for accuracy in
calculations most of the efficiently developed algorithm uses priority
queue and quick sort is used for sorting
• It is used to implement primitive type methods.
• The list is sorted.
5. Enhancement
• . Normally tail recursion is not preferable, but in this case tail re-
cursion is combined with the notion of solving the smallest partition
first, which drastically improves the space complexity to O(logn) from
O(n).
• Choose pivot randomly in range [left - right]
• There are many variants of quick sort with with distinct advantages:
Lomuto’s Partitioning, Hoare’s Partitioning, Three-way Partitioning,
Dutch National Flag, Multi-pivot quicksort, External quicksort, Three-
way radix quicksort, Quick radix sort, BlockQuicksort, and so on.
13
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
2. Pseudo Code:
• Time complexity
– Worst case: O(k)
With k = largest element - smallest element + 1(range of ele-
ments). When largest is significantly larger than others, k ¿¿ n,
the worst case occurs.
– Best case: O(n)
The best case occurs when the all elements have the same key so
k = 1, thus the total time complexity is O(n+1) i.e O(n) which is
linear.
– Average case: O(n+k)
As you can see, in this algorithm, the first loop we iterate n times,
and the second loop we use prefix sum and loop in k times, so if
neither value of n nor value of k are dominant, time complexity of
this algorithm O(n+k).
• Space complexity(auxiliary)
Space complexity is O(n+k), we need a list count ( with size = range
of elements) and a list res(with size = the elements number of list).
• Stability: Yes
14
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
4. Application
• If the range of input data is not much bigger than the number of
objects to be sorted, counting sort is efficient.
• Counting sort is very efficient in the cases where range is comparable
to number of input elements as it performs sorting in linear time and
can be a advantage in such cases over other sorting algorithms such as
quick sort.
• Since counting sort is suitable for sorting numbers that belong to a
well-defined, finite and small range, it can be used as a subprogram in
other sorting algorithms like radix sort which can be used for sorting
numbers having a large range
5. Enhancement
2. Pseudo Code:
15
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
16
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
17
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
18
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
m is the number of classes which should be 0.43*n. And we have the formula
to define the class of the element ai :
(m − 1) (ai − mina )
kai = + 1.
maxa − mina
• Time complexity
– Worst case: O(n2 )
When the maximum element has a large difference from the other
elements. So just a few classes(buckets) is used and insertion sort
with time complexity O(n2 ) lead time complexity in this case is
O(n2 )
– Best case: O(n)
When data set is balanced, most of the classes( buckets) have
elements, the best case occurs.
– Average case: O(n)
• Space complexity(auxiliary)
Space complexity is O(m), this algorithm doesn’t need auxiliary array.
• Stability: No. If stability is required, we can use an array to get
temporary output, and instead of step permutation, we map count
array to place elements in right positions. But space complexity in
this case is O(m + n).
4. Application
5. Enhancement
19
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
3 Experiments
In this experiment, I will test on data sizes set: 10,000, 30,000, 50,000, 100,000,
300,000, and 500,000 elements in term of running time(ms) and comparisons fre-
quency.
The hardware I use to measure the experiment is below:
20
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
21
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
22
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
- In term of randomize order input, there are four algorithms which are com-
pletely different from all other algorithms. In particular, Bubble sort runs the
slowest (because it takes time for swapping action). Shaker sort, although im-
proved over Bubble sort, is still very slow and then selection sort and insertion
sort are both time complexity O(n2 ) algorithms. Time complexity O(nlogn) and
O(n) algorithms have negligible running time when compared to the above 4
algorithms .
- In term of sorted order input, two algorithms, bubble sort and selection sort,
have a much longer running time than the other algorithms. Selection sort is
”stably higher” with selection of elements moved. Although in best case, Bubble
sorted is still very low when compared to others algorithms. Besides, sorted order
input is also the best case of insertion sort and shaker sort algorithms with time
complexity O(n). Time complexity O(nlogn) and O(n) algorithms have negligible
running time when compared to the bubble sort and selection sort.
23
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
- In term of reversed order input, there are four algorithms which are
completely different from all other algorithms. The worst case of selection sort,
bubble sort, shaker sort, insertion sort occurs with time complexity O(n2 ).
In general, the chart of the running time with nearly sorted order input is
quite similar to the one with sorted order input.
24
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
- In term of randomized order input, there are four algorithms which are com-
pletely different from all other algorithms. Selection sort and Bubble sort have
largest the number of comparisons because they have n2 of base comparisons in
loops. Shaker sort and Insertion sort are optimized a bit but it they all are not
significant with other algorithms having fewer comparisons.
- The sorted order input is the best case of Insertion sort and Shaker sort with
time complexity O(n) so their number of comparisons are much smaller than
Selection sort and Bubble sort.
25
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
- The reversed order input is the worst case of Insertion sort and Shaker sort
with time complexity O(n2 ) so their number of comparisons are increase, but the
number of insertion sort is just about a half of three rest O(n2 ) time complexity
algorithms.
In general, the bar of the running time with nearly sorted order input is kind of
similar to the one with sorted order input.
3.4 Overall comment
- Through testing with data sets, we can objectively evaluate the effective-
ness of the algorithms. From my point of views, Flash sort the the best sorting
algorithms with ”flash” speed with time complexity O(n) in most cases, whereas
the Bubble is always the worst one with comparisons frequency and time run-
ning with overwhelming numbers. In addition, insertion sort is fairly good when
compared average time complexity O(n2 ).
26
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
• Stable algorithms:
– Selection sort. With fixed cost for two loops, selection sort is a sta-
ble algorithm notwithstanding the giant number of time running and
comparisons.
– Shell sort. Not use recursion and sort quickly in various input order
cases.
– Heap sort. Application of heap data structure, the stability is shown
with time complexity O(nlogn) in every case.
– Merge sort. Divide and conquer approach make this algorithms sta-
bility in all case. However, it need additional memory with O(n).
– Radix sort. Sorting the elements by processing them in multiple passes,
digit by digit, the overall cost is O(d*n).
– Flash sort. This distribution sorting algorithm shows linear computa-
tional complexity O(n) for uniformly distributed data sets, that’s why
i think it’s stable and effective algorithm.
• Unstable algorithms:
– Insertion sort. It has shown inefficiency when the data is reversed and
randomized. However, by taking advantage of the available sorting
of the data, I thinks insertion sort is adequate algorithm and easy to
implement, well-suited for sorting with small datasets.
– Bubble sort. Substantial difference regarding running time between
data order randomized, reversed and sorted, nearly sorted make this
algorithms is truly not stable. Besides, the time running is too low to
apply in practice.
– Shaker sort. Pretty similar to Bubble sort, but it is surpass in sorted
and nearly sorted input order case with time complexity O(n).
– Quick sort. Although it has quite good performance in this experiment,
If pivot chosen is median of list, the program become disastrous with
time running and stack memory consumed.
– Counting sort. As well as Quick sort, if the difference of the largest
and smallest elements is much more than size of list, it will cost a lot
of additional memory and the time running will be very low.
– Each sort is a class, and the unique class Sort implements all sort-
ing algorithm including two function: calculating running time and
counting comparison frequency in specified algorithms.
– Class Helper for small tasks, such as read, write file, generate data,
swap items.
27
Lab3: Sorting Algorithms 21120462 - Do Khai Hung
• The library Map. Key is command input string and value is equivalent
integer value.
5 References
[1] Selection sort - Wikipedia https://en.wikipedia.org/wiki/Selection_
sort
28