Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

COMSATS University Islamabad

Islamabad Campus

LAB Assignment No. 3: COMPARISION OF SORTING ALGORITHMS


Subject: Data Structure and Algorithm Assigned: November 6, 2019 Assignment Marks:

Instructor: Tanveer Ahmed Siddiqui Due Date: November 13, 2019 Attached Files: Non
CLO3: Compare various searching and sorting algorithms with respect to time complexity.
Objective(s):
 Implement and analyze basic sorting algorithms: Selection Sort, Bubble Sort, and insertion sort,
 Think of ways of improving Sorting Algorithms.
Learning Outcomes:
 Analyze the best, worst, and average case running time of various sorting algorithms.
 Measuring the performance of sorting algorithm implementation: empirical measurement, operation
counting, and big O notation.
Level: Compare/Analyze/Evaluate
Instructions:
Please read the following instructions before attempting to solve this assignment
1. This is an individual assignment. You will submit your work individually through your logins (course
portal) and also submit hard copy.
2. Try to get the concepts, consolidate your concepts and ideas from these questions
3. You should concern recommended books for clarify your concepts as handouts are not sufficient.
4. Try to make solution by yourself and protect your work from other students. If I found the
solution files of some students are same then I will reward zero marks to all those students.
5. Deadline for this assignment is November 13, 2019. This deadline will not be extended.
Question # 1
An idea for enhancing the performance of “Bubble Sort” is to sort the list in both direction as follows:
“Compares each adjacent pair of items in a list in turn, swapping them if necessary, and alternately passes
through the list from the beginning to the end then from the end to the beginning. It stops when a pass does
no swaps”.
Here is illustration of above idea.

Spring 17 Page 1
COMSATS University Islamabad
Islamabad Campus

Implement above idea and compare its running time with simple Bubble Sort with respect to number of
comparison and number of elements moved/ swapped.
Question # 2
One can modify selection sort by finding minimum and maximum simultaneity and swapping them with first
and last element in the array.
Idea is
Consider the following array:
13 9 2 5 25 4 19 8
Find Minimum and Maximum
13 9 2 5 25 4 19 8

After first pass: Swapping minimum and maximum with first and last elements of the given array
2 9 13 5 8 4 19 25
Find Minimum and Maximum from remaining array

2 9 13 5 8 4 19 25
After 2nd pass: Swapping minimum and maximum with second and second last elements of the given array
2 4 13 5 8 9 19 25
Find Minimum and Maximum from remaining array

2 4 13 5 8 9 19 25

After 3rd pass: Swapping minimum and maximum with third and third last elements of the given array
2 4 5 9 8 13 19 25

2 4 5 9 8 13 19 25

After 4th pass:


2 4 5 8 9 13 19 25

Spring 17 Page 2
COMSATS University Islamabad
Islamabad Campus

Implement above idea and compare its running time with simple selection algorithm with respect to number of
comparison and number of elements moved/ swapped.
Note: Here is the algorithm that finds Minimum and Maximum from an array simultaneously.

Question # 3
A recursive procedure for Insertion sort is as as follows. In order to sort A[1 . . n], we recursively sort A[1
. . n−1] and then insert A[n] into the sorted array A[1 . . n − 1]. Here is the pseudo code that employs
above idea:
ALGORITHM InsertionSortRecur(A[0..n-1], n-1)
//A recursive implementation of insertion sort
//Input: An array A[0..n-1] of n orderable elements
//Output: Array A[0..n-1] sorted in nondecreasing order
if n > 1
InsertionSortRecur(A[0..n-1], n-2)
Insert(A[0..n-1], n-1) //insert A[n-1] to A[0..n-2]
ALGORITHM Insert(A[0..m], m)
//Insert A[m] to the sorted subarray A[0..m-1] of A[0..n-1]
//Input: A subarray A[0..m] of A[0..n-1]
//Output: Array A[0..m] sorted in nondecreasing order
key = A[m]
for j ß m – 1 to 0 do
if (key< A[j])
A[j+1] ß A[j]
else
break

Spring 17 Page 3
COMSATS University Islamabad
Islamabad Campus

A[j +1] ß key


Implement above procedure and compare its running time with iterative version.
Question # 4
Implement the following algorithm
BinaryInsertionSort (int a[], int n) BinarySearch (int a[], int low, int high, int key)
{ {
int ins, i, j; int mid;
int tmp;
if (low == high)
for (i = 1; i < n; i++) { return low;
ins = BinarySearch (a, 0, i, a[i]);
if (ins < i) { mid = low + ((high - low) / 2);
tmp = a[i];
for (j = i - 1; j >= ins; j--) if (key > a[mid])
a[j + 1] = a[j]; return BinarySearch (a, mid + 1, high, key);
a[ins] = tmp; else if (key < a[mid])
} return BinarySearch (a, low, mid, key);
}
} return mid;
}

Spring 17 Page 4

You might also like