Skip to main content
Chapter 5 of 13
NCERT Solutions

Sorting

CBSE · Class 12 · Computer Science

NCERT Solutions for Sorting — CBSE Class 12 Computer Science.

44 questions60 flashcards5 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.

1,000+ Class 12 students started this chapter today

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

This is just one of 9+ visuals inside Super Tutor's Sorting chapter

Explore the full set
6 Questions Solved · 1 Section

3 worked solutions below. 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.Show solution
The list is [63,42,21,9][63, 42, 21, 9].

## Selection sort
Selection sort places the smallest element at its correct position in each pass.

- Pass 1: smallest is 99, swap with 6363 → 1 swap
- Pass 2: smallest of remaining [42,21,63][42, 21, 63] is 2121, swap with 4242 → 1 swap
- Pass 3: smallest of remaining [42,63][42, 63] is 4242, already in place → 0 swap

So, selection sort requires 2 swaps.

Number of comparisons in selection sort for n=4n=4:
3+2+1=63 + 2 + 1 = 6

## Bubble sort
Bubble sort repeatedly compares adjacent elements and swaps if they are unordered.

For [63,42,21,9][63, 42, 21, 9]:
- Comparisons in pass 1: 3, swaps: 3
- Comparisons in pass 2: 2, swaps: 2
- Comparisons in pass 3: 1, swaps: 1

So, bubble sort requires 6 swaps.

Number of comparisons in bubble sort for n=4n=4:
3+2+1=63 + 2 + 1 = 6

## Conclusion
- Selection sort swaps = 2
- Bubble sort swaps = 6
- With respect to the number of comparisons, both make 6 comparisons for 4 elements.
- But considering swaps, selection sort is better because it uses fewer swaps.

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

3Consider the following lists:Show solution
For Insertion Sort, the number of comparisons is minimum when the list is already in ascending order, because each new element is inserted without much shifting.

- List 1: [2,3,5,7,11][2, 3, 5, 7, 11] is already sorted in ascending order.
- List 2: [11,7,5,3,2][11, 7, 5, 3, 2] is in descending order, so every element must be compared and shifted.

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

Diagrammatically:
- List 1: each element is already in correct order, so only direct checks are needed.
- List 2: each new element moves through the whole sorted part, causing many comparisons.

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)
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.
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.

3 more solved questions in Sorting

Every remaining exercise is solved step by step in Super Tutor, plus practice quizzes and flashcards for this chapter. Free to start.

Stuck on a step?

Ask Super Tutor AI to explain any solution on this page in a simpler way — free, 24x7.

Ask a Doubt Free

Frequently Asked Questions

What are the important topics in Sorting for CBSE Class 12 Computer Science?
Sorting covers several key topics that are frequently asked in CBSE Class 12 board exams. Focus on the core concepts listed on this page and practise related questions to build confidence.
How to score full marks in Sorting — CBSE Class 12 Computer Science?
Understand the core concepts first, then work through the 44 practice questions available for this chapter. Revise formulas and definitions regularly, and use flashcards for quick recall before the exam.
Where can I get free NCERT Solutions for Sorting Class 12 Computer Science?
This page has free step-by-step NCERT Solutions for every exercise question in Sorting (CBSE Class 12 Computer Science) — written the way examiners award marks: given, formula, working, answer.

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 — for free.

Quizzes, flashcards, AI doubt-solver and a step-by-step study plan for CBSE Class 12 Computer Science.