Assignment No. 3:: COMSATS University Islamabad Islamabad Campus

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

COMSATS University Islamabad

Islamabad Campus

Assignment No. 3: COMPARISION OF SEARCHING AND SORTING ALGORITHMS


Subject: CSC211 Data Structure 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.
Question 1
Compare the following two algorithms that search the given value in a given array sequentially and comment
your observation about their performance(with respect to time, space, computation). On the basis of your
analysis which algorithm will you select for implementation.
Algorithm 1 Algorithm 2

Question 2
Compare the efficiency of searching (Sequential search algorithm that you select in question 1) an
ordered array of size n and searching an unordered array of the same size for the key key.
i. If no record with key key is present.
ii. If one record with key key is present and only one is sought.
iii. If more than one record with key key present and it is desired to find only the first one.
iv. If more than one record with key key is present and it is desired to find them all.
Question 3
Compare the “Linear Search” and “Binary search” by filling the following table, showing the number
of comparisons needed either to “find the value” or to “determine that the value is not in the array”,
given the array of values. Write a paragraph comparing your answers.

14 27 95 12 26 5 33 15 9 99
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Values Search data Search sorted Search sorted


Values Values Values using
Sequentially Sequentially Binary Search
16 10 10 3

FALL 20 Page 1
COMSATS University Islamabad
Islamabad Campus

17 10 10 3
14 1 4 2
5 6 1 4
99 10 10 4
100 10 10 4
0 10 10 4
Question 4
Design an algorithm that searches a sorted list of n items by dividing it into three sub list of almost n/3 items.
This algorithm finds the sub list that might contain the given item and divides it into three smaller sub list of
almost equal size. This algorithm repeats this process until it finds the item or concludes that item is not in the
list.
i. Write Pseudo code for above procedure.
ii. What is its time efficiency?
iii. How is it compared to that of the Binary search version discussed in the class?
Question 5
Compare the following sorting algorithm with respect to:
i. the number of elements moved/swapped
ii. the number of comparisons
a) Bubble sort
b) Selection Sort
c) Insertion Sort
Which of the following sorts would take the longest time to execute, and which would take the shortest
time? Justify your answer.

Algorithm the number of elements the number of Comments


moved comparisons

Best case
Worst case
Bubble Sort Average
Case
Best case
Worst case
Selection Average
Sort Case
Best case
Insertion Worst case
Sort Average
Case
Question 6

FALL 20 Page 2
COMSATS University Islamabad
Islamabad Campus

How the number of comparisons used in the best case, average case, and worst-case changes when the
size of the list to be sorted doubles from n to 2n, where n is a positive integer when these sorting
algorithms are used.
i. Bubble Sort
ii. Selection Sort
iii. Insertion sort
Question 7
Consider following two versions of insertion sort algorithm

a) Compare above algorithms and comment your observation about their performance(with respect to
time, space, computation)
b) An inversion in an array A[1….n] is a pair of indexes (i, j), i < j, with A[i] > A[j](i.e. a pair of
indexes whose values are out of order). Show that, in effect, each iteration of the while loop in
insertion sort removes one inversion.
c) Give a reasonable definition of “ nearly- sorted”. Argue that insertion sort can sort a nearly-sorted
array quickly

FALL 20 Page 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy