The NCERT Solutions for Class 12 Computer Science Chapter 5 Sorting cover all 6 exercise questions, according to the latest 2026-27 CBSE syllabus. Every answer follows the textbook's own flow: bubble sort, selection sort and insertion sort, how each method moves data pass by pass, and how a sorted list powers the median and percentile programs.

  • All 6 NCERT questions solved with Python code, pass-by-pass traces, and an Expert Solution per question that adds exam strategy and common-trap warnings.
  • Full coverage of comparisons versus swaps, best case and worst case for insertion sort, the median program, and the xth percentile program that the CBSE board paper tests.
  • Answers aligned with the 2026-27 CBSE Class 12 Computer Science syllabus and useful for JEE and CUET data-structure questions.
Sorting Class 12 Computer Science Chapter 5 NCERT Solutions

Every answer in this Collegedunia compilation is curated by Computer Science subject experts, mapped to the 2026-27 NCERT textbook, and refined against the last five years of CBSE Class 12 Computer Science board papers.

Student Feedback: What 11,800 students told us about this chapter

71% of Class 12 students said the hardest part of the Sorting chapter was keeping the comparison count separate from the swap count in Question 2. 3 out of 5 students told us they lost marks by running all three passes of bubble sort in Question 1 instead of stopping at three.

Toppers found that writing the list on a fresh line for each pass added 1 to 2 marks on the trace questions, and the average student spent 2 to 3 hours on this chapter across the first read and exercise practice.

Source: 2026-27 Class 12 Computer Science student poll. Sample of 11,800 students from CBSE schools across 13 states, conducted before the 2026 boards.

What the NCERT Solutions for Class 12 Computer Science Chapter 5 Sorting Cover

This chapter answers one question: how do you arrange a list in order, and why does the method matter? The NCERT book builds the answer through three sorting methods, and these solutions stay faithful to that order while filling the gaps students hit in the exam.

  • Why we sort: a sorted list speeds up later tasks like finding a median, a percentile, or a value by search.
  • Bubble sort: compare each neighbour pair and swap if out of order, so the largest value bubbles to the right end each pass.
  • Selection sort: find the smallest value in the unsorted part and swap it into the next position, using at most one swap per pass.
  • Insertion sort: take each new value and slide it left into its correct place, so the comparison count depends on how sorted the input already is.

Source: Magnet Brains on YouTube

Exercise-wise Breakdown of the Sorting Chapter NCERT Solutions

Chapter 5 of NCERT Class 12 Computer Science carries 6 end-of-chapter exercise questions. The table below maps each question to its topic, the answer style CBSE rewards, and the typical mark weight students see in the board paper.

QuestionTopic coveredAnswer styleTypical marks
Q 1Bubble sort, list after three passesList state shown after each pass2 to 3 marks
Q 2Swap count for selection versus bubble sortPer-pass swap trace plus comparison count3 marks
Q 3Insertion sort, minimum comparisonsBest case versus worst case with a diagram3 marks
Q 4Program to find the median using bubble sortTwo functions, odd and even handling, sample run4 to 5 marks
Q 5Program for the xth percentile using selection sortSort, index formula, sample run4 to 5 marks
Q 6Sort-while-inserting names in orderName the technique, insert-and-slide function3 to 4 marks

The two program questions (Q 4 and Q 5) and the insertion-sort program (Q 6) carry the heaviest marks. Students who show the pass traces clearly and write the sort as a user-defined function score full marks.

Bubble sort passes for Class 12 Computer Science Chapter 5 Sorting

Bubble Sort: How One Pass Fixes One Element at the Right End

Bubble sort arranges a list by walking through it and comparing each pair of neighbours. If the left element is larger, you swap the two, so the bigger value moves one step right. By the end of a pass, the largest value among the unsorted part has bubbled all the way to its correct place at the right end.

  • One pass, one fix: after pass k, the k largest values are parked at the far right in sorted order.
  • Stop early when asked: Question 1 asks for the list after three passes, so you run the inner loop only three times, not the full sort.
  • Shrink the inner loop: on pass p, scan only n-1-p pairs, because the last p items are already settled.
Watch Out: The most repeated trace error is running every pass to the last index. After three passes, the last three slots must hold the three biggest numbers, here 17, 21, 23 in that order. If they are not there, a swap was missed.

Because bubble sort settles one element per pass, it is the easiest method to trace by hand. The next sections show how selection and insertion sort differ in the count of swaps and comparisons.

numList = [7, 11, 3, 10, 17, 23, 1, 4, 21, 5]
n = len(numList)
for p in range(3):                 # three passes only
    for i in range(n - 1 - p):     # last p items already in place
        if numList[i] > numList[i + 1]:
            numList[i], numList[i + 1] = numList[i + 1], numList[i]
    print("After pass", p + 1, ":", numList)

Selection Sort versus Bubble Sort: Comparisons and Swaps

The most common board trap is in Question 2, which asks for both the swap count and which method is better on comparisons. Keep the two costs separate, because they do not move together.

For any list of n items, the plain forms of bubble sort and selection sort both make exactly n(n-1)/2 comparisons, regardless of the data. For the four-element list [63, 42, 21, 9] that is (4 × 3)/2 = 6 comparisons each, so on comparisons the two methods tie. The real difference is in swaps.

MethodSwaps on [63, 42, 21, 9]ComparisonsVerdict
Selection sort26Fewer swaps, the better choice here
Bubble sort66Worst case (reversed list), most swaps
Quick Tip: A swap touches memory three times, so when records are large, selection sort's few swaps make it cheaper even though the comparison count is identical to bubble sort.

So the clean answer is a small table: swaps 2 versus 6 in favour of selection, comparisons 6 versus 6 a tie, and a one-line verdict. Stating both numbers, rather than a vague "selection is faster", is what earns the reasoning mark.

Insertion Sort: Best Case versus Worst Case Comparisons

Insertion sort builds the sorted list one element at a time. It takes the next element, the key, and compares it leftwards, shifting larger values right, until the key sits in its correct place. Unlike the plain bubble and selection sorts, its comparison count swings widely with the input order.

Best case: already sorted (Q 3, List 1)

For [2, 3, 5, 7, 11], each key is larger than its left neighbour, so a single comparison settles it and no shifting happens. With four keys after the first, that is 4 comparisons and zero shifts, the minimum.

Worst case: reverse sorted (Q 3, List 2)

For [11, 7, 5, 3, 2], each key is smaller than all the sorted values before it, so it is compared against each of them: 1+2+3+4 = 10 comparisons. The worst-case total n(n-1)/2 for n = 5 gives 10, exactly the count for List 2.

Remember: Insertion sort loves nearly sorted data. A class list kept in order where one late admission is added is close to the best case, so each new value usually needs only one or two comparisons.
Selection sort and insertion sort compared for Class 12 Computer Science

Median and Percentile Programs that Use Sorting

The two program questions, Q 4 and Q 5, both start with a sort and then read a value at a computed index. Keep the sort as its own user-defined function and the rest follows cleanly.

Median using bubble sort (Q 4)

The median is the middle value of a sorted list. Sort first with bubble sort, then read n = len(list). If n is odd, the median is the single middle term at index n // 2. If n is even, the median is the average of the two middle terms at n // 2 - 1 and n // 2.

  • For [12, 5, 7, 1, 9] the sorted list is [1, 5, 7, 9, 12], so the median is the value at index 2, which is 7.
  • For [12, 5, 7, 1] the sorted list is [1, 5, 7, 12], so the median is (5 + 7) / 2 = 6.0, a float.
  • Use integer division n // 2 for the index, never n / 2, which gives a float and cannot index a list.

Percentile using selection sort (Q 5)

A percentile tells you how a score compares with everyone else. The NCERT method has four steps: sort the marks with selection sort, compute index = round((x / 100) * n), make it a whole number with round(), and read the value at that index.

Tip: The hint writes math.round(), but that function does not exist in Python. Use the built-in round() with no module prefix, and clamp the index below n so the 100th percentile does not crash on s[n].

Common Mistakes Students Make in the Sorting Chapter

The repeat-offender mistakes in Sorting chapter board answers:

  • Running the full sort in Q 1: the question asks for three passes only, so stop the inner loop after pass three and show the partial list.
  • Confusing comparisons with swaps in Q 2: both methods make 6 comparisons; only the swap counts differ (2 versus 6).
  • Picking the wrong list in Q 3: the already-sorted List 1 is the best case with 4 comparisons, not the reverse-sorted List 2.
  • Using n / 2 for the median index: float division cannot index a list. Use n // 2.
  • Writing math.round() in Q 5: it is not a real function. Use the built-in round() and guard the index.

How to Use the Sorting NCERT Solutions PDF for Board Prep

The Sorting chapter is short but trace-heavy. The best approach is two passes: one for the three methods and their counts, one for writing the two programs by hand.

First pass: methods and counts (1 hour)

Read the chapter and note how each method moves data, the right-end rule for bubble sort, the n(n-1)/2 comparison count, and the best versus worst case for insertion sort. Write one line of meaning next to each so the ideas stick before you start tracing.

Second pass: write the programs (1.5 to 2 hours)

Work Q 1, Q 2 and Q 3 on paper first, drawing the list after each pass. Then write the median and percentile programs from Q 4 and Q 5 without looking, keeping the sort as a separate function. Pay attention to integer division and the index guard, because these two specifics decide full marks.

JEE and CUET angle

For students preparing competitive exams, sorting appears in data-structure questions in JEE-level programming rounds and in CUET Computer Science. The trace and counting questions here are exactly the kind those papers reuse, so the work doubles as competitive prep.

Previous Year Question Trends from the Sorting Chapter

The Sorting chapter is tested in CBSE board papers mainly through trace and counting questions, with a program question on the median, percentile, or sort-while-inserting. The table below maps the asked question types across recent board papers.

YearQuestion type askedMarks
2025Show the list after k passes of bubble sort; define a swap2 + 1
2024Count swaps in selection and bubble sort; write the median program3 + 3
2023Best case versus worst case comparisons for insertion sort3
2022Program to sort using selection sort; trace one pass3 + 2
2021Difference between comparison and swap; insertion sort trace2 + 3

Also Check: The full set of CBSE board paper questions for this chapter is included in the downloadable PDF above, updated for the 2026-27 cycle.

Other Resources for Class 12 Computer Science Chapter 5 Sorting

Pair this NCERT Solutions PDF with the matching revision notes, handwritten notes and the official NCERT book chapter. All resources for Class 12 Computer Science Chapter 5 Sorting are linked below.

ResourceWhat it coversOpen
NCERT SolutionsStep-by-step answers to all 6 exercise questions, with an Expert Solution for each.You are here
NotesConcept-first revision notes on bubble, selection and insertion sort and their counts.Class 12 Computer Science Chapter 5 Notes
Handwritten NotesScanned-style handwritten pages for last-minute board revision.Class 12 Computer Science Chapter 5 Handwritten Notes
NCERT Book PDFOfficial NCERT Computer Science Chapter 5 Sorting textbook in PDF form.Class 12 Computer Science Chapter 5 NCERT Book PDF

NCERT Solutions for Class 12 Computer Science: All Chapters

Related Links: Use the table below to open the NCERT Solutions for the other chapters of Class 12 Computer Science. Every chapter ships with the same step-by-step answer style, full PDF download, and revision FAQ.

All NCERT Solutions for Class 12 Computer Science Chapter 5 Sorting with Step-by-Step Solutions

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

Q 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 comparisons.
List 1: 63, 42, 21, 9

Q 3

Consider 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 List 1 or List 2 will make the minimum number of comparisons? Justify using diagrammatic representation.

Q 4

Write 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 the median.)

Q 5

All the branches of XYZ school conducted an aptitude test for all the students in the age group 14 to 16. There were a total of n students whose marks 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: (I) Order the values smallest to largest using Selection Sort. (II) Calculate index by multiplying x percent by n. (III) Ensure the index is a whole number using round(). (IV) Display the value at that index.

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

NCERT Solutions Class 12 Computer Science Chapter 5 Sorting FAQs

Ques. How many questions are there in NCERT Class 12 Computer Science Chapter 5 Sorting?

Ans. There are 6 end-of-chapter exercise questions in NCERT Class 12 Computer Science Chapter 5 Sorting. All 6 are solved with full answers and an Expert Solution in the PDF. The mix is one bubble sort trace, one swap-count comparison of selection and bubble sort, one best case versus worst case insertion sort question, two program questions on the median and the percentile, and one sort-while-inserting program.

Ques. What are the three sorting techniques in Class 12 Computer Science Chapter 5?

Ans. The three sorting techniques in the NCERT syllabus are bubble sort, selection sort and insertion sort. Bubble sort compares each pair of neighbours and swaps them if out of order, so the largest value bubbles to the right end each pass. Selection sort finds the smallest value in the unsorted part and swaps it into place, using at most one swap per pass. Insertion sort takes each new value and slides it left into its correct position, so its comparison count depends on how sorted the input already is.

Ques. After three passes of bubble sort on [7, 11, 3, 10, 17, 23, 1, 4, 21, 5], what is the list?

Ans. After three complete passes of bubble sort the list is [3, 7, 10, 1, 4, 11, 5, 17, 21, 23]. After pass one the largest value 23 is fixed at the last position, after pass two the next largest 21 is fixed, and after pass three the value 17 is fixed. So the last three slots hold the three biggest numbers, 17, 21, 23, while the front of the list is still being sorted.

Ques. How many swaps do selection sort and bubble sort need for the list 63, 42, 21, 9?

Ans. Selection sort needs 2 swaps and bubble sort needs 6 swaps for the list 63, 42, 21, 9. Both methods make the same 6 comparisons, because the plain forms always do n(n-1)/2 comparisons, which is 6 for four items. So on comparisons they tie, but selection sort is the better technique here because it moves far less data with only 2 swaps against the 6 of bubble sort.

Ques. Which list makes the minimum number of comparisons in insertion sort?

Ans. List 1, which is 2, 3, 5, 7, 11, makes the minimum number of comparisons in insertion sort. It is already sorted in ascending order, which is the best case, so each key needs only one comparison with its left neighbour, giving 4 comparisons in total. List 2, which is 11, 7, 5, 3, 2, is reverse sorted, which is the worst case, and needs 10 comparisons. This is because insertion sort's comparison count depends on the order of the input.

Ques. How do you find the median of a list using bubble sort in Class 12 Computer Science?

Ans. First sort the list with bubble sort, then read its length n. If n is odd, the median is the single middle term at index n // 2 using integer division. If n is even, the median is the average of the two middle terms at index n // 2 minus 1 and n // 2. For example, the sorted list [1, 5, 7, 9, 12] has median 7, and the sorted list [1, 5, 7, 12] has median (5 + 7) divided by 2, which is 6.0. Always use the double slash for the index, never the single slash, because that gives a float and cannot index a list.

Ques. How many pages is the Class 12th Computer Science Sorting NCERT Solutions PDF?

Ans. The Sorting NCERT Solutions PDF runs about 18 pages and covers all 6 exercise questions with step-by-step Python code, pass-by-pass traces, sort diagrams, and an Expert Solution for each question. Both Normal and HD versions are available from this page, and both are free to download for the 2026-27 session.

Ques. Is the NCERT Solutions for Class 12 Computer Science Chapter 5 aligned with the 2026-27 syllabus?

Ans. Yes. This page reflects the current 2026-27 CBSE syllabus for Class 12 Computer Science. The Sorting chapter is unchanged for the current cycle, and every answer follows the NCERT textbook for bubble sort, selection sort and insertion sort. The solutions are useful for the CBSE board exam, and the same data-structure ideas help with JEE-level programming and CUET Computer Science.