Sorting
Sorting
IN A SPECIFIC ORDER.
INTRODUCTION TO SORTING
Sorting: Arranging elements in a specific order (e.g., numbers, words, etc.)Data can be sorted in:
Can be done:
• Numerically → Ascending or descending
• Alphabetically → A-Z or Z-A
• By length/value → E.g., height, weight, string length
Real-life examples:
• Dictionary → Alphabetical order
• Exam hall seating → Roll numbers
• Student records → Sorted by height/weight
Why sorting is useful:
• Makes searching faster and easier
• Saves time and effort compared to searching in unsorted data
Performance note:
• Sorting large datasets adds some time (overhead)
• But the benefit in faster searching outweighs this cost
In this chapter:
Learn and implement 3 sorting algorithms in Python:
• Bubble Sort (Section 5.2)
• Selection Sort (Section 5.3)
• Insertion Sort (Section 5.4)
BUBBLE SORT
Bubble Sort is a technique that repeatedly compares adjacent elements and swaps them if they
are in the wrong order.
Process:
• Each round through the list = 1 pass pass: going through all the elements in the list
• For a list of n elements → performs (n - 1) passes
• After each pass, the largest unsorted element "bubbles up" to its correct position
How it works (Ascending Order):
• Start at index 0
• Compare each pair: if left > right, swap
• After 1st pass → largest element at the end
• Repeat for remaining unsorted elements
Example:
Given: numList = [8, 7, 13, 1, -9, 4]
• Pass 1 → Largest value 13 moves to the end
• Pass 2 → Next largest moves to 2nd last position
• Continue until list is sorted
Visual aid:
• Blue → elements being compared
• Green → elements already sorted
PASS 1:
Index= 0 &1
Values=8 &7
Action= interchange bcz 8>7
Index= 1&2
Values=7&13
Action= no interchange bcz 7<13
Index= 2 &3
Values=13 &1
\Action= interchange bcz 13>1
Index= 3&4
Values=13 &-9
Action= interchange bcz 13>-9
Index= 4&5
Values=13&4
Action= interchange bcz 13>4
def bubble_Sort(list1):
Defines a function called bubble_Sort that takes one argument list1 (the list of numbers to sort).
🔹 n = len(list1)
Stores the number of elements in the list in the variable n.
🔹 for i in range(n):
Outer loop runs n times. Each loop is called a pass. After each pass, the largest unsorted element bubbles up to its
correct position.
🔹 for j in range(0, n-i-1):
Inner loop for comparing adjacent elements. We subtract i because the last i elements are already sorted. -1
prevents index out of range.
🔹 if list1[j] > list1[j + 1]:
If the current element is greater than the next element, they're out of order (for ascending order).
🔹 list1[j], list1[j + 1] = list1[j + 1], list1[j]
Swap the two elements to move the larger one right.
🔹 numList = [8, 7, 13, 1, -9, 4]
Defines a list of numbers that we want to sort.
🔹 bubble_Sort(numList)
Calls the sorting function to sort the list.
🔹 print("The sorted list is :")
Prints the message before displaying the sorted list.
🔹 for i in range(len(numList)):
Loops through the sorted list using its length.
🔹 print(numList[i], end=" ")