DEV Community

222010301004
222010301004

Posted on

Types of sorting methods in python

What is sorting method in python?

The sort() method used to sort the elements of a given list in a specific ascending or descending order.

Types of Sorting Algorithms:

Insertion Sort
Bubble sort
Selection Sort

Insertion sort:-

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.

Example:-

Here is a code for insertion sort example
-Insertion sort
Input:-
def ins(l1):
for i in range(1,len(l1)):
temp=l1[i]
n=i-1
while n>=0 and temp<l1[n]:
l1[n+1]=l1[n]
n=n-1
l1[n+1]=temp
l1=[]
lim=int(input(" Enter the limit of the list should be : "))
for i in range(lim):
el=int(input(" Enter the value of element you wanted to be in the list : "))
l1.append(el)
lim=lim-1
ins(l1)
for i in range(len(l1)):
print(l1[i])
[1]
1m
lim=int(input(" Enter the limit of the list shoud be : "))
for i in range(lim):
el=int(input(" Enter the value of element you wanted to be in the list : "))
l1.append(el)
lim=lim-1
ins(l1)
for i in range(len(l1)):
print(l1[i])

Output:-
Enter the limit of the list should be : 4
Enter the value of element you wanted to be in the list : 3
Enter the value of element you wanted to be in the list : 5
Enter the value of element you wanted to be in the list : 1
Enter the value of element you wanted to be in the list : 3
1
3
3
5

Bubble sort:-

Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.
Example:-
Here is a code for bubble sort example
-Bubble sort
Input:-
def bbs(l1):
for i in range(len(l1)-1):
for m in range(len(l1)-1):
if(l1[m])>l1[m+1]:
l1[m],l1[m+1]=l1[m+1],l1[m]
return l1
l1=[]
lim=int(input(" Enter the limit of the list shoud be : "))
for i in range(lim):
el=int(input(" Enter the value of element you wanted to be in the list : "))
l1.append(el)
lim=lim-1
print(" The given unsorted list is : ",l1)
res=bbs(l1)
print(" The sorted list of the give list is ",l1)

Output:-
Enter the limit of the list shoud be : 5
Enter the value of element you wanted to be in the list : 2
Enter the value of element you wanted to be in the list : 7
Enter the value of element you wanted to be in the list : 8
Enter the value of element you wanted to be in the list : 10
Enter the value of element you wanted to be in the list : 3
The given unsorted list is : [2, 7, 8, 10, 3]
The sorted list of the give list is [2, 3, 7, 8, 10]

Selection Sort:-

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.

Example:-
Here is a code for selection sort example
-Selection sort
Input:-
def ss(l1):
for i in range(len(l1)-1):
min=1
for j in range(i+1,len(l1)):
if l1[j]<l1[min]:
min=j
(l1[i],l1[min])=(l1[min],l1[i])
print(l1)
l1=[]
lim=int(input(" Enter the limit of the list shoud be : "))
for i in range(lim):
el=int(input(" Enter the value of element you wanted to be in the list : "))
l1.append(el)
lim=lim-1
res=ss(l1)

Output:-
Enter the limit of the list shoud be : 4
Enter the value of element you wanted to be in the list : 2
Enter the value of element you wanted to be in the list : 1
Enter the value of element you wanted to be in the list : 6
Enter the value of element you wanted to be in the list : 8
[1, 2, 6, 8]
[1, 2, 6, 8]
[1, 6, 2, 8]

Top comments (0)