Skip to main content
Chapter 5 of 13
NCERT Solutions

Sorting — NCERT Solutions

Jharkhand Board · Class 12 · Computer Science

NCERT Solutions for Sorting, Jharkhand Board Class 12 Computer Science: 6 textbook questions solved step by step.

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.

6 Questions Solved · 1 Section

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

EXERCISE — Chapter: Sorting (Class 12 Computer Science)

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

Given: numList = [7, 11, 3, 10, 17, 23, 1, 4, 21, 5]

Concept: In Bubble Sort, in each pass we compare adjacent elements and swap them if they are in the wrong order. After pass kk, the kk largest elements are placed at the end in their correct positions.


Pass 1:

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

  • Compare 7, 11 → no swap → [7, 11, 3, 10, 17, 23, 1, 4, 21, 5]
  • Compare 11, 3 → swap → [7, 3, 11, 10, 17, 23, 1, 4, 21, 5]
  • Compare 11, 10 → swap → [7, 3, 10, 11, 17, 23, 1, 4, 21, 5]
  • Compare 11, 17 → no swap → [7, 3, 10, 11, 17, 23, 1, 4, 21, 5]
  • Compare 17, 23 → no swap → [7, 3, 10, 11, 17, 23, 1, 4, 21, 5]
  • Compare 23, 1 → swap → [7, 3, 10, 11, 17, 1, 23, 4, 21, 5]
  • Compare 23, 4 → swap → [7, 3, 10, 11, 17, 1, 4, 23, 21, 5]
  • Compare 23, 21 → swap → [7, 3, 10, 11, 17, 1, 4, 21, 23, 5]
  • Compare 23, 5 → swap → [7, 3, 10, 11, 17, 1, 4, 21, 5, 23]

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


Pass 2:

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

  • Compare 7, 3 → swap → [3, 7, 10, 11, 17, 1, 4, 21, 5, 23]
  • Compare 7, 10 → no swap → [3, 7, 10, 11, 17, 1, 4, 21, 5, 23]
  • Compare 10, 11 → no swap → [3, 7, 10, 11, 17, 1, 4, 21, 5, 23]
  • Compare 11, 17 → no swap → [3, 7, 10, 11, 17, 1, 4, 21, 5, 23]
  • Compare 17, 1 → swap → [3, 7, 10, 11, 1, 17, 4, 21, 5, 23]
  • Compare 17, 4 → swap → [3, 7, 10, 11, 1, 4, 17, 21, 5, 23]
  • Compare 17, 21 → no swap → [3, 7, 10, 11, 1, 4, 17, 21, 5, 23]
  • Compare 21, 5 → swap → [3, 7, 10, 11, 1, 4, 17, 5, 21, 23]
  • Compare 21, 23 → no swap → [3, 7, 10, 11, 1, 4, 17, 5, 21, 23]

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


Pass 3:

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

  • Compare 3, 7 → no swap → [3, 7, 10, 11, 1, 4, 17, 5, 21, 23]
  • Compare 7, 10 → no swap → [3, 7, 10, 11, 1, 4, 17, 5, 21, 23]
  • Compare 10, 11 → no swap → [3, 7, 10, 11, 1, 4, 17, 5, 21, 23]
  • Compare 11, 1 → swap → [3, 7, 10, 1, 11, 4, 17, 5, 21, 23]
  • Compare 11, 4 → swap → [3, 7, 10, 1, 4, 11, 17, 5, 21, 23]
  • Compare 11, 17 → no swap → [3, 7, 10, 1, 4, 11, 17, 5, 21, 23]
  • Compare 17, 5 → swap → [3, 7, 10, 1, 4, 11, 5, 17, 21, 23]
  • Compare 17, 21 → no swap → [3, 7, 10, 1, 4, 11, 5, 17, 21, 23]

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


Final Answer: After three complete passes of Bubble Sort, 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]

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: 63 42 21 9
Show solution

Given: List = [63, 42, 21, 9]


Bubble Sort

Concept: Compare adjacent elements and swap if out of order. Count swaps and comparisons.

Pass 1: (n−1 = 3 comparisons)

  • Compare 63, 42 → swap → [42, 63, 21, 9] — Swap 1
  • Compare 63, 21 → swap → [42, 21, 63, 9] — Swap 2
  • Compare 63, 9 → swap → [42, 21, 9, 63] — Swap 3

Pass 2: (2 comparisons)

  • Compare 42, 21 → swap → [21, 42, 9, 63] — Swap 4
  • Compare 42, 9 → swap → [21, 9, 42, 63] — Swap 5

Pass 3: (1 comparison)

  • Compare 21, 9 → swap → [9, 21, 42, 63] — Swap 6

Bubble Sort:

  • Total Swaps = 6
  • Total Comparisons = 3+2+1=3 + 2 + 1 = 6

Selection Sort

Concept: Find the minimum element from the unsorted part and swap it with the first element of the unsorted part.

Pass 1: Find minimum in [63, 42, 21, 9] → min = 9 (index 3)

  • Comparisons: 3 (compare 63 with 42, 21, 9)
  • Swap 63 and 9 → [9, 42, 21, 63] — Swap 1

Pass 2: Find minimum in [42, 21, 63] → min = 21 (index 2)

  • Comparisons: 2
  • Swap 42 and 21 → [9, 21, 42, 63] — Swap 2

Pass 3: Find minimum in [42, 63] → min = 42 (index 2)

  • Comparisons: 1
  • No swap needed (already in place) — Swap 0

Selection Sort:

  • Total Swaps = 2
  • Total Comparisons = 3+2+1=3 + 2 + 1 = 6

Comparison Table

TechniqueSwapsComparisons
Bubble Sort66
Selection Sort26

Conclusion: Both techniques require the same number of comparisons (6). However, Selection Sort is better because it requires only 2 swaps compared to 6 swaps in Bubble Sort. Fewer swaps mean less data movement, making Selection Sort more efficient for this list.

3Consider the following lists:
List 1: 2 3 5 7 11
List 2: 11 7 5 3 2
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

Given:

  • List 1: [2, 3, 5, 7, 11] (already sorted in ascending order)
  • List 2: [11, 7, 5, 3, 2] (sorted in descending order — worst case)

Concept: In Insertion Sort, each element is picked and inserted at its correct position in the already-sorted portion. If the list is already sorted, each new element only needs 1 comparison (with its immediate predecessor). If the list is in reverse order, each new element needs to be compared with all elements in the sorted portion.


List 1: [2, 3, 5, 7, 11] — Already Sorted (Best Case)

PassElement PickedSorted PortionComparisonsResult
13[2]1 (3 > 2, no shift)[2, 3, 5, 7, 11]
25[2, 3]1 (5 > 3, no shift)[2, 3, 5, 7, 11]
37[2, 3, 5]1 (7 > 5, no shift)[2, 3, 5, 7, 11]
411[2, 3, 5, 7]1 (11 > 7, no shift)[2, 3, 5, 7, 11]

Total Comparisons for List 1 = 1 + 1 + 1 + 1 = 4


List 2: [11, 7, 5, 3, 2] — Reverse Sorted (Worst Case)

PassElement PickedSorted PortionComparisonsResult
17[11]1 (7 < 11, shift 11)[7, 11, 5, 3, 2]
25[7, 11]2 (5 < 11, 5 < 7, shift both)[5, 7, 11, 3, 2]
33[5, 7, 11]3 (3 < 11, 3 < 7, 3 < 5, shift all)[3, 5, 7, 11, 2]
42[3, 5, 7, 11]4 (2 < 11, 2 < 7, 2 < 5, 2 < 3, shift all)[2, 3, 5, 7, 11]

Total Comparisons for List 2 = 1 + 2 + 3 + 4 = 10


Conclusion: List 1 makes the minimum number of comparisons (4) because it is already sorted in ascending order, which is the best case for Insertion Sort. List 2 requires 10 comparisons as it is in reverse order (worst case).

In general:

  • Best case complexity of Insertion Sort = O(n)O(n) → when list is already sorted.
  • Worst case complexity of Insertion Sort = O(n2)O(n^2) → when list is in reverse order.
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 to 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 'x-th' percentile (where x is any number between 0 and 100).

Steps:
I. Order all values from smallest to largest using Selection Sort.
II. Calculate index by multiplying x percent by the total number of values, n.
III. Ensure that the index is a whole number by using math.round().
IV. Display the value at the index obtained in Step 3.

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 Jharkhand Board 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 Jharkhand Board 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 Jharkhand Board 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 Jharkhand Board Class 12 Computer Science. Free to start, no card needed.