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

Sorting

The document describes implementations of several sorting algorithms: insertion sort, bubble sort, merge sort, quicksort, and selection sort. Insertion sort iterates through a list and inserts each element into its sorted position. Bubble sort iterates through pairs of adjacent elements and swaps them if out of order. Merge sort divides a list in half recursively until single elements remain, then merges the halves back together. Quicksort chooses a pivot element and partitions the list into sublists based on element values relative to the pivot. Selection sort iterates through the list and swaps the maximum value into the end, effectively sorting the list from left to right. Sample code and test cases are provided for each algorithm.

Uploaded by

ARYAN RATHORE
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Sorting

The document describes implementations of several sorting algorithms: insertion sort, bubble sort, merge sort, quicksort, and selection sort. Insertion sort iterates through a list and inserts each element into its sorted position. Bubble sort iterates through pairs of adjacent elements and swaps them if out of order. Merge sort divides a list in half recursively until single elements remain, then merges the halves back together. Quicksort chooses a pivot element and partitions the list into sublists based on element values relative to the pivot. Selection sort iterates through the list and swaps the maximum value into the end, effectively sorting the list from left to right. Sample code and test cases are provided for each algorithm.

Uploaded by

ARYAN RATHORE
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

def insertion_sort(InputList):

for i in range(1, len(InputList)):


j = i-1
nxt_element = InputList[i]
while (InputList[j] > nxt_element) and (j >= 0):
InputList[j+1] = InputList[j]
j=j-1
InputList[j+1] = nxt_element

list = [19,2,31,45,30,11,121,27]
insertion_sort(list)
print(list)

def bubblesort(list):

# Swap the elements to arrange in order


for iter_num in range(len(list)-1,0,-1):
for idx in range(iter_num):
if list[idx]>list[idx+1]:
temp = list[idx]
list[idx] = list[idx+1]
list[idx+1] = temp

list = [19,2,31,45,6,11,121,27]
bubblesort(list)
print(list)

# Python program for implementation of MergeSort


def mergeSort(arr):
if len(arr) >1:
mid = len(arr)//2 #Finding the mid of the array
L = arr[:mid] # Dividing the array elements
R = arr[mid:] # into 2 halves

mergeSort(L) # Sorting the first half


mergeSort(R) # Sorting the second half

i = j = k = 0

# Copy data to temp arrays L[] and R[]


while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i+=1
else:
arr[k] = R[j]
j+=1
k+=1

# Checking if any element was left


while i < len(L):
arr[k] = L[i]
i+=1
k+=1
while j < len(R):
arr[k] = R[j]
j+=1
k+=1

# Code to print the list


def printList(arr):
for i in range(len(arr)):
print(arr[i],end=" ")
print()

# driver code to test the above code


if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]
print ("Given array is", end="\n")
printList(arr)
mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)

def partition(arr,low,high):
i = ( low-1 ) # index of smaller element
pivot = arr[high] # pivot

for j in range(low , high):

# If current element is smaller than or


# equal to pivot
if arr[j] <= pivot:

# increment index of smaller element


i = i+1
arr[i],arr[j] = arr[j],arr[i]

arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )

# The main function that implements QuickSort


# arr[] --> Array to be sorted,
# low --> Starting index,
# high --> Ending index

# Function to do Quick sort


def quickSort(arr,low,high):
if low < high:

# pi is partitioning index, arr[p] is now


# at right place
pi = partition(arr,low,high)

# Separately sort elements before


# partition and after partition
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)

# Driver code to test above


arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
quickSort(arr,0,n-1)
print ("Sorted array is:")
for i in range(n):
print ("%d" %arr[i]),

def selection_sort(input_list):

for idx in range(len(input_list)):

min_idx = idx
for j in range( idx +1, len(input_list)):
if input_list[min_idx] > input_list[j]:
min_idx = j
# Swap the minimum value with the compared value

input_list[idx], input_list[min_idx] = input_list[min_idx], input_list[idx]

l = [19,2,31,45,30,11,121,27]
selection_sort(l)
print(l)

You might also like