0% found this document useful (0 votes)
3 views2 pages

Sorting

The document contains Python implementations of various sorting algorithms, including bubble sort, insertion sort, selection sort, merge sort, and quick sort. Each algorithm is demonstrated with an example array, showing the sorted output. The code snippets illustrate the logic and steps involved in each sorting method.

Uploaded by

jadey63425
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)
3 views2 pages

Sorting

The document contains Python implementations of various sorting algorithms, including bubble sort, insertion sort, selection sort, merge sort, and quick sort. Each algorithm is demonstrated with an example array, showing the sorted output. The code snippets illustrate the logic and steps involved in each sorting method.

Uploaded by

jadey63425
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

7/13/25, 6:28 PM Untitled24

In [1]: def bubble(arr):


n = len(arr)
for i in range(n):
for j in range(n-i-1):
if arr[j]>arr[j+1]:
arr[j],arr[j+1] = arr[j+1],arr[j]
return arr
a = [2,6,9,3,1,5]
print(bubble(a))

[1, 2, 3, 5, 6, 9]

In [2]: def insertion(arr):


n = len(arr)
for i in range(1,n):
key = arr[i]
j = i-1

while j>=0 and arr[j]>key:


arr[j+1] = arr[j]
j-=1
arr[j+1] = key

return arr
a = [2,6,9,3,1,5]
print(insertion(a))

[1, 2, 3, 5, 6, 9]

In [5]: def selection(arr):


n = len(arr)
for i in range(n):
min_index = i
for j in range(i+1,n):
if arr[j]<arr[min_index]:
min_index = j
arr[i],arr[min_index] = arr[min_index],arr[i]
return arr
a = [2,6,9,3,1,5]
print(selection(a))

[1, 2, 3, 5, 6, 9]

In [12]: def merge(arr, low, mid, high):


a = arr[low:mid+1]
b = arr[mid+1:high+1]

i = j = 0
k = low

while i < len(a) and j < len(b):


if a[i] <= b[j]:
arr[k] = a[i]
i += 1
else:
arr[k] = b[j]
j += 1
k += 1

while i < len(a):


arr[k] = a[i]
i += 1
k += 1

localhost:8888/nbconvert/html/[Link]?download=false 1/2
7/13/25, 6:28 PM Untitled24

while j < len(b):


arr[k] = b[j]
j += 1
k += 1

def mergeSort(arr, low, high):


if low >= high:
return
mid = (low + high) // 2
mergeSort(arr, low, mid)
mergeSort(arr, mid + 1, high)
merge(arr, low, mid, high)

# Test
a = [2, 6, 9, 3, 1, 5]
mergeSort(a, 0, len(a) - 1)
print(a)

[1, 2, 3, 5, 6, 9]

In [14]: def partion(arr,low,high):


pivot = arr[high]
start = low

for i in range(low,high):
if arr[i]<pivot:
arr[i] , arr[start] = arr[start], arr[i]
start+=1
arr[start] , arr[high] = arr[high] , arr[start]
return start

def quickSort(arr,low,high):
if low>=high:
return
p = partion(arr,low,high)
quickSort(arr,low,p-1)
quickSort(arr,p+1,high)

a = [2, 6, 9, 3, 1, 5]
quickSort(a, 0, len(a) - 1)
print(a)

[1, 2, 3, 5, 6, 9]

In [ ]:

localhost:8888/nbconvert/html/[Link]?download=false 2/2

You might also like