0% found this document useful (0 votes)
75 views7 pages

Sorting Algorithms: Bubble, Selection, Insertion

Chapter 5 discusses various sorting algorithms including Bubble Sort, Selection Sort, and Insertion Sort, explaining their mechanisms and providing Python implementations. Each algorithm sorts a list of elements in ascending order, with Bubble Sort repeatedly swapping adjacent elements, Selection Sort finding the smallest element, and Insertion Sort inserting elements into a sorted list. The chapter also covers the time complexity of these algorithms, indicating that they all have a time complexity of O(n^2).

Uploaded by

anush.r673
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)
75 views7 pages

Sorting Algorithms: Bubble, Selection, Insertion

Chapter 5 discusses various sorting algorithms including Bubble Sort, Selection Sort, and Insertion Sort, explaining their mechanisms and providing Python implementations. Each algorithm sorts a list of elements in ascending order, with Bubble Sort repeatedly swapping adjacent elements, Selection Sort finding the smallest element, and Insertion Sort inserting elements into a sorted list. The chapter also covers the time complexity of these algorithms, indicating that they all have a time complexity of O(n^2).

Uploaded by

anush.r673
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

Chapter 5

SORTING

5.1 Introduction
Sorting is the process of ordering or arranging a given collection of
elements in some particular order. We can sort a collection of numbers in
ascending (increasing) or descending (decreasing) order.

5.2 Bubble Sort


It sorts a given list of elements by repeatedly comparing the adjacent
elements and swapping them if they are unordered.
In algorithm, every iteration through each element of a list is called a
pass. For a list with n elements, the bubble sort makes a total of n – 1
passes to sort the list. In each pass, the required pairs of adjacent
elements of the list will be compared.
In order to arrange elements in ascending order, the largest element
is identified after each pass and placed at the correct position in the list.
This can be considered as the largest element being ‘bubbled up’. Hence
the name Bubble sort.

Ex: Sort (8, 7, 13, 1, -9, 4) in ascending form using bubble sort.

1
Algorithm:
BUBBLESORT( numList, n)
Step 1: SET i = 0
Step 2: WHILE i< n-1 REPEAT STEPS 3 to 8
Step 3: SET j = 0
Step 4: WHILE j< n-1-i, REPEAT STEPS 5 to 7
Step 5: IF numList[j] > numList[j+1] THEN
(numList[j], numList[j+1]) = (numList[j+1], numList[j])
Step 6: SET j=j+1
Step 7: SET i=i+1

Program: Implementation of bubble sort using Python.


def bubble_Sort(list1):
n = len(list1)
for i in range(0, n-1): # Number of passes
for j in range(0, n-1-i):
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)
2
print (“The sorted list is :”)
for i in range(0, len(numList)):
print (numList[i], end=" ")
Output:
The sorted list is : -9 1 4 7 8 13
5.3 Selection sort
I t is a sorting technique to sort a list having n elements, the selection
sort makes (n-1) number of passes through the list. The list is considered to
be divided into two lists -- the left list containing the sorted elements, and
the right list containing the unsorted elements. Initially, the left list is
empty, and the right list contains all the elements.
For arranging elements in ascending order, in the first pass, all the
elements in the unsorted list are traversed to find the smallest element. The
smallest element is then swapped with the leftmost element of the unsorted
list. This element occupies the first position in the sorted list, and it is not
considered in further passes. In the second pass, the next smallest element
is selected from the remaining elements in the unsorted list and swapped
with the leftmost element of the unsorted list. This element occupies the
second position in the sorted list, and the unsorted list reduces by one
element for the third pass.
This process continues until n-1 smallest elements are found and
moved to their respective places. The nth element is the last, and it is
already in place. Ex: 8, 7, 13, 1, -9, 4

3
Algorithm: Selection Sort SELECTIONSORT( numList, n)

Step 1: SET i=0


Step 2: WHILE i< n-1 REPEAT STEPS 3 to 11
Step 3: SET min = i, flag = 0
Step 4: SET j= i+1
Step 5: WHILE j<n-1, REPEAT STEPS 6 to 10
Step 6: IF numList[j] < numList[min] THEN
Step 7: min = j
Step 8: flag = 1
Step 9: IF flag = 1 THEN
Step 10: (numList[i], numList[min])= (numList[min], numList[i])
Step 11: SET i=i+1
Program to Implement selection sort using Python.
def selection_Sort(list1):
flag = 0
n=len(list1)
4
for i in range(0, n-1):
min = i
for j in range(i + 1, n):
if list1[j] < list1[min]:
min = j
flag = 1
if flag == 1 :
list1[min], list1[i] = list1[i], list1[min]

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=" ")

Output:
The sorted list is : -9 1 4 7 8 13

5.4 Insertion sort


In Insertion sort, the list is divided into two parts - one of sorted elements
and another of unsorted elements. Each element in the unsorted list is
considered one by one and is inserted into the sorted list at its appropriate
position. In each pass, the sorted list is traversed from the backward
direction to find the position where the unsorted element could be inserted.
Hence the sorting method is called insertion sort.
In pass 1, the unsorted list has n-1 elements and the sorted list has a
single element (say element s). The first element of the unsorted list (say
element e) is compared with the element s of sorted list. If element e is
smaller than element s, then element s is shifted to the right making space
for inserting element e. This shifting will now make sorted list of size 2
and unsorted list of size n-2.
This continues till all the elements in unsorted lists are inserted at
appropriate positions in the sorted list. This results into a sorted list in which
elements are arranged in ascending order.
Ex: 8,7,13,1,-9,4

5
Algorithm: INSERTIONSORT( numList, n)
Step 1: SET i=1
Step 2: WHILE i< n REPEAT STEPS 3 to 9
Step 3: temp = numList[i]
Step 4: SET j = i-1
Step 5: WHILE j> = 0 and numList[j]>temp, REPEAT STEPS 6 to 7
Step 6: numList[j+1] = numList[j]
Step 7: SET j=j-1
Step 8: numList[j+1] = temp #insert temp at position j
Step 9: set i=i+1

6
Program to Implement insertion sort using Python.

def insertion_Sort(list1):
n= len(list1)
for i in range(n): # Traverse through all elements
temp = list1[i]
j = i-1
while j >=0 and temp< list1[j] :
list1[j+1] = list1[j]
j = j-1
list1[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=" ")

Output:
The sorted list is :
-9 1 4 7 8 13
5.5 Time complexity of Algorithms
There can be more than one approach to solve a problem using a
computer. The amount of time an algorithm takes to process a given data can
be called its time complexity. Calculating the complexity of different
algorithms involves mathematical calculations and detailed analysis .

Estimating the time complexity of an algorithm includes:


➢ Any algorithm that does not have any loop will have time complexity
as 1 since the number of instructions to be executed will be constant,
irrespective of the data size. Such algorithms are known as
Constant time algorithms.
➢ Any algorithm that has a loop ( 1 to n) will have the time complexity as
n because the loop will execute the statement inside its body n number
of times. Such algorithms are known as Linear time algorithms.

➢ A loop within a loop (nested loop) will have the time complexity as n2.
Such algorithms are known as Quadratic time algorithms.

➢ If there is a nested loop and also a single loop, the time complexity will
be estimated on the basis of the nested loop only.

So according to the above rules, all the sorting algorithms namely, bubble
sort, selection sort and insertion sort have a time complexity of n2.

You might also like