0% found this document useful (0 votes)
20 views

Project

The document provides instructions for submitting an assessment. It consists of three sections: Section A provides submission details like the course, deadline, and penalties for late submissions. Section B requires students to agree they understand policies on plagiarism and academic integrity. Section C is for recording submission receipt details like date, student names and IDs.

Uploaded by

KJRyozaki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Project

The document provides instructions for submitting an assessment. It consists of three sections: Section A provides submission details like the course, deadline, and penalties for late submissions. Section B requires students to agree they understand policies on plagiarism and academic integrity. Section C is for recording submission receipt details like date, student names and IDs.

Uploaded by

KJRyozaki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ASSESSMENT COVERSHEET

Attach this coversheet as the cover of your submission. All sections must be completed.

Section A: Submission Details

Programme : DIPLOMA IN INFORMATION TECHNOLOGY


Course Code & Name : ITD20603 - DATA STRUCTURE
Course Lecturer(s) : SUZANA KASSIM
Submission Title : PROJECT
Deadline : Day 29 Month MAY Year 2022 Time 4PM
Penalties :  5% will be deducted per day to a maximum of four (4) working days, after which the
submission will not be accepted.
 Plagiarised work is an Academic Offence in University Rules & Regulations and will be
penalised accordingly.

Section B: Academic Integrity


Tick (√) each box below if you agree:
√ I have read and understood the UniKL’s policy on Plagiarism in University Rules & Regulations.
√ This submission is my own, unless indicated with proper referencing.
√ This submission has not been previously submitted or published.
√ This submission follows the requirements stated in the course.

Section C: Submission Receipt


(must be filled in manually)

Office Receipt of Submission


Date & Time of
Student Name(s) Student ID(s)
Submission (stamp)
29/5/2022 MUHAMMAD ARIF BIN MOHAMAD YUSOF 52101120020
NUR WITRAH ATIKAH BINTI SYALMAN 52101120139
NUR DINI ASYIQIN BINTI ILHAM ARMALIS 52101120122

Student Receipt of Submission

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.

Date & Time of


Course Code Submission Title Student ID(s) & Signature(s)
Submission (stamp)
29/5/2022 ITD20603 PROJECT 52101120020
52101120139
52101120122
UNIVERSITI KUALA LUMPUR – MIIT

DIPLOMA IN INFORMATION TECHNOLOGY


ITD21603 DATA STRUCTURE

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

//start –> Starting index, end --> Ending index


Quicksort (array, start, end)
{
if (start < end)
{
pIndex = Partition (A, start, end)
Quicksort(A, start,pIndex-1)
Quicksort(A,pIndex+1, end)
}
}

Partition 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.

partition (array, start, end)


{
// Setting rightmost Index as pivot
pivot = arr[end];

i = (start - 1) // Index of smaller element and indicates


the
// right position of pivot found so far
for (j = start; j <= end- 1; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swap arr[i] and arr[j]
}
}
swap arr[i + 1] and arr[end])
return (i + 1)
}

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.

To understand merge sort, we take an unsorted array as the following: -

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.

Step 1 − if it is only one element in the list it is already sorted,


return.
Step 2 − divide the list recursively into two halves until it can no
more be divided.
Step 3 − merge the smaller lists into new list in sorted order.

In the following algorithm, arr is the given array, beg is the starting element, and end is the
last element of the array:

MERGE_SORT(arr, beg, end)

if beg < end


set mid = (beg + end)/2
MERGE_SORT(arr, beg, mid)
MERGE_SORT(arr, mid + 1, end)
MERGE (arr, beg, mid, end)
end of if

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:

/* Function to merge the subarrays of a[] */


void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;

int LeftArray[n1], RightArray[n2]; //temporary arrays

/* copy data to temp arrays */


for (int i = 0; i < n1; i++)
LeftArray[i] = a[beg + i];
for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j];

i = 0, /* initial index of first sub-array */


j = 0; /* initial index of second sub-array */
k = beg; /* initial index of merged sub-array */

while (i < n1 && j < n2)


{
if(LeftArray[i] <= RightArray[j])
{
a[k] = LeftArray[i];
i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}

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.

A heap is a tree data structure that satisfies the following properties:

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

Suppose the array that is to be sorted contains the following elements:


11, 2, 9, 13, 57, 25, 17, 1, 90, 3

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 is the in-place sorting algorithm.

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

You might also like