Curriculum
Course: Grade XII Computer Science
Login
Text lesson

Ch-5 Sorting

School Revise · Class 11 Computer Science · Chapter 5

Sorting

Code 083, Class 12. Sorting arranges items in order. Here we use built-in sorting and build a simple bubble sort of our own.

sort

arranges a list

bubble

a simple method

What this chapter is about

Sorting puts items in order, smallest to largest or A to Z. Python can sort for us, and we can also write our own sort to see how it works.

1. Built-in sorting

list.sort() sorts the list itself, and sorted(list) returns a new sorted list. Add reverse=True for descending order.

nums = [3, 1, 2] nums.sort() print(nums) Output: [1, 2, 3]

2. Bubble sort

Bubble sort compares each pair of neighbours and swaps them if they are in the wrong order, passing through the list again and again until it is sorted.

3. When we sort

Sorting makes data easy to read and lets us search it quickly, for example arranging marks from highest to lowest, or names in order.

Practise with the interactive

Explore the idea by tapping. The interactive opens right here in the lesson.

Try it in the code lab

Write and run real Python right here in the lesson, then work through the practice problems with answers.

Starting Python...

Coding practice problems, with answers

Type each one into the code lab above, then open the card to see the worked solution and its output.

Problem 1. Sort a list of numbers.

SOLUTION

nums = [3, 1, 2] nums.sort() print(nums)

OUTPUT

[1, 2, 3]
Problem 2. Sort in descending order.

SOLUTION

nums = [3, 1, 2] nums.sort(reverse=True) print(nums)

OUTPUT

[3, 2, 1]
Problem 3. Use sorted() to make a new list.

SOLUTION

a = [5, 2, 8] b = sorted(a) print(b)

OUTPUT

[2, 5, 8]
Problem 4. Sort words alphabetically.

SOLUTION

words = [“mango”, “apple”, “banana”] words.sort() print(words)

OUTPUT

[‘apple’, ‘banana’, ‘mango’]
Problem 5. Find the smallest by sorting.

SOLUTION

nums = [7, 3, 9, 1] nums.sort() print(nums[0])

OUTPUT

1
Problem 6. Find the largest (last after sorting).

SOLUTION

nums = [7, 3, 9, 1] nums.sort() print(nums[-1])

OUTPUT

9
Problem 7. Write a bubble sort.

SOLUTION

nums = [3, 1, 2] n = len(nums) for i in range(n): for j in range(n – 1 – i): if nums[j] > nums[j + 1]: nums[j], nums[j + 1] = nums[j + 1], nums[j] print(nums)

OUTPUT

[1, 2, 3]
Problem 8. Sort a list, then reverse it.

SOLUTION

nums = [3, 1, 2] nums.sort() nums.reverse() print(nums)

OUTPUT

[3, 2, 1]
Problem 9. Find the minimum in one pass.

SOLUTION

nums = [7, 3, 9, 1] small = nums[0] for x in nums: if x < small: small = x print(small)

OUTPUT

1
Problem 10. Check if a list is already sorted.

SOLUTION

nums = [1, 2, 3] print(nums == sorted(nums))

OUTPUT

True

Practice set A, multiple choice

1. Which method sorts a list in place?

sort().

2. Which returns a new sorted list?

sorted().

3. What does reverse=True do?

Sorts in descending order.

4. Bubble sort works by comparing and swapping …

neighbouring items.

Quick summary

Sorting arranges items in order. list.sort() sorts in place and sorted() returns a new sorted list; reverse=True gives descending order. Bubble sort compares neighbours and swaps them if out of order, repeating until sorted. Sorting makes data readable and quick to search.

Open the Virtual Lab

These free Class 12 Computer Science notes explain sorting, the built-in sort and sorted functions, ascending and descending order, and the bubble sort algorithm with worked examples and practice, for CBSE students across India and the Gulf including the UAE, Saudi Arabia, Qatar, Oman, Kuwait and Bahrain.

© 2026 School Revise. All rights reserved. Original content aligned to the CBSE and NCERT Class 12 Computer Science syllabus. Unauthorised copying is not permitted.

Layer 1
Login Categories