Project
Project
Attach this coversheet as the cover of your submission. All sections must be completed.
This is your submission receipt, the only accepted evidence that you have submitted your work. After this is
stamped by the appointed staff & filled in, cut along the dotted lines above & retain this for your record.
PROJECT QUESTIONS
Faster Sorts
Selection, Insertion and Bubble sorts are not very efficient for large arrays or lists.
There are more efficient techniques to sort large lists. This include Quicksort,
Mergesort and Heapsort.
With the aid of diagrams and algorithm, explain how data values are sorted in
each Quicksort, Mergesort and Heapsort. Provide sufficient test data.
2
Quicksort
Quicksort is an extremely efficient sorting technique that divides a large data array into
smaller ones. A huge array is divided into two arrays, one of which contains values smaller
than the provided value, say pivot, on which the partition is based, and the other of which
contains values greater than the pivot value.
Quicksort partitions an array and then calls itself recursively twice to sort the two resulting
subarrays:
The process starts by selecting one element (known as the pivot) from the list; this can be
any element. A pivot can be:
Any element at random
The first or last element
Middle element
The last element is chosen as a pivot. The partition index will be set to the first element of
the array. From the beginning, compare each element to the pivot; if the element is less than
the pivot, swap it with the element at the partition index and increment the partition index. At
the end, we will get our partitioned array in which all elements smaller than pivot will appear
to the left of pivot and elements greater than pivot to its right in the array.
3
Quick Sort Function Algorithm
The sub-arrays are rearranged in a certain order using the partition method. You will find
various ways to partition. Here we will see one of the most used methods.
4
Mergesort
Merge sort is a sorting technique based on divide and conquer technique. With worst-case
time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted
manner.
We know that merge sort first divides the whole array iteratively into equal halves unless the
atomic values are achieved. We see here that an array of 8 items is divided into two arrays
of size 4.
This does not change the sequence of appearance of items in the original. Now we divide
these two arrays into halves.
We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note
the color codes given to these lists.
We first compare the element for each list and then combine them into another list in a
sorted manner. We see that 12 and 30 are in sorted positions. We compare 25 and 10 and
in the target list of 2 values we put 10 first, followed by 25. We change the order of 18 and
38 whereas 44 and 42 are placed sequentially.
5
In the next iteration of the combining phase, we compare lists of two data values, and merge
them into a list of found data values placing all in a sorted order.
After the final merging, the final list will look like this –
Mergesort – Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more be divided. By
definition, if it is only one element in the list, it is sorted. Then, merge sort combines the
smaller sorted lists keeping the new list sorted too.
In the following algorithm, arr is the given array, beg is the starting element, and end is the
last element of the array:
END MERGE_SOR
The important part of the merge sort is the MERGE function. This function performs the
merging of two sorted sub-arrays that are A[beg…mid] and A[mid+1…end], to build one
sorted array A[beg…end]. So, the inputs of the MERGE function are A[], beg, mid, and end.
6
The implementation of the MERGE function is given as follows:
7
Heapsort
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It
is similar to selection sort where we first find the minimum element and place the minimum
element at the beginning. We repeat the same process for the remaining elements.
Type Properties
Shape property Heap is always a complete binary tree which means that all the levels
of a tree are fully filled. There should not be a node which has only one
child. Every node except leaves should have two children then only a
heap is called as a complete binary tree.
Heap property All nodes are either greater than or equal to or less than or equal to
each of its children. This means if the parent node is greater than the
child node it is called as a max heap. Whereas if the parent node is
lesser than the child node it is called as a min heap
The first step now is to create a heap from the array elements. For this consider a binary tree
that can be built from the array elements the zeroth element would be the root element and
the left and right child of any element would be valued at i, 2*i+1, and 2*i + 2th index
respectively.
8
The above diagram depicts the binary tree version of the array.
We build the heap from the above binary tree and is shown below:
Now the root 90 is moved to the last location by exchanging it with 3. Finally, 90 is eliminated
from the heap by reducing the maximum index value of the array by 1. The balance
elements are then rearranged into the heap. The heap and array look like as shown in the
below diagram:
9
Similarly, when the current root 57 is exchanged with 2, and eliminated from the heap by
reducing the maximum index value of the array by 1. The balance elements are then
rearranged into the heap. The heap and array look like as shown in the below diagram:
Following the same approach, the following phases are followed until the fully sorted array is
achieved.
10
11
12
Heapsort – Algorithm
Heapsort is a popular and efficient sorting algorithm. The concept of heap sort is to eliminate
the elements one by one from the heap part of the list, and then insert them into the sorted
part of the list.
HeapSort(arr)
BuildMaxHeap(arr)
for i = length(arr) to 2
swap arr[1] with arr[i]
heap_size[arr] = heap_size[arr] ? 1
MaxHeapify(arr,1)
End
BuildMaxHeap(arr)
BuildMaxHeap(arr)
heap_size(arr) = length(arr)
for i = length(arr)/2 to 1
MaxHeapify(arr,i)
End
MaxHeapify(arr,i)
MaxHeapify(arr,i)
L = left(i)
R = right(i)
if L ? heap_size[arr] and arr[L] > arr[i]
largest = L
else
largest = i
if R ? heap_size[arr] and arr[R] > arr[largest]
largest = R
if largest != i
swap arr[i] with arr[largest]
MaxHeapify(arr,largest)
End
13
References:
Quicksort https://www.geeksforgeeks.org/quick-sort/
https://towardsdatascience.com/an-overview-of-
quicksort-algorithm-b9144e314a72
https://www.tutorialspoint.com/
data_structures_algorithms/
quick_sort_algorithm.htm#:~:text=Quicksort
%20partitions%20an%20array%20and,(n2)%2C
%20respectively
Mergesort https://www.tutorialspoint.com/
data_structures_algorithms/
merge_sort_algorithm.htm#:~:text=Merge%20sort%20is
%20a%20sorting,them%20in%20a%20sorted%20manner.
https://www.javatpoint.com/merge-sort
Heapsort https://www.javatpoint.com/heap-sort
https://www.geeksforgeeks.org/heap-sort/
#:~:text=Heap%20sort%20is%20a%20comparison,process
%20for%20the%20remaining%20elements.
https://www.interviewbit.com/tutorial/heap-sort-
algorithm/
14