Here you can find all Implementation of sorting algorithms in python which sorts the array in Increasing Order (Ascending Order).
- Bubble sort:
- Insertion sort:
- Selection sort:
- Merge sort:
Read Also:All Sorting Algorithm In C++
Bubble Sort:
Implementation:
# python implementation of Bubble sort
# Bubble Sort function
def bubble_sort(arr, n):
# iterate i from 0 to n-1
for i in range(n):
# variable for checking if swap is done or not
swapped = 0
# iterate j form 0 to n-2
for j in range(n-1):
# compare two neighbors at j index
if arr[j] > arr[j + 1]:
# if first is greater than second then swap element
# swapping
arr[j], arr[j+1] = arr[j+1], arr[j]
# swapping is done
swapped = 1
# if swapping is not done then arr is sorted
# then return function to break for loop
if swapped == 0:
return
# Driver code:
arr = [1, 5, 8, 9, 2, 0, 323, 4, 0, 65, 6, 3, 1, 23, 5]
n = len(arr)
bubble_sort(arr, n)
# printing sorted array
print("sorted array")
for i in range(n):
print(arr[i], end=' ')
print()
Output:
sorted array:
0 0 1 1 2 3 4 5 5 6 8 9 23 65 323
Insertion Sort:
Implementation:
# python implementation of insertion sort
# insertion sort function
def insertion_sort(arr, n):
# iterate i form 1 to n-1
for i in range(1, n):
# here arr[0...i-1] is sorted in every iteration
# key which we will inserting
key = arr[i]
# point to last element of sorted array
j = i - 1
# shift elements greater than key
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
# please key element to appropriate please
arr[j + 1] = key
# Driver code:
arr = [1, 5, 8, 9, 2, 65, 6, 3, 1, 23, 5]
n = len(arr)
insertion_sort(arr, n)
# printing sorted array
print("sorted array")
for i in range(n):
print(arr[i], end=' ')
print()
Output:
sorted array:
1 1 2 3 5 5 6 8 9 23 65
Selection Sort:
Implementation:
# python implementation of selection sort
# selection sort function
def selection_sort(arr, n):
# iterate i from 0 to n-2
for i in range(n-1):
# consider arr[i...n] array
min_index = i
# find min element index
for j in range(i + 1, n):
if arr[min_index] > arr[j]:
min_index = j
# swapping min element with first element for array.
arr[i], arr[min_index] = arr[min_index], arr[i]
# Driver code:
arr = [1, 5, 8, 9, 2, 67895, 99999, 6, 3, 1, 23, 5]
n = len(arr)
selection_sort(arr, n)
# printing sorted array
print("sorted array")
for i in range(n):
print(arr[i], end=' ')
print()
Output:
sorted array:
1 1 2 3 5 5 6 8 9 23 67895 99999
Merge Sort:
Implementation:
# python program for merge sort
# Merge to subarray of array
# first_array = array[start..mid]
# second_array = array[mid+1..end]
def Merge(array, start, mid, end):
# creating to temp subarray
first_array = []
second_array = []
# copy first_array
for i in range(start, mid+1):
first_array.append(array[i])
# copy second_array
for i in range(mid + 1, end+1):
second_array.append(array[i])
# index reference
i = 0 # for first_array
j = 0 # for second_array
main_i = start # for main array
size_1 = len(first_array) # size of first_array
size_2 = len(second_array) # size of second_array
# merge subarray first_array and second_array into array[start..end]
while i < size_1 and j < size_2:
if first_array[i] <= second_array[j]:
array[main_i] = first_array[i]
i += 1
else:
array[main_i] = second_array[j]
j += 1
main_i += 1
# copy remaining element of first_array
while i < size_1:
array[main_i] = first_array[i]
i += 1
main_i += 1
# copy remaining element of second_array
while j < size_2:
array[main_i] = second_array[j]
j += 1
main_i += 1
# main merge Sort function
def mergeSort(array, start, end):
if start >= end:
return
# finding mid point
mid = start + (end - start) // 2
# divide
mergeSort(array, start, mid)
mergeSort(array, mid + 1, end)
# conquer
Merge(array, start, mid, end)
# driver code
# declaring array
array = [1, 12, 5, 4, 78, 9, 5, 6, 3, 2, 1, 4]
n = len(array)
mergeSort(array, 0, n - 1)
# array is sorted
print("sorted array:")
for i in range(n):
print(array[i], end=' ')
print()
Output:
sorted array:
1 1 2 3 4 4 5 5 6 9 12 78
Read Also:Merge Sort Algorithm
0 Comments
If you have any doubt let me know.