Skip to main content
Chapter 5 of 13
NCERT Solutions

Sorting — NCERT Solutions

CBSE · Class 12 · Computer Science

NCERT question answers for Sorting, CBSE Class 12 Computer Science: 6 textbook questions solved step by step. Covers Exercise.

44 questions60 flashcards3 formulas & key relations5 concepts

Interactive on Super Tutor

Studying Sorting? Get the full interactive chapter.

Quizzes, flashcards, AI doubt-solver and a step-by-step study plan — built for NCERT solutions and more.

Free trial, no card needed.

A concept map illustrating the definition of sorting, its purpose (e.g., ease of searching), and examples of different sorting orders (ascending, descending, alphabetical, by length).
Super Tutor

Learn better with visuals Super Tutor pairs illustrations like this with notes and quizzes for Sorting.

Get started
6 Questions Solved · 1 Section

The first 3 solutions are open to read. The other 3 are free with a Super Tutor account. Unlock all 6 free in Super Tutor

Exercise

1Consider 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.
Show solution

Use Bubble Sort on the list
[7,11,3,10,17,23,1,4,21,5][7, 11, 3, 10, 17, 23, 1, 4, 21, 5].

  • Pass 1: largest element moves to the end.

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

  • Pass 2: next largest moves to its position.

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

  • Pass 3: next largest moves to its position.

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

So after three complete passes, the partially sorted list is:
[3,7,10,1,4,11,5,17,21,23][3, 7, 10, 1, 4, 11, 5, 17, 21, 23].

Not sure why a step works? check your working in Super Tutor

2Identify 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 comparisons.
List 1 :6342219
Show solution

For the list [63, 42, 21, 9]:

  • Selection sort makes n - 1 = 3 passes.
  • In each pass, it finds the smallest element from the unsorted part and swaps it into place.

Selection sort swaps

  1. Pass 1: smallest is 9 → swap with 63
  • [9, 42, 21, 63]
  1. Pass 2: smallest in remaining list is 21 → swap with 42
  • [9, 21, 42, 63]
  1. Pass 3: remaining elements are already in order, so no swap needed.

So, selection sort requires 2 swaps.

Bubble sort swaps

Bubble sort swaps adjacent elements whenever they are out of order.
For a list in descending order, every adjacent comparison causes a swap until the list becomes sorted.

For 4 elements, the number of swaps needed is:

  • Pass 1: 3 swaps
  • Pass 2: 2 swaps
  • Pass 3: 1 swap

Total swaps = 3 + 2 + 1 = 6

So, bubble sort requires 6 swaps.

Better technique with respect to comparisons

From the chapter, both selection sort and bubble sort use nested loops and have n2n^2 time complexity, so for this small list both make the same order of comparisons. However, selection sort is better with respect to swaps, and in this case it also uses fewer swaps than bubble sort.

Thus, the better sorting technique here is selection sort.

Not sure why a step works? check your working in Super Tutor

3Consider the following lists:
List 1:235711
List 2:117532

If the lists are sorted using Insertion sort then which of the lists List1 or List 2 will make the minimum number of comparisons? Justify using diagrammatic representation.

Show solution

For Insertion sort, the number of comparisons depends on how ordered the list already is.

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

This list is already in ascending order.

  • In each pass, the next element is already in the correct place.
  • So insertion sort makes the minimum number of comparisons.

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

This list is in descending order.

  • Every new element must be compared with all the elements in the sorted part.
  • So it makes the maximum number of comparisons.

Justification

  • List 1 → best case for insertion sort → minimum comparisons
  • List 2 → worst case for insertion sort → more comparisons

Therefore, List 1 will make the minimum number of comparisons.

Diagrammatically:

  • List 1: 2 | 3 | 5 | 7 | 11 → already sorted, so each next element stays in place.
  • List 2: 11 | 7 | 5 | 3 | 2 → each element must move left across the sorted part.

Not sure why a step works? check your working in Super Tutor

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

Free with a Super Tutor account

5All 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^{th}' percentile (where x is any number between 0 and 100). You are required to perform the following steps to be able to calculate the 'xth^{th}' percentile.

Note: Percentile is a measure of relative performance i.e. It is calculated based on a candidate's performance with respect to others. For example : If a candidate's score is in the 90th^{th} percentile, that means she/he scored better than 90% of people who took the test.

Steps to calculate the xth^{th} percentile:

I. Order all the values in the data set from smallest to largest using Selection Sort. In general any of the sorting methods can be used.
II. Calculate index by multiplying x percent by the total number of values, n.

For example: to find 90th^{th} percentile for 120 students:

0.90×120=1080.90 \times 120 = 108

III. Ensure that the index is a whole number by using math.round()
IV. Display the value at the index obtained in Step 3.

The corresponding value in the list is the xth^{th} percentile.

Free with a Super Tutor account

6During 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.

Free with a Super Tutor account

3 more solved questions in Sorting

They are free with a Super Tutor account, along with practice quizzes and flashcards for this chapter. Free to start, no card needed.

Frequently Asked Questions

What are the important topics in Sorting for CBSE Class 12 Computer Science?
Key topics in Sorting include Core idea of sorting, Bubble Sort, Selection Sort, Insertion Sort. Study these first, then practise questions on each for the CBSE Class 12 board exam.
Are these NCERT Solutions for Sorting free?
The first 3 of the 6 solutions on this page are open to read. The other 3 are free with a Super Tutor account — signing up is free and needs no card.
How should I revise Sorting for the CBSE Class 12 board exam?
Learn the core ideas first, then work through the 44 practice questions on Sorting. Revise definitions regularly and use flashcards for quick recall before the exam.

Sources & Official References

Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.

For serious students

Get the full Sorting chapter — start free.

Quizzes, flashcards, an AI doubt solver and a study plan for CBSE Class 12 Computer Science. Free to start, no card needed.