Sorting in Class 12 Computer Science: Complete Guide to Selection Sort
By ConceptScroll Team · Published on 17 July 2026 · 6 min read

Sorting is a fundamental topic in Class 12 NCERT Computer Science that involves arranging data in a specific order. This blog explains Sorting with a detailed look at Selection Sort, helping students grasp the concept clearly and prepare effectively for exams.
What is Sorting and Why is it Important in Class 12 Computer Science?
Sorting is the process of arranging data in a particular order, usually ascending or descending. In Class 12 NCERT Computer Science, sorting helps organize data for efficient searching, analysis, and presentation. It is a basic algorithmic concept that forms the foundation for more complex operations.
Sorting is essential because:
- It helps in quick data retrieval.
- It simplifies data analysis.
- It is used in percentile calculations and ranking.
- It improves the efficiency of other algorithms like searching.
Understanding sorting prepares students for programming and problem-solving tasks in computer science.
Selection Sort: Concept and Working Explained
Selection Sort is a simple sorting technique taught in Class 12 NCERT Computer Science. It works by dividing the list into two parts:
- Sorted sublist: Initially empty, grows from left to right.
- Unsorted sublist: Contains all elements at the start.
The algorithm repeatedly selects the smallest element from the unsorted sublist and swaps it with the leftmost element of the unsorted sublist. This process continues for $n-1$ passes for a list of $n$ elements.
Step-by-step working:
1. Start with the first element as the minimum. 2. Compare it with the rest to find the smallest element. 3. Swap the smallest element with the first element. 4. Move the boundary of the sorted sublist one step right. 5. Repeat until the entire list is sorted.
This method ensures the left side gradually becomes sorted, while the right side shrinks.
Example:
Consider the list: [8, 7, 13, 1, -9, 4]
- Pass 1: Smallest is
-9; swap with8→[-9, 7, 13, 1, 8, 4] - Pass 2: Smallest in unsorted is
1; swap with7→[-9, 1, 13, 7, 8, 4] - Pass 3: Smallest is
4; swap with13→[-9, 1, 4, 7, 8, 13] - Pass 4: Smallest is
7; already in place. - Pass 5: Smallest is
8; already in place.
The list is now sorted.
Want to test yourself on Sorting? Try our free quiz →
Selection Sort Algorithm and Python Implementation
The Selection Sort algorithm uses nested loops:
- Outer loop runs from the first to the second last element.
- Inner loop finds the minimum element in the unsorted part.
- Swap is done only if the minimum is not already at the current position.
Python code example:
```python numList = [8, 7, 13, 1, -9, 4]
for i in range(len(numList) - 1): min_index = i for j in range(i + 1, len(numList)): if numList[j] < numList[min_index]: min_index = j if min_index != i: numList[i], numList[min_index] = numList[min_index], numList[i]
print("Sorted list:", numList) ```
Explanation:
min_indexstores the index of the smallest element found.- Inner loop compares elements to find the minimum.
- Swap happens only if a smaller element is found.
This code sorts the list in ascending order using Selection Sort.
Time Complexity and Efficiency of Selection Sort
Selection Sort has a time complexity of $O(n^2)$ because of its nested loops:
- The outer loop runs $n-1$ times.
- The inner loop runs decreasingly from $n-1$ to 1.
Number of comparisons:
$$rac{n(n-1)}{2}$$
Number of swaps:
At most $n-1$ swaps, which is fewer than Bubble Sort.
| Algorithm | Comparisons | Swaps | Best Use Case |
|---|---|---|---|
| Selection Sort | $O(n^2)$ | $O(n)$ | Small datasets, costly swaps |
| Bubble Sort | $O(n^2)$ | $O(n^2)$ | Simple implementation |
Advantages:
- Performs fewer swaps than Bubble Sort.
- Easy to understand and implement.
Disadvantages:
- Not efficient for large datasets.
- High number of comparisons.
Selection Sort is suitable when memory writes are expensive or for small data.
Comparison with Other Sorting Techniques: Bubble Sort and Insertion Sort
Understanding how Selection Sort compares with other sorting algorithms helps Class 12 students choose the right method.
| Feature | Selection Sort | Bubble Sort | Insertion Sort |
|---|---|---|---|
| Method | Select minimum, swap once | Repeated swaps of adjacent | Insert element in sorted part |
| Comparisons | $O(n^2)$ | $O(n^2)$ | $O(n^2)$ |
| Swaps | $O(n)$ | $O(n^2)$ | $O(n^2)$ (worst case) |
| Best Case | $O(n^2)$ | $O(n)$ | $O(n)$ |
| Use Case | Small data, fewer swaps | Simple, educational | Nearly sorted data |
Insertion Sort builds the sorted list by inserting each new element at its correct position, unlike Selection Sort which swaps after finding the minimum.
Example:
Insertion Sort is used when student names are inserted in ascending order during admission, maintaining sorted order dynamically.
Practical Activities and Exercises for Class 12 Students
To strengthen understanding of Sorting, Class 12 students can try these activities:
- Activity 1: Apply Bubble Sort to
numList2 = [8, 7, 6, 5, 4]and identify the pass where the last swap occurs. - Activity 2: Perform Selection Sort on
randList = [7, 11, 3, 10, 17, 23, 1, 4, 21, 5]and note the partially sorted list after four passes. - Activity 3: Write a Python function to insert a student’s name into a sorted list, maintaining ascending order (Insertion Sort concept).
These exercises help students visualize sorting steps and improve coding skills.
Worked Example:
Calculate the 75th percentile index in a dataset of 200 values:
$$ ext{Index} = ext{math.round}(0.75 imes 200) = 150$$
The data must be sorted first using Selection Sort before accessing the 150th element.
Frequently asked questions
What is Sorting in Class 12 Computer Science?
Sorting is arranging data in a specific order, usually ascending or descending, to simplify data handling.
How does Selection Sort work?
Selection Sort divides the list into sorted and unsorted parts, repeatedly selecting the smallest element to swap into place.
What is the time complexity of Selection Sort?
Selection Sort has a time complexity of O(n²) due to its nested loops for comparisons.
When is Selection Sort preferred over other sorting algorithms?
It is preferred for small datasets or when minimizing swaps is important, as it performs fewer swaps than Bubble Sort.
How is Insertion Sort different from Selection Sort?
Insertion Sort inserts elements into the sorted sublist at the correct position, while Selection Sort swaps the minimum element.
Can you give a simple Python example of Selection Sort?
Yes, Selection Sort uses nested loops to find the minimum element and swap it with the current position in the list.
Ready to ace this chapter?
Get the full Sorting chapter — interactive notes, diagrams, worked solutions, polls and a free practice quiz — in the ConceptScroll app.
Study smarter with ConceptScroll
Daily NCERT-aligned reels, AI doubt solving and chapter quizzes — all free.
Start learning freeContinue reading
- Project Based Learning in Class 12 Computer Science: A Complete Guide
Discover how Project Based Learning (PBL) in Class 12 Computer Science helps students apply concepts through real-world projects, improving problem-solving and teamwork skills.
- Project Based Learning in Class 12 Computer Science: A Complete Guide
Discover how Project Based Learning (PBL) enhances practical skills in Class 12 Computer Science. Understand the process, benefits, and examples from NCERT.
- Project Based Learning in Class 12 Computer Science: A Practical Approach
Discover how Project Based Learning (PBL) in Class 12 Computer Science helps students apply theory through real-world projects, enhancing skills and teamwork.