Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views5 pages

Sorting

Chapter 5 discusses sorting algorithms, including Bubble Sort, Selection Sort, and Insertion Sort, highlighting their processes and implementations. Each algorithm is explained with examples and Python code, emphasizing their O(n²) time complexity due to nested loops. The chapter also covers the importance of sorting for efficient data retrieval and searching.

Uploaded by

hazel967377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Sorting

Chapter 5 discusses sorting algorithms, including Bubble Sort, Selection Sort, and Insertion Sort, highlighting their processes and implementations. Each algorithm is explained with examples and Python code, emphasizing their O(n²) time complexity due to nested loops. The chapter also covers the importance of sorting for efficient data retrieval and searching.

Uploaded by

hazel967377
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 5: SORTING

Introduction

• Sorting is the process of arranging elements in a particular order (ascending, descending,


alphabetical, etc.).
• It is essential for easy data retrieval and efficient searching.
• Common applications:

– Dictionaries (alphabetical order)


– Exam seating plans (roll number order)
– Sorting by height, weight, etc.

Bubble Sort
Concept
• Compares adjacent elements and swaps them if they are in the wrong order.
• After each pass, the largest unsorted element “bubbles up” to its correct position.
• Requires n – 1 passes for a list of size n.

Optimization Tip If no swaps occur in a pass, the list is already sorted — the algorithm can be
terminated early.

Diagram: Bubble Sort Passes We will sort: [8, 7, 13, 1, -9, 4]


Each pass compares adjacent elements and swaps if needed.
Pass 1

[8, 7, 13, 1, -9, 4]


→ Swap 8 and 7 → [7, 8, 13, 1, -9, 4]
→ No Swap (8,13)
→ Swap 13 and 1 → [7, 8, 1, 13, -9, 4]
→ Swap 13 and -9 → [7, 8, 1, -9, 13, 4]
→ Swap 13 and 4 → [7, 8, 1, -9, 4, 13]

Pass 2

[7, 8, 1, -9, 4, 13]


→ No Swap (7,8)
→ Swap 8 and 1 → [7, 1, 8, -9, 4, 13]
→ Swap 8 and -9 → [7, 1, -9, 8, 4, 13]
→ Swap 8 and 4 → [7, 1, -9, 4, 8, 13]

Pass 3

[7, 1, -9, 4, 8, 13]


→ Swap 7 and 1 → [1, 7, -9, 4, 8, 13]
→ Swap 7 and -9 → [1, -9, 7, 4, 8, 13]
→ Swap 7 and 4 → [1, -9, 4, 7, 8, 13]
Pass 4

[1, -9, 4, 7, 8, 13]


→ Swap 1 and -9 → [-9, 1, 4, 7, 8, 13]

Pass 5

No swaps → Sorting complete.


Final list: [-9, 1, 4, 7, 8, 13]

Algorithm 5.1: Bubble Sort


BUBBLESORT(numList, n)
1. Set i = 0
2. While i < n repeat:
3. Set j = 0
4. While j < n - i - 1 repeat:
5. If numList[j] > numList[j+1], then
6. Swap numList[j] and numList[j+1]
7. j = j + 1
8. i = i + 1.

[Python Program

def bubble_Sort(list1):
n = len(list1)
for i in range(n):

for j in range(0, n-i-1):


if list1[j] > list1[j+1]:
list1[j], list1[j+1] = list1[j+1], list1[j]

numList = [8, 7, 13, 1, -9, 4]


bubble_Sort(numList)
print("The sorted list is:")
for i in range(len(numList)):
print(numList[i], end=" ")
Selection Sort

Concept
• The list is divided into sorted and unsorted parts.
• The smallest element from the unsorted list is selected and swapped with the first unsorted element.
• Requires n – 1 passes for n elements.

Diagram: Selection Sort Passes Starting List: [8, 7, 13, 1, -9, 4]

Pass 1 - Smallest is -9 → Swap with 8


[-9, 7, 13, 1, 8, 4]

Pass 2 - Smallest in [7,13,1,8,4] is 1 → Swap with 7


[-9, 1, 13, 7, 8, 4]

Pass 3 - Smallest in [13,7,8,4] is 4 → Swap with 13


[-9, 1, 4, 7, 8, 13]

Pass 4 - Smallest in [7,8,13] is 7 (already in place)


[-9, 1, 4, 7, 8, 13]
Pass 5 - Already sorted
[-9, 1, 4, 7, 8, 13]

Algorithm 5.2: Selection Sort


SELECTIONSORT(numList, n)
1. Set i = 0
2. While i < n repeat:
3. Set min = i, flag = 0
4. Set j = i + 1
5. While j < n repeat:
6. If numList[j] < numList[min]:
7. min = j
8. flag = 1
9. If flag == 1:
10. Swap numList[i], numList[min]
11. i = i + 1
Python Program
def selection_Sort(list2):
n = len(list2)
for i in range(n):
min = i
for j in range(i + 1, n):
if list2[j] < list2[min]:
min = j
list2[i], list2[min] = list2[min], list2[i]

numList = [8, 7, 13, 1, -9, 4]


selection_Sort(numList)
print("The sorted list is:")
for i in range(len(numList)):
print(numList[i], end=" ")

5.2 Insertion Sort

Concept
• Elements from the unsorted part are picked and inserted into the correct position of the sorted part.
• Like arranging cards in hand.

Diagram: Insertion Sort Passes Initial List: [8, 7, 13, 1, -9, 4]


Pass 1 Insert 7 into sorted [8]
[7, 8, 13, 1, -9, 4]
Pass 2 Insert 13 into sorted [7,8]
[7, 8, 13, 1, -9, 4]
Pass 3 Insert 1 into sorted [7,8,13]
[1, 7, 8, 13, -9, 4]
Pass 4 Insert -9 into sorted [1,7,8,13]
[-9, 1, 7, 8, 13, 4]

Pass 5 Insert 4 into sorted [-9,1,7,8,13]


[-9, 1, 4, 7, 8, 13]
Algorithm 5.3: Insertion Sort
INSERTIONSORT(numList, n)
1. Set i = 1
2. While i < n repeat:
3. temp = numList[i]
4. Set j = i - 1
5. While j >= 0 and numList[j] > temp:
6. numList[j+1] = numList[j]
7. j = j - 1
8. numList[j+1] = temp
9. i = i + 1

Python Program
def insertion_Sort(list3):
n = len(list3)
for i in range(n):
temp = list3[i]
j = i-1
while j >= 0 and temp < list3[j]:
list3[j+1] = list3[j]
j -= 1
list3[j+1] = temp

numList = [8, 7, 13, 1, -9, 4]


insertion_Sort(numList)
print("The sorted list is:")
for i in range(len(numList)):
print(numList[i], end=" ")

5.3 Time Complexity of Algorithms

Basic Complexity Concepts


• Constant Time (O(1)): No loops.
• Linear Time (O(n)): Single loop.
• Quadratic Time (O(n²)): Nested loops.

All three sorting algorithms in this chapter (bubble, selection, insertion) have O(n²) complexity due to
nested loops.

• Sorting: Rearranging elements for efficient access.


• Bubble Sort: Repeated adjacent swaps, n-1 passes.
• Selection Sort: Select and place smallest, reduce unsorted list.
• Insertion Sort: Insert each element into sorted part at the correct position.
• Time Complexity: Helps choose suitable algorithms for large datasets.

You might also like