0% found this document useful (0 votes)
4 views43 pages

Rev Mid

The document is a midterm revision guide for a Data Structures course, authored by Eng Ihap El-Galaly, covering time complexity calculations, sorting algorithms (including selection, bubble, insertion, quicksort, and mergesort), and various algorithmic questions. It includes code snippets for analyzing time complexity, questions on algorithm performance, and comparisons between different sorting methods. Additionally, it discusses the characteristics of stable sorting algorithms and the efficiency of various search algorithms.

Uploaded by

hamokh128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views43 pages

Rev Mid

The document is a midterm revision guide for a Data Structures course, authored by Eng Ihap El-Galaly, covering time complexity calculations, sorting algorithms (including selection, bubble, insertion, quicksort, and mergesort), and various algorithmic questions. It includes code snippets for analyzing time complexity, questions on algorithm performance, and comparisons between different sorting methods. Additionally, it discusses the characteristics of stable sorting algorithms and the efficiency of various search algorithms.

Uploaded by

hamokh128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Course 5 Data Structure Midterm Revision 2025 by Eng Ihap El-Galaly

Rules for calculcate time complexity


for (int i = 0; i <= n; i++) {
x*=5;
}
System.out.println(x);

for (int i = 1; i <= n; i++) {


x*=5;
System.out.println(x);
}

for (int i = 1; i <= n; i++) {


for (int j = 1; j <= n; j++) {
x += i * j;
}
}

for (int i = 0; i <= n; i++) {


for (int j = 0; j <= n; j++) {
x += i * j;
}
}

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 1


for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
x += i * j;
}
}

for (int i = 0; i <= n; i++) {


i*=5;
}
System.out.println(x);

for (int i = 0; i*i <n ; i++) {


System.out.println(i);
}

for (int i = 0; i<n*n ; i++) {


System.out.println(i);
}

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 2


Identify the dominant term for each of the following time
complexity
2𝒏𝟑 + 𝟑𝒏𝟐 + 𝟐𝟎

300n + 200𝒏𝟏.𝟓 + 15 n 𝒍𝒐𝒈𝟏𝟎

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 3


Selection sort algorithm

Bubble sort algorithm

Insertion Sort Algorithm

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 4


Quick sort

Mergesort

List the steps to sort the following number using insertion sort

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 5


List the steps to sort the following number using selection sort

List the steps to sort the following number using bubble sort

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 6


List the steps to sort the following number using Merge sort

List the steps to sort the following number using quick sort
4 3 6 9 2 4 3 1 2 1 8 9 3 5

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 7


With my Best wishes Eng Ihap El-Galaly 01062665758 Page 8
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 9
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 10
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 11
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 12
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 13
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 14
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 15
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 16
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 17
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 18
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 19
An algorithm takes 15 seconds to solve a problem of size 200 and two minutes to
solve a problem of size 400. What is the likely running time of the algorithm?
a. constant
b. linear
c. quadratic
d. cubic

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 20


Which of the following functions grows fastest?
a. N
b. N log N
c. log N
d. N2

Which of the following functions grows fastest?


a. N + log N
b. N log N
c. N - log N
d. N

What will be the number of passes to sort the elements using insertion sort? 14, 12,16,
6, 3, 10
a) 6
b) 5
c) 7
d) 1

Using insertion-sort, how will the array elements look like after second pass? 34, 8, 64,
51, 32, 21
a) 8, 21, 32, 34, 51, 64
b) 8, 32, 34, 51, 64, 21
c) 8, 34, 51, 64, 32, 21
d) 8, 34, 64, 51, 32, 21

If the average time for searching a list of 800 elements is 2.1 microseconds using
Binary search, what is the expected average time for searching a list of 1600
elements.
a. 2.8
b. 8.4
c. 4.2
d. 6.3

Which of the following is not an advantage for array lists.


a. Elements are accessible without a specific order.
b. It is simple to declare array lists.
c. It uses less memory space.
d. It is easy to implement.

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 21


Which of the following is not an advantage for linked lists.
a. Memory space usage is dynamic.
b. Elements are accessible without a specific order.
c. Insertion at head node is fast.
d. It does not occupy unused memory space.

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 22


With my Best wishes Eng Ihap El-Galaly 01062665758 Page 23
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 24
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 25
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 26
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 27
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 28
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 29
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 30
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 31
What is recurrence for worst case of QuickSort and what is the time
complexity in Worst case?
A. Recurrence is T(n) = T(n-2) + O(n) and time complexity is O(n2)
B. Recurrence is T(n) = T(n-1) + O(n) and time complexity is O(n2)
C. Recurrence is T(n) = 2T(n/2) + O(n) and time complexity is O(n Log n)
D. Recurrence is T(n) = T(n/10) + T(9n/10) + O(n) and time complexity is O(n
Log n)

What is an external sorting algorithm?


A. Algorithm that uses tape or disk during the sort
B. Algorithm that uses main memory during the sort
C. Algorithm that involves swapping
D. Algorithm that are considered ‘in place’

If the number of records to be sorted is small, then …… sorting can be


efficient.
A. Merge
B. Heap
C. Selection
D. Bubble

When searching for the value 34 in the sorted list


[3,5,7,8,10,11,13,15,20,25,30,33] using Binary search, we check the following
element indexes.
a. 6,9,10,11
b. 6,8,9,10
c. 5,8,9,10
d. 5,8,10,11

It is not recommended to represent lists using linked lists if the most


frequently used function is...
a. Insert at the beginning of the list
b. Append
c. Delete at the beginning of the list
d. Search

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 32


Suppose we have an O(n) time algorithm that finds median of an unsorted
array. Now consider a Quicksort implementation where we first find median
using the above algorithm, then use median as pivot. What will be the worst-
case time complexity of this modified Quicksort.
A. O(n2 Log n)
B. O(n2)
C. O(n Log n Log n)
D. O(n Log n)

What is the average case complexity of Quicksort?


a) O(n log n)
b) O(log n)
c) O(n)
d) O(n2)

The given array is arr = {2,6,1}. What are the pivots that are returned as a
result of subsequent partitioning?
a) 1 and 6
b) 6 and 1
c) 2 and 6
d) 1

Which of the following is not true about Quicksort?


a) in-place algorithm
b) pivot position can be changed
c) adaptive sorting algorithm
d) can be implemented as a stable sort

Quick sort uses which of the following method to implement sorting?


a) merging
b) partitioning
c) selection
d) exchanging

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 33


You have an array of n elements. Suppose you implement quick sort by always
choosing the central element of the array as the pivot. Then the tightest
upper bound for the worst-case performance is ……………….
a) O (n2)
b) O (n long n)
c) O (n log n)
d) O (n3)

Merge sort uses which of the following technique to implement sorting?


a) backtracking
b) greedy algorithm
c) divide and conquer
d) dynamic programming

The largest number of comparisons for searching in a sorted list using Binary
search for a list that contains 280 elements is.
a. 6 times
b. 7 times
c. 8 times
d. 9 times

If we are sorting a list of 300 elements using Selection sort, and it took 2.4
microseconds, how long does it take to sort a list of 600 elements.
a. 4.8 ms
b. 9.6 ms
c. 7.2 ms
d. 12 ms

What is the average case time complexity of merge sort?


a) O(n log n)
b) O(n2)
c) O(n2log n)
d) O(n log n2)

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 34


Two-way merge sort algorithm is used to sort the following elements in
ascending order. 200, 470, 150, 80, 90, 40, 400, 300, 120, 70. What is the
order of these elements after the second pass of the merge sort algorithm?
a) 40, 80, 90, 150, 200, 300, 400, 470, 70, 120
b) 80, 150, 200, 470, 40, 90, 300, 400, 70, 120
c) 40, 70, 80, 90, 120, 150, 200, 300, 400, 470
d) 200, 470, 80, 150, 40, 90, 300, 400, 70, 120

What is mean by stable sorting algorithm?


1. A sorting algorithm is stable if it doesn’t preserve the order of duplicate
keys
2.A sorting algorithm is stable if it preserves the order of duplicate keys
3. A sorting algorithm is stable if it preserves the order of all keys
4. A sorting algorithm is stable if it preserves the order of non-duplicate keys

In the following scenarios, when will you use selection sort?


A. The input is already sorted
B. A large file has to be sorted
C. Large values need to be sorted with small keys
D. Small values need to be sorted with large keys

The number of comparisons in Selection sort algorithm is


a. n*(n-1)/2
b. n(n+1)/2
c. (n+1)*(n+2)/2
d. n²

In Binary search algorithm, if first is greater than last then


a. The list is not sorted
b. The list has only one element
c. The list is sorted in reverse order
d. The list is empty

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 35


What is the worst-case complexity of selection sort?
A. O(n log n)
B. O(log n)
C. O(n)
D. O(n2)

What is the advantage of selection sort over other sorting techniques?


a) It requires no additional storage space
b) It is scalable
c) It works best for inputs which are already sorted
d) It is faster than any other sorting technique

What is the average case complexity of selection sort?


A. O(n log n)
B. O(log n)
C. O(n)
D. O(n2)

What is the disadvantage of selection sort?


A. It requires auxiliary memory
B. It is not scalable
C. It can be used for small keys
D. None of the mentioned

The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and
selection sort respectively are,
A. 5 and 4
B. 4 and 5
C. 2 and 4
D. 2 and 5

The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag variable)
The number of iterations in selection sort and bubble sort respectively are
……………………
A. 5 and 4
B. 1 and 4
C. 0 and 4
D. 4 and 1

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 36


Comparing the efficiency of an algorithm depends on ………..
a) Time
b) Memory
c) Both a & b
d) None of the above

What is the time complexity of the following code:

1) O(n)
2) O(n log n)
3) O(n^2)
4) O(n^2 log n)

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 37


1) O(n)
2) O(log n)
3) O(n^2)
4) O(n^2 log n)

1) O(n)
2) O(n log n)
3) O(n^2)
4) O(n^2 log n)

Answer: O(1)

1. n
2. (n+1)
3. n(n-1)
4. n(n+1)

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 38


Pseudocode : List_sum(A,n)
{
1.sum =0
2.for i=0 to n-1
3.sum = sum +A[i]
4.return sum
}

for ( i=0 ; i<n ;i++)


num+=i*i;

What is the time complexity for linear search?


a) O(log2n)
b) O(n)
c) O(n2 )
d) O(nlogn)

What is the time complexity for binary search?


a) O(log2n)
b) O(n)
c) O(n2 )
d) O(nlogn)

With my Best wishes Eng Ihap El-Galaly 01062665758 Page 39


With my Best wishes Eng Ihap El-Galaly 01062665758 Page 40
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 41
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 42
With my Best wishes Eng Ihap El-Galaly 01062665758 Page 43

You might also like