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

0% found this document useful (0 votes)
52 views9 pages

05 Sorting

This chapter covers sorting techniques in computer science, focusing on Bubble Sort, Selection Sort, and Insertion Sort. Each sorting method is explained with its algorithm and a Python implementation example, demonstrating how to sort a list of numbers. The time complexity for all three sorting algorithms is noted to be O(n^2).

Uploaded by

gssprithvinms1
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)
52 views9 pages

05 Sorting

This chapter covers sorting techniques in computer science, focusing on Bubble Sort, Selection Sort, and Insertion Sort. Each sorting method is explained with its algorithm and a Python implementation example, demonstrating how to sort a list of numbers. The time complexity for all three sorting algorithms is noted to be O(n^2).

Uploaded by

gssprithvinms1
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/ 9

Computer Science

Chapter-05

Sorting
 Introduction:
 Sorting is the process of arranging a given collection of elements in
some particular order (increasing/decreasing/alphabetical order).
 Sorting is helpful in so many places like searching a word in
dictionary is easy, because the words are arranged in alphabetical
order, searching for an employee is easy, because the employees are
stored in ascending order by using their employee id’s, etc..
 Different techniques like Bubble Sort, Selection Sort, Insertion Sort,
Merge Sort, Quick Sort, Heap Sort, etc., are used to arrange the
elements.
 In this chapter we learn only Bubble Sort, Selection Sort, Insertion
Sort techniques.

1. Bubble Sort:
 Bubble sort is a technique to sort the 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 PASS.
 For a list with ‘n’ elements makes ‘n-1’ passes to sort the list.
 In each pass two adjacent elements will be compared and
swapped if they are unordered to find out the largest element and
to place it in the correct position of the list.
 This is considered as the largest element id ‘bubbled up’ hence
this technique is called as Bubble Sort.
 For next pass this largest element will not be consider to find out
the second largest and this process is repeated until the list is
sorted.

 Implementation of Bubble sort on numList:


 For the list numList = [8, 7, 13, 1, -9, 4],
bubble sort would typically require 5 passes to sort the list.

Ashvini.Y.K, Lecturer in CS dept., BGS PU College, Mysuru Page 1


Computer Science

 Algorithm to perform BUBBLE SORT


Bubble Sort BUBBLESORT( numList, n)

Step 1: SET i = 0

Step 2: WHILE i< n REPEAT STEPS 3 to 8

Step 3: SET j = 0

Step 4: WHILE j< n-i-1,REPEAT STEPS 5 to 7

Step 5: IF numList[j] > numList[j+1] THEN

Step 6: swap(numList[j],numList[j+1])

Step 7: SET j=j+1

Step 8: SET i=i+1

Ashvini.Y.K, Lecturer in CS dept., BGS PU College, Mysuru Page 2


Computer Science
1. Program 5-1 Implementation of bubble sort using Python.

def bubble_Sort(list1):
n = len(list1)
for i in range(n): # Number of passes
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 :", numList)


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

2. Selection Sort:
 Selection sort is a sorting method that finds smallest element of
list in each pass and moves it to the correct position of the sorted
list until the whole list is sorted.

 In this technique the whole list is divided into two lists where 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.

 In selection sort also, for a list of 'n' elements, 'n-1' passes are
conducted. Each pass places the next smallest element in its
correct position. After 'n-1' passes, the list is sorted.

 For the list numList = [8, 7, 13, 1, -9, 4],


selection sort would typically require 5 passes to sort the list.

Ashvini.Y.K, Lecturer in CS dept., BGS PU College, Mysuru Page 3


Computer Science

 Algorithm to perform selection sort:

SELECTIONSORT( numList, n)
Step 1: SET i=0
Step 2: WHILE i< n REPEAT STEPS 3 to 12
Step 3: SET min = i, flag = 0
Step 4: SET j= i+1
Step 5: WHILE j<n, REPEAT STEP 6 TO 11
Step 6: IF numList[j] < numList[min] THEN
Step 7: min = j
Step 8: flag = 1
Ashvini.Y.K, Lecturer in CS dept., BGS PU College, Mysuru Page 4
Computer Science
Step 9: j=j+1
Step 10: IF flag = 1 THEN
Step 11: swap(numList[i],numList[min])
Step 12: SET i=i+1

 Implementation of selection sort using Python.


def selection_Sort(list2):
f lag = 0
n=len(list2)
for i in range(n):
min = i
for j in range(i + 1, len(list2)):
if list2[j] < list2[min]:
min = j
f lag = 1
if flag == 1 : # next smallest element is found
list2[min], list2[i] = list2[i], list2[min]

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


selection_Sort(numList)
print ("The sorted list is :", numList)
Output:
The sorted list is : -9 1 4 7 8 1

3. Insertion Sort:
 In this technique also the whole list is divided into two
lists where the left list containing the sorted elements,
and the right list containing the unsorted elements.

 Initially, the left(sorted) list consist single element and the


right(unsorted) list contains n-1 elements.

Ashvini.Y.K, Lecturer in CS dept., BGS PU College, Mysuru Page 5


Computer Science
 Each element in the unsorted list is considered one by
one and is inserted into the sorted list at its appropriate
position. Hence it is called as insertion sort.

 In Insertion sort also, for a list of 'n' elements, 'n-1'


passes are conducted.

 For the list numList = [8, 7, 13, 1, -9, 4],


Insertion sort would typically require 5 passes to sort the list.

 Algorithm - Insertion Sort Activity

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
Step 9: set i=i+1

Ashvini.Y.K, Lecturer in CS dept., BGS PU College, Mysuru Page 6


Computer Science
Implementation of insertion sort using Python.
def insertion_Sort(list3):
n= len(list3)
for i in range(1,n):
temp = list3[i]
j = i-1
while j >=0 and temp< list3[j] :
list3[j+1] = list3[j]
j = j-1
list3[j+1] = temp

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


insertion_Sort(numList)
print (“The sorted list is :” numList)

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

 Time complexity of algorithms:


 The amount of time taken by an algorithm to process a given data
is called its time complexity.
 Time complexity helps us to decide which algorithm is more
suitable for which kind of data and application.
 The following rules will guide us in estimating the time complexity
of an algorithm
1. 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.

Ashvini.Y.K, Lecturer in CS dept., BGS PU College, Mysuru Page 7


Computer Science
2. Any algorithm that has a loop (usually 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.
3. A loop within a loop (nested loop) will have the time
complexity as n2. Such algorithms are known as Quadratic
time algorithms.
4. 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.
 Based on the above Rules the time complexity bubble sort,
selection sort and insertion sort have a time complexity of n2.

Ashvini.Y.K, Lecturer in CS dept., BGS PU College, Mysuru Page 8


Computer Science

Ashvini.Y.K, Lecturer in CS dept., BGS PU College, Mysuru Page 9

You might also like