0% found this document useful (0 votes)
21 views

C-Sanjay - Sorting-Algorithm - Selection Sort and Insertion Sort

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)
21 views

C-Sanjay - Sorting-Algorithm - Selection Sort and Insertion Sort

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/ 3

29/12/2023, 23:34 c-sanjay/Sorting-Algorithm: Selection sort and Insertion sort

Selection sort and Insertion sort

Aim:
To write a program to perform selection sort and insertion sort using python programming.

Equipment’s required:
1. Hardware – PCs
2. Anaconda – Python 3.7 Installation / Moodle-Code Runner

Algorithm:

Selection Sort Algorithm:


1. Set the first unsorted element as the minimum
2. For each of the unsorted elements, check if the element < current minimum.
3. If yes, set the element as the new minimum.
4. Swap minimum with first unsorted position.
5. Repeat the steps 2 and 3 for all the elements in the array.

Insertion Sort Algorithm:


1. Set the first element as sorted element j.
2. For each unsorted element X, check if current sorted element j >X.
3. If yes, move sorted element to the right by 1.
4. Break the loop and insert X.
5. Repeat the steps 2 to 4 for sorting all the elements in the array.

Program:
i) #Selection Sort

'''
Program to sort the elements in the list using the Selection Sort algorithm.
Developed by: SANJAY C
RegisterNumber: 212223240150
'''
def selection_sort(nums):
for i in range(len(nums)):
low=i
for j in range(i+1,len(nums)):
if nums[j] < nums[low]:
low=j
nums[i],nums[low]=nums[low],nums[i]
list_of_nums = eval(input())
selection_sort(list_of_nums)
print(list_of_nums)

https://github.com/c-sanjay/Sorting-Algorithm 1/3
29/12/2023, 23:34 c-sanjay/Sorting-Algorithm: Selection sort and Insertion sort

ii) #Insertion Sort

'''
Program to sort the elements in the list using the Insertion Sort algorithm.
Developed by: SANJAY C
RegisterNumber: 212223240150
'''
def insertion_sort(nums):
for i in range(1,len(nums)):
item_to_insert=nums[i]
j = i-1
while j>=0 and nums[j] > item_to_insert:
nums[j+1]=nums[j]
j-=1
nums[j+1] = item_to_insert
list_of_nums = eval(input())
insertion_sort(list_of_nums)
print(list_of_nums)

Output:

https://github.com/c-sanjay/Sorting-Algorithm 2/3
29/12/2023, 23:34 c-sanjay/Sorting-Algorithm: Selection sort and Insertion sort

Result:
Thus the program is written to perform selection sort and insertion sort using python programming.

https://github.com/c-sanjay/Sorting-Algorithm 3/3

You might also like