0% found this document useful (0 votes)
312 views31 pages

Chapter 5 Sorting

Chapter 5 covers sorting algorithms including Bubble Sort, Selection Sort, and Insertion Sort, explaining their concepts, processes, and time complexities. Each algorithm is detailed with examples and Python implementations. The chapter emphasizes the importance of sorting for efficient data retrieval and searching.

Uploaded by

sureshdn1997
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)
312 views31 pages

Chapter 5 Sorting

Chapter 5 covers sorting algorithms including Bubble Sort, Selection Sort, and Insertion Sort, explaining their concepts, processes, and time complexities. Each algorithm is detailed with examples and Python implementations. The chapter emphasizes the importance of sorting for efficient data retrieval and searching.

Uploaded by

sureshdn1997
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/ 31

M

O
H
A
M
M
ED
M
AT
H
EE
N
L
R
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

Contents

LICENSE 3

CHAPTER 5: SORTING 4
CHAPTER NOTES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5.2 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

R
5.3 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.4 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

L
5.5 Time Complexity of Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

N
Summary Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

EE
MULTIPLE CHOICE QUESTIONS (MCQs) 9

FILL IN THE BLANKS 17

2 MARKS QUESTIONS H 19
AT
3 MARKS QUESTIONS 20
M

5 MARKS QUESTIONS 23
ED

CHAPTER END EXERCISES 27


M
M
A
H
O
M

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
2
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

LICENSE

This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 4.0 Interna-
tional License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or
send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

“Karnataka Second PUC Computer Science Study Material / Student Notes” by L R Mohammed Matheen
is licensed under CC BY-NC-ND 4.0.

R
L
N
Figure 1: Licence

EE
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 4.0 Interna-
tional License.

H
Portions of this work may include material under separate copyright. These materials are not covered by
AT
this Creative Commons license and are used by permission or under applicable copyright exceptions.

This book is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 4.0 Interna-


M

tional License.
ED
M
M
A
H
O
M

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
3
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

CHAPTER 5: SORTING

CHAPTER NOTES

5.1 Introduction

• Sorting is the process of arranging elements in a particular order (ascending, descending, alpha-
betical, etc.).

R
• It is essential for easy data retrieval and efficient searching.
• Common applications:

L
– Dictionaries (alphabetical order)

N
– Exam seating plans (roll number order)

EE
– Sorting by height, weight, etc.

5.2 Bubble Sort


H
AT
Concept
• Compares adjacent elements and swaps them if they are in the wrong order.
M

• After each pass, the largest unsorted element “bubbles up” to its correct position.
• Requires n – 1 passes for a list of size n.
ED

Optimization Tip If no swaps occur in a pass, the list is already sorted — the algorithm can be termi-
nated early.
M
M

Diagram: Bubble Sort Passes We will sort:


[8, 7, 13, 1, -9, 4]
A

Each pass compares adjacent elements and swaps if needed.


H

Pass 1
O

[8, 7, 13, 1, -9, 4]


M

→ Swap 8 and 7 → [7, 8, 13, 1, -9, 4]


→ No Swap (8,13)
→ Swap 13 and 1 → [7, 8, 1, 13, -9, 4]
→ Swap 13 and -9 → [7, 8, 1, -9, 13, 4]
→ Swap 13 and 4 → [7, 8, 1, -9, 4, 13]

Pass 2

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
4
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

[7, 8, 1, -9, 4, 13]


→ No Swap (7,8)
→ Swap 8 and 1 → [7, 1, 8, -9, 4, 13]
→ Swap 8 and -9 → [7, 1, -9, 8, 4, 13]
→ Swap 8 and 4 → [7, 1, -9, 4, 8, 13]

Pass 3

R
[7, 1, -9, 4, 8, 13]

L
→ Swap 7 and 1 → [1, 7, -9, 4, 8, 13]
→ Swap 7 and -9 → [1, -9, 7, 4, 8, 13]

N
→ Swap 7 and 4 → [1, -9, 4, 7, 8, 13]

EE
Pass 4

[1, -9, 4, 7, 8, 13]


H
AT
→ Swap 1 and -9 → [-9, 1, 4, 7, 8, 13]
M

Pass 5

No swaps → Sorting complete.


ED

Final list: [-9, 1, 4, 7, 8, 13]


M

Algorithm 5.1: Bubble Sort


M

BUBBLESORT(numList, n)
1. Set i = 0
A

2. While i < n repeat:


3. Set j = 0
H

4. While j < n - i - 1 repeat:


O

5. If numList[j] > numList[j+1], then


6. Swap numList[j] and numList[j+1]
M

7. j = j + 1
8. i = i + 1

Python Program
def bubble_Sort(list1):
n = len(list1)
for i in range(n):

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
5
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

for j in range(0, n-i-1):


if list1[j] > list1[j+1]:
list1[j], list1[j+1] = list1[j+1], list1[j]

numList = [8, 7, 13, 1, -9, 4]


bubble_Sort(numList)
print("The sorted list is:")
for i in range(len(numList)):
print(numList[i], end=" ")

R
L
5.3 Selection Sort

N
Concept

EE
• The list is divided into sorted and unsorted parts.
• The smallest element from the unsorted list is selected and swapped with the first unsorted element.
• Requires n – 1 passes for n elements.
H
AT
Diagram: Selection Sort Passes Starting List: [8, 7, 13, 1, -9, 4]
M

Pass 1 - Smallest is -9 → Swap with 8


[-9, 7, 13, 1, 8, 4]
ED

Pass 2 - Smallest in [7,13,1,8,4] is 1 → Swap with 7


[-9, 1, 13, 7, 8, 4]
M

Pass 3 - Smallest in [13,7,8,4] is 4 → Swap with 13


[-9, 1, 4, 7, 8, 13]
M

Pass 4 - Smallest in [7,8,13] is 7 (already in place)


A

[-9, 1, 4, 7, 8, 13]
H

Pass 5 - Already sorted


[-9, 1, 4, 7, 8, 13]
O
M

Algorithm 5.2: Selection Sort


SELECTIONSORT(numList, n)
1. Set i = 0
2. While i < n repeat:
3. Set min = i, flag = 0
4. Set j = i + 1
5. While j < n repeat:

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
6
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

6. If numList[j] < numList[min]:


7. min = j
8. flag = 1
9. If flag == 1:
10. Swap numList[i], numList[min]
11. i = i + 1

Python Program

R
def selection_Sort(list2):
n = len(list2)

L
for i in range(n):
min = i

N
for j in range(i + 1, n):

EE
if list2[j] < list2[min]:
min = j
list2[i], list2[min] = list2[min], list2[i]

H
AT
numList = [8, 7, 13, 1, -9, 4]
selection_Sort(numList)
print("The sorted list is:")
M

for i in range(len(numList)):
print(numList[i], end=" ")
ED

5.4 Insertion Sort


M

Concept
M

• Elements from the unsorted part are picked and inserted into the correct position of the sorted part.
• Like arranging cards in hand.
A
H

Diagram: Insertion Sort Passes Initial List: [8, 7, 13, 1, -9, 4]


O

Pass 1 Insert 7 into sorted [8]


[7, 8, 13, 1, -9, 4]
M

Pass 2 Insert 13 into sorted [7,8]


[7, 8, 13, 1, -9, 4]
Pass 3 Insert 1 into sorted [7,8,13]
[1, 7, 8, 13, -9, 4]
Pass 4 Insert -9 into sorted [1,7,8,13]
[-9, 1, 7, 8, 13, 4]

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
7
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

Pass 5 Insert 4 into sorted [-9,1,7,8,13]


[-9, 1, 4, 7, 8, 13]

Algorithm 5.3: Insertion Sort


INSERTIONSORT(numList, n)
1. Set i = 1
2. While i < n repeat:

R
3. temp = numList[i]
4. Set j = i - 1

L
5. While j >= 0 and numList[j] > temp:
6. numList[j+1] = numList[j]

N
7. j = j - 1
8. numList[j+1] = temp

EE
9. i = i + 1

Python Program
H
AT
def insertion_Sort(list3):
n = len(list3)
for i in range(n):
M

temp = list3[i]
j = i-1
ED

while j >= 0 and temp < list3[j]:


list3[j+1] = list3[j]
j -= 1
M

list3[j+1] = temp
M

numList = [8, 7, 13, 1, -9, 4]


insertion_Sort(numList)
A

print("The sorted list is:")


for i in range(len(numList)):
H

print(numList[i], end=" ")


O
M

5.5 Time Complexity of Algorithms

Basic Complexity Concepts

• Constant Time (O(1)): No loops.


• Linear Time (O(n)): Single loop.
• Quadratic Time (O(n²)): Nested loops.

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
8
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

All three sorting algorithms in this chapter (bubble, selection, insertion) have O(n²) complexity due to
nested loops.

Summary Points

• Sorting: Rearranging elements for efficient access.


• Bubble Sort: Repeated adjacent swaps, n-1 passes.

R
• Selection Sort: Select and place smallest, reduce unsorted list.
• Insertion Sort: Insert each element into sorted part at the correct position.

L
• Time Complexity: Helps choose suitable algorithms for large datasets.

N
MULTIPLE CHOICE QUESTIONS (MCQs)

EE
1. Which of the following sorting techniques is based on repeatedly comparing adjacent ele-
ments and swapping them if they are in the wrong order?
H
AT
A) Selection Sort
M

B) Bubble Sort
ED

C) Insertion Sort

D) Merge Sort
M

Answer: B) Bubble Sort


M

2. What is the time complexity of Bubble Sort in the worst-case scenario?


A

A) O(n)
H
O

B) O(log n)
M

C) O(n²)

D) O(n log n)

Answer: C) O(n²)

3. Which of the following best describes the logic of the Bubble Sort algorithm?

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
9
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

A) Select the smallest element and place it in the beginning.

B) Insert the current element into its correct position in a sorted list.

C) Swap adjacent elements until the largest reaches the end.

D) Divide the list and merge sorted parts.

R
Answer: C) Swap adjacent elements until the largest reaches the end.

L
4. How many passes are required to sort a list of 6 elements using Bubble Sort?

A) 5

N
EE
B) 6

C) 4
H
AT
D) 3

Answer: A) 5
M

5. Which sorting technique finds the smallest element in the list and places it in its correct po-
ED

sition?

A) Insertion Sort
M

B) Bubble Sort
M

C) Selection Sort
A
H

D) Quick Sort
O

Answer: C) Selection Sort


M

6. In Selection Sort, after the first pass, the first element of the list is:

A) The largest element

B) The average of the list

C) The smallest element

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
10
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

D) Unchanged

Answer: C) The smallest element

7. What is the time complexity of Selection Sort in the best case?

A) O(n log n)

B) O(n²)

R
C) O(n)

L
N
D) O(log n)

EE
Answer: B) O(n²)

8. Which sorting technique is best described as “a sorted list is built one element at a time”?

A) Merge Sort
H
AT
B) Insertion Sort
M

C) Bubble Sort
ED

D) Selection Sort

Answer: B) Insertion Sort


M

9. In Insertion Sort, which of the following is TRUE about the sorted portion of the list?
M

A) It decreases in size
A

B) It is built from left to right


H
O

C) It always remains constant


M

D) It is sorted in reverse order

Answer: B) It is built from left to right

10. What is the best-case time complexity of Insertion Sort?

A) O(n log n)

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
11
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

B) O(n)

C) O(n²)

D) O(1)

Answer: B) O(n)

R
11. Which sorting algorithm is generally better for nearly sorted data?

A) Selection Sort

L
N
B) Bubble Sort

EE
C) Insertion Sort

D) Heap Sort
H
AT
Answer: C) Insertion Sort

12. In Bubble Sort, which element reaches its correct position after the first pass?
M

A) The smallest element


ED

B) The middle element


M

C) The largest element


M

D) The second largest element


A

Answer: C) The largest element


H

13. Which sorting algorithm swaps elements only when necessary and avoids unnecessary move-
O

ments?
M

A) Bubble Sort

B) Insertion Sort

C) Selection Sort

D) None of the above

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
12
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

Answer: C) Selection Sort

14. Which of the following algorithms uses a temporary variable during swapping?

A) Bubble Sort

B) Selection Sort

R
C) Insertion Sort

L
D) All of the above

N
Answer: D) All of the above

EE
15. Which sort algorithm is not** efficient for large datasets?**

A) Quick Sort

H
AT
B) Insertion Sort

C) Merge Sort
M

D) Heap Sort
ED

Answer: B) Insertion Sort

16. In the Insertion Sort algorithm, when is shifting required?


M

A) When the current element is greater than the previous one


M
A

B) When the current element is smaller than elements in the sorted list
H

C) When elements are equal


O

D) Always
M

Answer: B) When the current element is smaller than elements in the sorted list

17. The number of comparisons in the worst case for Bubble Sort is:

A) n

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
13
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

B) n-1

C) n(n-1)/2

D) log n

Answer: C) n(n-1)/2

R
18. Which sorting algorithm is described using the following pseudocode?

For i = 0 to n-2:

L
For j = 0 to n-2-i:
If arr[j] > arr[j+1]:

N
Swap arr[j] and arr[j+1]

EE
A) Insertion Sort

B) Bubble Sort
H
AT
C) Selection Sort
M

D) Quick Sort

Answer: B) Bubble Sort


ED

19. Which sorting method has the same time complexity in best, average, and worst cases?
M

A) Selection Sort
M

B) Insertion Sort
A

C) Merge Sort
H
O

D) Bubble Sort
M

Answer: A) Selection Sort

20. In Selection Sort, if the minimum element is already at its correct position, what happens?

A) The list is reversed

B) The algorithm stops

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
14
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

C) No swap is made

D) Swap still happens

Answer: C) No swap is made

21.
Assertion (A): Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order.
Reason (R): Bubble Sort pushes the smallest element to the end in each pass.

R
A. Both A and R are true, and R is the correct explanation of A.

L
B. Both A and R are true, but R is not the correct explanation of A.
C. A is true, but R is false.

N
D. A is false, but R is true.

EE
Answer: C. A is true, but R is false.
Explanation: Bubble Sort pushes the largest element to the end of the list in each pass, not the small-
est.
H
AT
22.
Assertion (A): In Selection Sort, the smallest element is selected in each pass and placed in its correct
position.
M

Reason (R): Selection Sort uses repeated shifting of elements to sort the list.
ED

A. Both A and R are true, and R is the correct explanation of A.


B. Both A and R are true, but R is not the correct explanation of A.
C. A is true, but R is false.
M

D. A is false, but R is true.


M

Answer: C. A is true, but R is false.


Explanation: Selection Sort works by swapping, not shifting elements.
A

23.
H

Assertion (A): Insertion Sort is efficient for nearly sorted lists.


Reason (R): In the best case, the time complexity of Insertion Sort is O(n).
O

A. Both A and R are true, and R is the correct explanation of A.


M

B. Both A and R are true, but R is not the correct explanation of A.


C. A is true, but R is false.
D. A is false, but R is true.

Answer: A. Both A and R are true, and R is the correct explanation of A.

24.
Assertion (A): Selection Sort is a stable sorting algorithm.

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
15
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

Reason (R): Selection Sort preserves the order of identical elements.

A. Both A and R are true, and R is the correct explanation of A.


B. Both A and R are true, but R is not the correct explanation of A.
C. A is false, but R is true.
D. Both A and R are false.

Answer: D. Both A and R are false.


Explanation: Selection Sort is not stable by default and may change the relative order of identical ele-

R
ments.

L
25.
Assertion (A): Insertion Sort builds the final sorted list one item at a time.

N
Reason (R): It inserts each new element at the beginning of the list.

EE
A. Both A and R are true, and R is the correct explanation of A.
B. Both A and R are true, but R is not the correct explanation of A.
C. A is true, but R is false.
D. A is false, but R is true. H
AT
Answer: C. A is true, but R is false.
Explanation: Insertion Sort inserts elements into the correct position within the sorted portion, not
M

necessarily at the beginning.

26.
ED

Assertion (A): Bubble Sort can be optimized to stop early if no swaps are made during a pass.
Reason (R): This optimization improves its best-case time complexity to O(n).
M

A. Both A and R are true, and R is the correct explanation of A.


B. Both A and R are true, but R is not the correct explanation of A.
M

C. A is true, but R is false.


A

D. A is false, but R is true.


H

Answer: A. Both A and R are true, and R is the correct explanation of A.


O

27.
Assertion (A): In Bubble Sort, every pair of adjacent elements is compared in each pass.
M

Reason (R): The number of comparisons increases with each pass.

A. Both A and R are true, and R is the correct explanation of A.


B. Both A and R are true, but R is not the correct explanation of A.
C. A is true, but R is false.
D. A is false, but R is true.

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
16
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

Answer: C. A is true, but R is false.


Explanation: The number of comparisons decreases with each pass in Bubble Sort.

28.
Assertion (A): In Selection Sort, the number of comparisons is always the same regardless of the input
order.
Reason (R): Selection Sort has a time complexity of O(n²) for all cases.

A. Both A and R are true, and R is the correct explanation of A.

R
B. Both A and R are true, but R is not the correct explanation of A.
C. A is true, but R is false.

L
D. A is false, but R is true.

N
Answer: A. Both A and R are true, and R is the correct explanation of A.

EE
29.
Assertion (A): In Insertion Sort, shifting elements is necessary to maintain the sorted portion of the list.

H
Reason (R): Elements are shifted right to make space for the current element.
AT
A. Both A and R are true, and R is the correct explanation of A.
B. Both A and R are true, but R is not the correct explanation of A.
C. A is true, but R is false.
M

D. A is false, but R is true.

Answer: A. Both A and R are true, and R is the correct explanation of A.


ED

30.
Assertion (A): All three sorting techniques (Bubble, Selection, Insertion) have a worst-case time com-
M

plexity of O(n²).
Reason (R): They all use nested loops with two iterations each.
M

A. Both A and R are true, and R is the correct explanation of A.


A

B. Both A and R are true, but R is not the correct explanation of A.


H

C. A is true, but R is false.


D. A is false, but R is true.
O

Answer: A. Both A and R are true, and R is the correct explanation of A.


M

FILL IN THE BLANKS

1. Sorting is the process of arranging elements in a particular __________.


Answer: order

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
17
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

2. In Bubble Sort, after each pass, the __________ element gets placed at its correct position.
Answer: largest
3. In Bubble Sort, the process of comparing and swapping adjacent elements is repeated for __________
- 1 passes.
Answer: n
4. If no __________ are made during a pass in Bubble Sort, the list is already sorted.
Answer: swaps

R
5. In Selection Sort, the list is conceptually divided into a __________ list and an unsorted list.

L
Answer: sorted
6. In each pass of Selection Sort, the __________ element from the unsorted portion is selected and

N
placed in the sorted list.

EE
Answer: smallest
7. Selection Sort requires a total of __________ - 1 passes to sort a list of n elements.
Answer: n
H
AT
8. In Insertion Sort, each element from the unsorted list is __________ into its correct position in the
sorted part.
Answer: inserted
M

9. In Insertion Sort, shifting of elements occurs from __________ to find the correct position of insertion.
Answer: right to left
ED

10. In the best-case scenario, Insertion Sort has a time complexity of __________.
Answer: O(n)
M

11. The time complexity of Bubble, Selection, and Insertion Sort in the worst case is __________.
M

Answer: O(n²)
12. A __________ loop is responsible for increasing the time complexity of sorting algorithms to
A

quadratic.
H

Answer: nested
O

13. Sorting helps to improve the efficiency of __________ operations on large datasets.
Answer: searching
M

14. In Python, a pair of adjacent elements in Bubble Sort can be swapped using the syntax:
list[j], list[j+1] = __________
Answer: list[j+1], list[j]
15. The algorithm that builds the sorted list one element at a time by comparing and shifting is
__________ Sort.
Answer: Insertion

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
18
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

2 MARKS QUESTIONS

1. What is sorting? Give one real-life example.

Answer:
Sorting is the process of arranging or ordering a given collection of elements in a particular order, such
as ascending or descending.
Example: Words in a dictionary are sorted in alphabetical order to make searching easier.

R
2. Define pass and swap in the context of Bubble Sort.

L
Answer:
A pass refers to one complete iteration through the list during sorting.

N
A swap is the process of exchanging the positions of two adjacent elements if they are not in the desired

EE
order.

3. Write the syntax of swapping two elements in Python with an example.

Answer:
H
AT
Syntax:

a, b = b, a
M

Example:

list[j], list[j+1] = list[j+1], list[j]


ED

This swaps the elements at index j and j+1 in a list.


M

4. Differentiate between Bubble Sort and Selection Sort (any two points).

Answer:
M

Bubble Sort Selection Sort


A
H

Repeatedly compares and swaps adjacent Selects the smallest element and swaps it with the
elements. leftmost unsorted element.
O

Pushes the largest element to the end in each Places the smallest element at the beginning in each
M

pass. pass.

5. What is the purpose of using a flag in Selection Sort algorithm?

Answer:
The flag is used to check whether a smaller element than the current minimum was found in the current
pass. If flag = 1, a swap is needed; otherwise, no swap is performed.

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
19
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

6. Give one example where Insertion Sort is more efficient than other sorts. Explain why.

Answer:
Insertion Sort is more efficient for nearly sorted lists because it requires fewer comparisons and shifts,
giving it a best-case time complexity of O(n).

7. Write the initial list and final sorted list after applying Insertion Sort on: [8, 7, 13, 1, -9,
4].

R
Answer:
Initial List: [8, 7, 13, 1, -9, 4]

L
Final Sorted List: [-9, 1, 4, 7, 8, 13]

8. Distinguish between shifting and swapping in the context of sorting.

N
Answer:

EE
Shifting means moving elements one position to the right to make space (used in Insertion Sort).
Swapping means exchanging two elements’ positions directly (used in Bubble and Selection Sort).

9. Explain time complexity with an example from the chapter.


H
AT
Answer:
Time complexity is the amount of time an algorithm takes to run, depending on input size.
M

Example: Bubble Sort has O(n²) time complexity because it uses a nested loop to perform n × (n – 1)/2
comparisons.
ED

10. Write the output of the following Bubble Sort implementation on the list [8, 7, 13, 1, -9,
4].
M

Also, mention how many passes are needed.

Answer:
M

Output: [-9, 1, 4, 7, 8, 13]


Passes Needed: 5 (as the list contains 6 elements)
A
H

3 MARKS QUESTIONS
O
M

1. Explain the concept of Bubble Sort with an example.

Answer:
Bubble Sort compares adjacent elements and swaps them if they are in the wrong order. After each pass,
the largest element is moved to its correct position at the end. This process is repeated for n−1 passes for
a list of n elements.

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
20
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

Example:
For numList = [8, 7, 13, 1, -9, 4], after Bubble Sort, the list becomes [-9, 1, 4, 7, 8,
13].

2. Differentiate between Bubble Sort and Insertion Sort (any three points).

Answer:

Bubble Sort Insertion Sort

R
Compares and swaps adjacent elements. Inserts each element at the correct position in sorted

L
part.
Time complexity: O(n²) in all cases. Best case time complexity: O(n).

N
Poor for nearly sorted data. Efficient for nearly sorted data.

EE
H
3. Write the Python implementation of Selection Sort and explain it briefly.
AT
Answer:

def selection_Sort(list2):
n = len(list2)
M

for i in range(n):
min = i
ED

for j in range(i + 1, n):


if list2[j] < list2[min]:
min = j
M

list2[i], list2[min] = list2[min], list2[i]


M

Explanation:
The algorithm selects the smallest element from the unsorted list and swaps it with the element at the
A

current position. This process continues until the list is sorted.


H

4. Define the term ‘pass’ in sorting. How many passes are required to sort a list of 6 elements using
O

Selection Sort?

Answer:
M

A pass is one complete traversal over the list during the sorting process.
In Selection Sort, for a list of n = 6 elements, the total number of passes required is n – 1 = 5 passes.

5. How does Insertion Sort work? Explain with an example.

Answer:
Insertion Sort builds a sorted list one element at a time. Each new element is compared with elements in
the sorted list and inserted into its correct position by shifting larger elements to the right.

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
21
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

Example:
[8, 7, 13, 1, -9, 4] becomes [-9, 1, 4, 7, 8, 13] after applying Insertion Sort.

6. Write the algorithm for Bubble Sort and explain its steps.

Answer:
Algorithm:

1. Set i = 0

R
2. While i < n, repeat steps 3–8
3. Set j = 0

L
4. While j < n - i - 1, repeat steps 5–7

N
5. If list[j] > list[j+1]
6. Swap list[j] and list[j+1]

EE
7. Increment j
8. Increment i

H
AT
Explanation:
The algorithm performs nested loops to compare and swap adjacent elements so that the largest unsorted
element moves to its correct position after each pass.
M

7. What is time complexity? Explain with respect to Bubble, Selection, and Insertion Sort.
ED

Answer:
Time complexity is the amount of time an algorithm takes to complete based on input size.
In Bubble, Selection, and Insertion Sort, all three have nested loops, hence their worst-case time com-
M

plexity is O(n²).
M

Insertion Sort, however, can have a best-case complexity of O(n) if the list is already nearly sorted.

8. Differentiate between swapping and shifting with suitable examples from the sorting algo-
A

rithms.
H

Answer:
O

Swapping Shifting
M

Used in Bubble and Selection Sort. Used in Insertion Sort.


Exchanges the positions of two elements. Moves elements one position to the right.
Example: list[j], list[j+1] = Example: list[j+1] = list[j]
list[j+1], list[j]

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
22
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

5 MARKS QUESTIONS

1. Explain the working of Bubble Sort with the help of an example. Show all passes and swaps.
Answer:
Bubble Sort works by comparing adjacent elements and swapping them if they are in the wrong order.
The largest element “bubbles up” to the end after each pass.
Example: numList = [8, 7, 13, 1, -9, 4]

R
Pass 1:
[8, 7, 13, 1, -9, 4] → [7, 8, 13, 1, -9, 4] → [7, 8, 1, 13, -9, 4] → [7, 8, 1, -9, 13, 4] → [7, 8, 1, -9, 4, 13]

L
Pass 2:

N
[7, 8, 1, -9, 4, 13] → [7, 1, 8, -9, 4, 13] → [7, 1, -9, 8, 4, 13] → [7, 1, -9, 4, 8, 13]
Pass 3:

EE
[7, 1, -9, 4, 8, 13] → [1, 7, -9, 4, 8, 13] → [1, -9, 7, 4, 8, 13] → [1, -9, 4, 7, 8, 13]
Pass 4:
[1, -9, 4, 7, 8, 13] → [-9, 1, 4, 7, 8, 13]
Pass 5: No swap → Already sorted H
AT
Final list: [-9, 1, 4, 7, 8, 13]
2. Write the algorithm and Python implementation of Bubble Sort.
M

Answer:
ED

Algorithm 5.1: Bubble Sort

1. Set i = 0
M

2. WHILE i < n repeat:


- Set j = 0
M

- WHILE j < n – i – 1:
A

- IF numList[j] > numList[j+1]:


- Swap numList[j], numList[j+1]
H

- j = j + 1
O

- i = i + 1
M

Python Program:

def bubble_Sort(list1):
n = len(list1)
for i in range(n):
for j in range(0, n - i - 1):
if list1[j] > list1[j+1]:
list1[j], list1[j+1] = list1[j+1], list1[j]

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
23
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

3. Explain the working of Selection Sort using an example. Show all passes.

Answer:
Selection Sort selects the smallest element from the unsorted portion and swaps it with the first element
in the unsorted list.

Example: numList = [8, 7, 13, 1, -9, 4]

Pass 1:

R
Minimum is -9 → Swap with 8 → [-9, 7, 13, 1, 8, 4]

Pass 2:

L
Minimum is 1 → Swap with 7 → [-9, 1, 13, 7, 8, 4]

N
Pass 3:
Minimum is 4 → Swap with 13 → [-9, 1, 4, 7, 8, 13]

EE
Pass 4:
Already sorted → [-9, 1, 4, 7, 8, 13]

Final List: [-9, 1, 4, 7, 8, 13] H


AT
4. Write the algorithm and Python program for Selection Sort.
M

Answer:

Algorithm 5.2: Selection Sort


ED
M

1. Set i = 0
M

2. WHILE i < n repeat:


- Set min = i, flag = 0
A

- Set j = i + 1
- WHILE j < n repeat:
H

- IF numList[j] < numList[min]:


O

- min = j, flag = 1
M

- j = j + 1
- IF flag = 1 → Swap numList[i], numList[min]
- i = i + 1

Python Program:

def selection_Sort(list2):
n = len(list2)

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
24
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

for i in range(n):
min = i
for j in range(i+1, n):
if list2[j] < list2[min]:
min = j
list2[i], list2[min] = list2[min], list2[i]

5. What is Insertion Sort? Explain its working with an example and diagrammatically.

R
Answer:
Insertion Sort builds a sorted list one element at a time. Each new element is inserted into its correct

L
position in the sorted list by shifting larger elements to the right.

N
Example: numList = [8, 7, 13, 1, -9, 4]

EE
Pass 1: [7, 8, 13, 1, -9, 4]
Pass 2: [7, 8, 13, 1, -9, 4]
Pass 3: [1, 7, 8, 13, -9, 4]
Pass 4: [-9, 1, 7, 8, 13, 4]
H
AT
Pass 5: [-9, 1, 4, 7, 8, 13]

Final List: [-9, 1, 4, 7, 8, 13]


M

6. Write the algorithm and Python implementation of Insertion Sort.

Answer:
ED

Algorithm 5.3: Insertion Sort


M

1. Set i = 1
2. WHILE i < n repeat:
M

- temp = numList[i]
A

- j = i – 1
- WHILE j � 0 and numList[j] > temp repeat:
H

- numList[j+1] = numList[j]
O

- j = j – 1
M

- numList[j+1] = temp
- i = i + 1

Python Program:

def insertion_Sort(list3):
n = len(list3)
for i in range(n):

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
25
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

temp = list3[i]
j = i - 1
while j >= 0 and temp < list3[j]:
list3[j+1] = list3[j]
j -= 1
list3[j+1] = temp

7. Compare Bubble Sort, Selection Sort, and Insertion Sort based on working, time complexity,
and best use cases.

R
Answer:

L
Criteria Bubble Sort Selection Sort Insertion Sort

N
Basic Operation Swaps adjacent Selects minimum and Inserts in sorted portion

EE
elements swaps
Time Complexity O(n²) O(n²) O(n²), best case O(n)
Best Use Case Small lists H
Small or fixed-size lists Nearly sorted lists
AT
Stable? Yes No Yes
M

8. What is time complexity? Describe the complexity of the three sorting algorithms discussed in
ED

the chapter.

Answer:
M

Time complexity is a measure of the amount of time an algorithm takes based on input size.

• Bubble Sort: O(n²) – Nested loop for comparisons and swaps


M
A

• Selection Sort: O(n²) – Regardless of input, comparisons are same


H

• Insertion Sort: O(n²) in worst case, O(n) in best case (nearly sorted data)
O

All three algorithms are quadratic time algorithms due to nested loops.
M

9. Give the syntax and explanation of swapping and shifting with examples.

Answer:

Swapping Syntax in Python:

list[j], list[j+1] = list[j+1], list[j]

Example: In Bubble/Selection Sort → swaps adjacent or selected elements.

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
26
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

Shifting Syntax:
list[j+1] = list[j]

Example: In Insertion Sort → elements are moved one place right to insert a smaller element in the
correct position.
10. Explain with an example how you can use Bubble Sort to find the median of a list.
Answer:

R
Step 1: Sort the list using Bubble Sort.
Step 2: If number of terms is odd, median is the middle term.

L
Step 3: If number is even, median = (term[n//2 – 1] + term[n//2]) / 2

N
Example:
List = [4, 1, 5, 3] → After Bubble Sort: [1, 3, 4, 5]

EE
Median = (3 + 4)/2 = 3.5
11. What is the significance of pass number in all sorting algorithms? Explain with examples.
Answer: H
AT
A pass is a complete iteration over the list.
- In Bubble Sort, each pass places the largest unsorted element at its correct position.
M

- In Selection Sort, each pass selects and places the next smallest element.
- In Insertion Sort, each pass inserts the next element into the correct position in the sorted list.
ED

Example for Bubble Sort (n = 6): 5 passes are needed.


12. Explain how sorting can be useful in real-world applications with examples.
M

Answer:
Sorting simplifies and speeds up data retrieval and comparison.
M

Examples:
- Dictionary words sorted alphabetically
A

- Roll numbers arranged in order in an exam hall


H

- Student data sorted by height, weight, or scores


O

- Percentile calculation in aptitude tests using sorting


M

Sorting is essential before binary search or when evaluating ranks/statistics.

CHAPTER END EXERCISES

1. Consider a list of 10 elements:


numList = [7, 11, 3, 10, 17, 23, 1, 4, 21, 5]
Display the partially sorted list after three complete passes of Bubble sort.

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
27
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

Answer:
After applying 3 passes of Bubble Sort, we get:

• Pass 1: [7, 3, 10, 11, 17, 1, 4, 21, 5, 23]

• Pass 2: [3, 7, 10, 11, 1, 4, 17, 5, 21, 23]

• Pass 3: [3, 7, 10, 1, 4, 11, 5, 17, 21, 23]

R
Partially Sorted List: [3, 7, 10, 1, 4, 11, 5, 17, 21, 23]

L
2. Identify the number of swaps required for sorting the following list using selection sort and
bubble sort and identify which is the better sorting technique with respect to the number of com-

N
parisons.

EE
List 1: [63, 42, 21, 9]

Answer:

• Bubble Sort: H
AT
– Swaps:
Pass 1: 63↔42, 63↔21, 63↔9 → 3 swaps
M

* Pass 2: 42↔21, 42↔9 → 2 swaps


ED

* Pass 3: 21↔9 → 1 swap


M

* Total Swaps: 6
M

• Selection Sort:
A

– Pass 1: Swap 9↔63


H

– Pass 2: Swap 21↔42


O
M

– Pass 3: Already sorted

– Total Swaps: 2

Conclusion: Selection Sort performs fewer swaps and is better in this case.

3. Consider the following lists:


List 1: [2, 3, 5, 7, 11]

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
28
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

List 2: [11, 7, 5, 3, 2]
If the lists are sorted using Insertion Sort then which of the lists (List 1 or List 2) will make the
minimum number of comparisons? Justify using diagrammatic representation.
Answer:
- List 1 is already sorted → Best-case scenario
- Comparisons per pass: 0 (or 1 minimal comparison per element)
- Time complexity: O(n)

R
• List 2 is reverse sorted → Worst-case scenario

L
– Maximum comparisons and shifts per pass

N
– Time complexity: O(n²)

EE
Conclusion: List 1 will make the minimum number of comparisons as it represents the best-case input
for Insertion Sort.

H
4. Write a program using user-defined functions that accepts a list of numbers as an argument and
AT
finds its median.
(Hint: Use bubble sort to sort the accepted list. If there are an odd number of terms, the median is the
center term. If even, add the two middle terms and divide by 2 to get the median.)
M

Answer:
ED

def bubble_sort(lst):
n = len(lst)
for i in range(n):
M

for j in range(0, n-i-1):


if lst[j] > lst[j+1]:
M

lst[j], lst[j+1] = lst[j+1], lst[j]


A

def find_median(lst):
H

bubble_sort(lst)
n = len(lst)
O

if n % 2 == 1:
return lst[n//2]
M

else:
return (lst[n//2 - 1] + lst[n//2]) / 2

# Example
nums = [4, 2, 7, 3]
print("Median is:", find_median(nums))

Output: Median is: 3.5

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
29
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

5. All the branches of XYZ school conducted an aptitude test for all the students in the age group
14 - 16. There were a total of n students. The marks of n students are stored in a list. Write a
program using a user-defined function that accepts a list of marks as an argument and calculates
the ‘xth’ percentile (where x is any number between 0 and 100).
Steps to calculate the xth percentile:
I. Order all values using Selection Sort
II. Calculate index as: x% of n

R
III. Use math.ceil() or math.floor() if necessary
IV. Return the value at that index

L
Answer:

N
import math

EE
def selection_sort(marks):
n = len(marks)
for i in range(n):
min_idx = i H
AT
for j in range(i+1, n):
if marks[j] < marks[min_idx]:
min_idx = j
M

marks[i], marks[min_idx] = marks[min_idx], marks[i]


ED

def calculate_percentile(marks, x):


selection_sort(marks)
index = math.ceil((x / 100) * len(marks)) - 1
M

return marks[index]
M

# Example
marks_list = [56, 78, 45, 90, 82, 67]
A

x = 90
H

print("90th Percentile is:", calculate_percentile(marks_list, x))


O

Output: 90th Percentile is: 90


M

6. During admission in a course, the names of the students are inserted in ascending order. Thus,
performing the sorting operation at the time of inserting elements in a list. Identify the type of
sorting technique being used and write a program using a user-defined function that is invoked
every time a name is input and stores the name in ascending order of names in the list.
Answer:
Type of Sorting Technique: Insertion Sort
Python Program:

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
30
STUDENT NOTES || CHAPTER 5 SORTING April 18, 2025

def insert_in_order(name_list, new_name):


name_list.append(new_name)
i = len(name_list) - 1
while i > 0 and name_list[i] < name_list[i-1]:
name_list[i], name_list[i-1] = name_list[i-1], name_list[i]
i -= 1
return name_list

# Example

R
names = []
names = insert_in_order(names, "Zara")

L
names = insert_in_order(names, "Amit")

N
names = insert_in_order(names, "Meera")
print("Sorted Names:", names)

EE
Output: Sorted Names: ['Amit', 'Meera', 'Zara']

H
For more information Visit:
AT
https://matheenhere.blogspot.com
M
ED
M
M
A
H
O
M

L R MOHAMMED MATHEEN M.C.A., M.A., B.ED., UGC NET.,


LECTURER, PRIMUS PU COLLEGE, BANGALORE - 560 035
31

You might also like