Chapter 5
Chapter 5
[Link]
Contents
LICENSE 2
Chapter 5: Sorting 3
CHAPTER BLUEPRINT PLAN FOR FINAL EXAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
STUDENT NOTES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.2 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.3 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
5.4 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5.5 Time Complexity of Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Question and Answer Bank . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
VERY SHORT ANSWERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Multiple Choice Questions MCQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Fill in the Blanks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
DESCRIPTIVE QUESTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2 Marks Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3 Marks Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5 Marks Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Chapter End Exercise Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Disclaimer and Acknowledgement
Disclaimer
This eBook is distributed solely for educational and study-support purposes, with no commercial intent. The content
provided herein is offered on an “as-is” basis, and the editors or publishers do not assume any responsibility for errors,
inaccuracies, or ambiguities arising from the translation.
Readers are advised to refer to official textbooks, notifications, and original sources for verification. The authors or con-
tributors shall not be held responsible for any direct or indirect consequences arising from the use of this study material.
Sources
This Material has been prepared using the following sources:
1. NCERT Textbook for Class XII – Computer Science: [Link]
2. NCERT Textbook for Class XI – Computer Science: [Link]
Kannada Edition
The translation provided in the Kannada version has been facilitated using the Google Cloud Translation API. Every
possible effort has been made to ensure technical accuracy. However, due to the automated nature of the translation process,
there may be instances of linguistic nuances, variations in terminology, or minor differences in meaning.
1
LICENSE
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 4.0 International License. To
view a copy of this license, visit [Link] or send a letter to Creative Commons,
PO Box 1866, Mountain View, CA 94042, USA.
Figure 1: Licence
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 4.0 International License.
Portions of this work may include material under separate copyright. These materials are not covered by 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 International License.
2
Chapter 5: Sorting
STUDENT NOTES
5.1 Introduction
• Sorting is the process of ordering or arranging a given collection of elements in some particular order.
• Elements can be sorted in ascending (increasing) or descending (decreasing) order.
• Strings can be sorted alphabetically (a-z or z-a) or by length.
• Real-world examples:
– Words in a dictionary are sorted alphabetically.
– Examination hall seats are ordered by roll number.
– Students can be sorted by height or weight.
• Sorting makes searching easier and faster. For example, searching in an unsorted dictionary would be tedious.
• Sorting is important in computer science; many algorithms have been developed and analyzed for performance.
• In this chapter: Bubble Sort, Selection Sort, Insertion Sort, and Time Complexity.
3
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
Output:
The sorted list is:
-9 1 4 7 8 13
Output:
The sorted list is:
-9 1 4 7 8 13
Output:
The sorted list is:
-9 1 4 7 8 13
5 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
– If both nested and single loops exist, complexity is dominated by the nested loop.
• Bubble Sort, Selection Sort, and Insertion Sort all have O(n2 ) time complexity due to nested loops.
Summary
• Sorting: Arranging elements in a specific order.
• Bubble Sort: Repeatedly swaps adjacent elements; O(n2 ).
• Selection Sort: Selects smallest element and swaps; O(n2 ).
• Insertion Sort: Inserts each element into its correct position; O(n2 ).
• Complexity Analysis: Evaluates algorithm performance as input size grows.
(d) n/2
Answer:
(b) n-1
Ref: Section 5.2
5. In Bubble Sort, after each pass, the largest element moves to the ______.
(a) beginning
(b) middle
(c) end
(d) random position
Answer:
(c) end
Ref: Section 5.2
6. What is the time complexity of Bubble Sort?
(a) O(1)
(b) O(n)
(c) O(n2 )
(d) O(log n)
Answer:
(c) O(n2 )
Ref: Section 5.5
7. Selection Sort divides the list into ______ parts.
(a) two
(b) three
(c) four
(d) five
Answer:
(a) two
Ref: Section 5.3
8. In Selection Sort, the left part contains ______ elements.
(a) unsorted
(b) sorted
(c) random
(d) duplicate
Answer:
(b) sorted
Ref: Section 5.3
9. How many passes does Selection Sort make for n elements?
(a) n
(b) n-1
(c) n+1
7 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
(d) n/2
Answer:
(b) n-1
Ref: Section 5.3
10. In Selection Sort, the smallest element is swapped with the ______ element of the unsorted list.
(a) rightmost
(b) middle
(c) leftmost
(d) last
Answer:
(c) leftmost
Ref: Section 5.3
11. What is the time complexity of Selection Sort?
(a) O(1)
(b) O(n)
(c) O(n2 )
(d) O(log n)
Answer:
(c) O(n2 )
Ref: Section 5.5
12. Insertion Sort is similar to arranging ______.
(a) books on a shelf
(b) cards in hand
(c) numbers in a lottery
(d) files in a cabinet
Answer:
(b) cards in hand
Ref: Section 5.4
13. In Insertion Sort, the list is divided into ______ parts.
(a) two
(b) three
(c) four
(d) five
Answer:
(a) two
Ref: Section 5.4
14. In Insertion Sort, the sorted list is traversed from the ______ to find the insertion point.
(a) beginning
(b) middle
(c) end
9 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
11 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
(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
Ref: Section 5.1
35. Which of the following is NOT a sorting algorithm discussed in the chapter?
(a) Bubble Sort
(b) Selection Sort
(c) Insertion Sort
(d) Quick Sort
Answer:
(d) Quick Sort
Ref: Chapter 5
36. In Bubble Sort, after the first pass, the largest element is at ______.
(a) position 0
(b) position n-1
(c) position n/2
(d) random position
Answer:
(b) position n-1
Ref: Section 5.2
37. In Selection Sort, the flag variable is used to ______.
(a) count passes
(b) check if swap is needed
(c) store temporary value
(d) terminate loop
Answer:
(b) check if swap is needed
Ref: Algorithm 5.2
38. In Insertion Sort, the while loop condition checks if ______.
(a) j > 0
(b) j >= 0 and temp < list[j]
(c) j < n
(d) temp > list[j]
Answer:
(b) j >= 0 and temp < list[j]
Ref: Algorithm 5.3
39. The term “overhead” in sorting refers to ______.
(a) extra time taken
13 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
(b) smallest
(c) middle
(d) first
Answer:
(b) smallest
Ref: Algorithm 5.2
45. In Insertion Sort, the variable temp holds the ______.
(a) current element to be inserted
(b) previous element
(c) next element
(d) sorted element
Answer:
(a) current element to be inserted
Ref: Algorithm 5.3
46. Which sorting algorithm is most suitable for sorting a list as elements are received one by one?
(a) Bubble Sort
(b) Selection Sort
(c) Insertion Sort
(d) None
Answer:
(c) Insertion Sort
Ref: Section 5.4
47. The time complexity of an algorithm with a single loop is ______.
(a) constant
(b) linear
(c) quadratic
(d) logarithmic
Answer:
(b) linear
Ref: Section 5.5
48. Which sorting algorithm is also known as “sinking sort”?
(a) Bubble Sort
(b) Selection Sort
(c) Insertion Sort
(d) Heap Sort
Answer:
(a) Bubble Sort
Ref: Section 5.2
49. In Selection Sort, after each pass, the sorted list size ______.
(a) increases by 1
15 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
(b) decreases by 1
(c) remains same
(d) doubles
Answer:
(a) increases by 1
Ref: Section 5.3
50. Which sorting algorithm uses the concept of “shifting” instead of “swapping”?
(a) Bubble Sort
(b) Selection Sort
(c) Insertion Sort
(d) Both (b) and (c)
Answer:
(c) Insertion Sort
Ref: Section 5.4
Answer:
leftmost
Ref: Section 5.3
8. Insertion Sort is similar to arranging ______ in hand.
Answer:
cards
Ref: Section 5.4
9. In Insertion Sort, the sorted list is traversed from the ______ to find the insertion point.
Answer:
end
Ref: Section 5.4
10. The time complexity of Bubble Sort is ______.
Answer:
O(n2 )
Ref: Section 5.5
11. An algorithm with no loops has ______ time complexity.
Answer:
constant or O(1)
Ref: Section 5.5
12. An algorithm with nested loops has ______ time complexity.
Answer:
quadratic or O(n2 )
Ref: Section 5.5
13. The amount of time an algorithm takes is called its ______ complexity.
Answer:
time
Ref: Section 5.5
14. Sorting a list in ______ order means from smallest to largest.
Answer:
ascending
Ref: Section 5.1
15. In Selection Sort, the flag variable indicates whether a ______ was found.
Answer:
smaller element
Ref: Algorithm 5.2
16. In Insertion Sort, the element to be inserted is stored in a ______ variable.
Answer:
temporary or temp
Ref: Algorithm 5.3
17. The extra time taken for sorting is called ______.
17 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
Answer:
overhead
Ref: Section 5.1
18. ______ analysis helps in comparing different algorithms.
Answer:
Complexity
Ref: Section 5.5
19. In Bubble Sort, the largest element “______” up to its correct position.
Answer:
bubbles
Ref: Section 5.2
20. Selection Sort makes ______ number of swaps compared to Bubble Sort.
Answer:
fewer
Ref: Section 5.2 & 5.3
21. Insertion Sort is efficient for ______ sized lists.
Answer:
small or nearly sorted
Ref: Section 5.4
22. The process of finding the median requires the list to be ______.
Answer:
sorted
Ref: Exercise Q4
23. Percentile is a measure of ______ performance.
Answer:
relative
Ref: Exercise Q5
24. If a candidate’s score is in the 90th percentile, it means they scored better than ______% of candidates.
Answer:
90
Ref: Exercise Q5
25. In ______ sort, elements are shifted to make space for insertion.
Answer:
Insertion
Ref: Section 5.4
DESCRIPTIVE QUESTIONS
2 Marks Questions
1. Define sorting. Give one real-world example.
Answer:
Sorting is the process of arranging a collection of elements in a particular order, such as ascending or descending.
Example: Words in a dictionary are arranged in alphabetical order.
Ref: Section 5.1
2. What is a pass in Bubble Sort?
Answer:
A pass in Bubble Sort is one complete iteration through the list where adjacent elements are compared and swapped if
they are in the wrong order.
Ref: Section 5.2
3. How many passes are required in Bubble Sort for n elements?
Answer:
Bubble Sort requires n-1 passes to sort a list of n elements.
Ref: Section 5.2
4. How does Selection Sort divide the list?
Answer:
Selection Sort divides the list into two parts: the sorted part (left) and the unsorted part (right). Initially, the sorted part is
empty.
Ref: Section 5.3
5. What is the role of the flag variable in Selection Sort?
Answer:
The flag variable in Selection Sort is used to indicate whether a smaller element than the current minimum was found
during the pass. If yes, a swap is performed.
Ref: Algorithm 5.2
6. How is Insertion Sort similar to arranging cards?
Answer:
In Insertion Sort, each new element is taken from the unsorted list and inserted into its correct position in the sorted list
by shifting other elements, similar to arranging a hand of cards.
Ref: Section 5.4
7. What is time complexity?
Answer:
Time complexity is the amount of time an algorithm takes to run as a function of the size of the input. It helps in analyzing
algorithm efficiency.
Ref: Section 5.5
8. State the time complexity of Bubble Sort, Selection Sort, and Insertion Sort.
Answer:
All three sorting algorithms—Bubble Sort, Selection Sort, and Insertion Sort—have a time complexity of O(n2 ).
Ref: Section 5.5
9. Why is sorting important in computing?
Answer:
Sorting makes searching faster and more efficient. It organizes data so that operations like searching, merging, and ana-
lyzing become easier and quicker.
Ref: Section 5.1
10. What is meant by “overhead” in sorting?
Answer:
19 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
Overhead refers to the extra time taken to sort a list. This time is considered worthwhile compared to the time needed to
search an unsorted list.
Ref: Section 5.1
11. Differentiate between ascending and descending order.
Answer:
Ascending order arranges elements from smallest to largest.
Descending order arranges elements from largest to smallest.
Ref: Section 5.1
12. How can Bubble Sort be optimized?
Answer:
Bubble Sort can be optimized by stopping early if no swaps occur in a pass, indicating that the list is already sorted.
Ref: Section 5.2
13. In Insertion Sort, how is the correct insertion point found?
Answer:
The sorted list is traversed from the end backwards until the correct position for the new element is found by comparing
with each element.
Ref: Section 5.4
14. What is a constant time algorithm?
Answer:
A constant time algorithm has a time complexity of O(1), meaning its execution time does not change with the size of the
input.
Ref: Section 5.5
15. What is a linear time algorithm?
Answer:
A linear time algorithm has a time complexity of O(n), meaning its execution time increases linearly with the size of the
input.
Ref: Section 5.5
3 Marks Questions
1. Explain Bubble Sort with an example of a list of 5 numbers.
Answer:
Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. For example, sorting
[5, 3, 8, 1, 2] in ascending order:
Pass 1: Compare 5 and 3 → swap → [3,5,8,1,2]; Compare 5 and 8 → no swap; Compare 8 and 1 → swap → [3,5,1,8,2];
Compare 8 and 2 → swap → [3,5,1,2,8]
Pass 2: [3,1,5,2,8]
Pass 3: [1,3,2,5,8]
Pass 4: [1,2,3,5,8] → sorted
Ref: Section 5.2
2. Describe the steps of Selection Sort algorithm.
Answer:
1. Divide the list into sorted (left) and unsorted (right) parts.
2. Find the smallest element in the unsorted part.
3. Swap it with the leftmost element of the unsorted part.
4. Move the boundary between sorted and unsorted one element to the right.
21 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
5 Marks Questions
1. Explain Bubble Sort in detail with a suitable example. Also write its Python implementation.
Answer:
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps
them if they are in the wrong order. The process is repeated until the list is sorted.
Example: Sorting [8, 7, 13, 1, -9, 4]
Pass 1: [7,8,13,1,-9,4] → [7,8,1,13,-9,4] → [7,8,1,-9,13,4] → [7,8,1,-9,4,13]
Pass 2: [7,1,8,-9,4,13] → [7,1,-9,8,4,13] → [7,1,-9,4,8,13]
Pass 3: [1,7,-9,4,8,13] → [1,-9,7,4,8,13] → [1,-9,4,7,8,13]
Pass 4: [-9,1,4,7,8,13] → sorted
Python Implementation:
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]
numList = [8, 7, 13, 1, -9, 4]
bubble_Sort(numList)
print(numList)
23 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
def find_median(num_list):
bubble_Sort(num_list)
n = len(num_list)
if n % 2 == 1: # odd
median = num_list[n//2]
else: # even
median = (num_list[n//2 - 1] + num_list[n//2]) / 2
return median
# Example
numbers = [7, 11, 3, 10, 17]
print("Median:", find_median(numbers))
Ref: Exercise Q4
10. Explain how to calculate the 90th percentile for a list of marks using Selection Sort.
Answer:
Steps:
1. Sort the list of marks in ascending order using Selection Sort.
2. Calculate index = (90/100) * n, where n is number of students.
3. Round the index to the nearest whole number using round().
4. The mark at that index in the sorted list is the 90th percentile.
Example: For 120 students, index = 0.90 * 120 = 108 → round(108) = 108. The 108th mark in sorted order is the
90th percentile.
Ref: Exercise Q5
25 L R Mohammed Matheen
PUC COMPUTER SCIENCE STUDY MATERIAL January 2, 2026
def find_median(num_list):
bubble_Sort(num_list)
n = len(num_list)
if n % 2 == 1:
return num_list[n//2]
else:
return (num_list[n//2 - 1] + num_list[n//2]) / 2
# Example usage
numbers = [7, 11, 3, 10, 17]
print("Median:", find_median(numbers))
Ref: Exercise Q4
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 ‘x’ percentile (where x is any number between 0 and
100).
Answer:
def selection_Sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
# Example usage
marks = [85, 92, 78, 90, 88, 76, 95, 89, 84, 91]
x = 90
print(f"{x}th percentile:", calculate_percentile(marks, x))
Ref: Exercise Q5
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:
The technique being used is Insertion Sort, because each new element (name) is inserted into its correct position in the
already sorted list.
def insert_sorted(name_list, new_name):
name_list.append(new_name)
# Insertion Sort logic for one element
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
# Example usage
students = []
while True:
name = input("Enter student name (or 'stop' to end): ")
if [Link]() == 'stop':
break
insert_sorted(students, name)
print("Current list:", students)
27 L R Mohammed Matheen