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

Algorithm PAssignment 2

The document discusses various sorting algorithms including heap sort, quicksort, merging k sorted lists using min heap, counting sort, and bucket sort. For each algorithm, Python code is provided to implement the algorithm and analyze time complexity which is O(nlogn) for heap sort, quicksort, and bucket sort, and O(n) for counting sort.

Uploaded by

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

Algorithm PAssignment 2

The document discusses various sorting algorithms including heap sort, quicksort, merging k sorted lists using min heap, counting sort, and bucket sort. For each algorithm, Python code is provided to implement the algorithm and analyze time complexity which is O(nlogn) for heap sort, quicksort, and bucket sort, and O(n) for counting sort.

Uploaded by

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

-------------------------------------------ASSIGNMENT:2------------------------------------------

1.HEAP_SORT
In [2]: import random

import time

import matplotlib.pyplot as plt

def heapify(arr,n,i):

largest=i

l=2*i+1

r=2*i+2

if l<n and arr[largest]<arr[l]:

largest=l

if r<n and arr[largest]<arr[r]:

largest=r

if largest !=i:

arr[i],arr[largest]=arr[largest],arr[i]

heapify(arr, n, largest)

def max_heap(arr):

ln=len(arr)

for i in range(ln//2-1,-1,-1):

heapify(arr,ln,i)

def heap_sort(arr):

max_heap(arr)

ln=len(arr)

for i in range(ln-1, -1, -1):

arr[i], arr[0] = arr[0], arr[i]

heapify(arr, i, 0)

arr=[random.randint(1,100) for i in range(20)]

print(arr)

heap_sort(arr)

print(arr)

[82, 52, 17, 14, 78, 46, 50, 67, 55, 31, 73, 36, 67, 19, 93, 16, 24, 9, 35, 87]

[9, 14, 16, 17, 19, 24, 31, 35, 36, 46, 50, 52, 55, 67, 67, 73, 78, 82, 87, 93]

HeapSort_Analysis
From the graph bellow ,we see that the graph looks like a nlogn graph,so the complexcity of heapsort is 0(nlogn) (n=size of the heap)

In [39]: elements=[]

times=[]

for i in range(50):

l1=[random.randint(0,1000) for j in range(random.randint(0,1000))]

ln=len(l1)

elements.append(ln)

start=time.time()

heap_sort(l1)

end=time.time()

times.append(end-start)

plt.xlabel('list_length')

plt.ylabel('time_complexcity')

plt.scatter(elements,times)

plt.grid()

plt.show()

2. quicksort
In [3]: import random

def partition(arr,low,high):

p=arr[high]

i=low-1

for j in range(low,high):

if arr[j]<=p:

i+=1

arr[i],arr[j]=arr[j],arr[i]

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

return i+1

def quick_sort(arr,low,high):

if low<high:

pi=partition(arr,low,high)

quick_sort(arr,low,pi-1)

quick_sort(arr,pi+1,high)

arr=[random.randint(0,50) for i in range(20)]

print(arr)

high=len(arr)

quick_sort(arr,0,high-1)

print(arr)

[29, 41, 33, 28, 11, 27, 35, 25, 25, 9, 2, 50, 27, 47, 47, 6, 22, 0, 35, 23]

[0, 2, 6, 9, 11, 22, 23, 25, 25, 27, 27, 28, 29, 33, 35, 35, 41, 47, 47, 50]

Quick_sort_analysis
From the graph bellow ,we see that the graph looks like a nlogn graph,so the complexcity of quicksort is 0(nlogn) (n=size of the list)

In [29]: import time

import matplotlib.pyplot as plt

elements=[]

times=[]

for i in range(100):

arr=[random.randint(100,1000) for j in range(random.randint(0,1000))]

ln=len(arr)

elements.append(ln)

start=time.time()

quick_sort(arr,0,ln-1)

end=time.time()

times.append(end-start)

plt.xlabel('list_length')

plt.ylabel('time_complexcity')

plt.plot(elements,times,'or')

plt.grid()

plt.show()

randomized version of quicksort and analysis


In [40]: import random

import time

import matplotlib.pyplot as plt

def randomized_partition(arr,l,h):

r=random.randrange(l,h)

arr[l]=arr[r]

po=partition(arr, l, h)

return po

def partition(arr,l,h):

p=arr[h]

i=l-1

for j in range(l,h):

if arr[j]<=p:

i+=1

arr[i],arr[j]=arr[j],arr[i]

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

return i+1

def quick_sort(arr,l,h):

if l<h:

p=randomized_partition(arr, l, h)

quick_sort(arr, l, p-1)

quick_sort(arr, p+1, h)

l=[random.randint(0, 10) for i in range(20)]

print(l)

quick_sort(l, 0, len(l)-1)

print(l)

print("-----------------------------------")

elements=[]

times=[]

for i in range(100):

l1=[random.randint(0,1000) for j in range(random.randint(0,1000))]

ln=len(l1)

elements.append(ln)

start=time.time()

quick_sort(l1,0,ln-1)

end=time.time()

times.append(end-start)

plt.xlabel('list_length')

plt.ylabel('time_complexcity')

plt.plot(elements,times,'ob')

plt.grid()

plt.show()

[6, 6, 7, 3, 8, 4, 6, 5, 7, 5, 3, 3, 0, 10, 10, 6, 8, 6, 2, 5]

[0, 2, 3, 3, 3, 3, 4, 5, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 10, 10]

-----------------------------------

3.Merge k-sorted lists using a min-heap


In [1]: def min_heapify(a,i,n):

left = i*2+1

right = i*2+2

small = i

if left<=n and a[left]<a[small]:

small = left

if right<=n and a[right]<a[small]:

small = right

if small != i:

a[small], a[i] = a[i], a[small]

min_heapify(a,small,n)

def build_min_heap(l):

n=len(l)

for i in range(n//2,-1,-1):

min_heapify(l,i,len(l)-1)

def merge(arr):

l = []

for i in arr:

for j in i:

l.append(j)

build_min_heap(l)

for i in range(len(l)-1,-1,-1):

min_heapify(l,0,i)

l[0], l[i] = l[i], l[0]

return l

n = int(input('Enter total number of list: '))

arr = []

for i in range(n):

arr.append(list(map(int,input('list %d: '%(i+1)).split())))

print('After sorting',merge(arr))

Enter total number of list: 3

list 1: 87 0 14 5 36 69 5

list 2: 12 87 58 99 0 1 5

list 3: 65 3 1 4 8 254

After sorting [254, 99, 87, 87, 69, 65, 58, 36, 14, 12, 8, 5, 5, 5, 4, 3, 1, 1, 0, 0]

4.Counting Sort
In [2]: import random

def counting_sort(a):

l=max(a)

ln=len(a)

c=[0]*(l+1)

b=[0]*ln

for j in range(0,ln):

c[a[j]]+=1

for i in range(1,l+1):

c[i]=c[i]+c[i-1]

for j in range(ln-1,-1,-1):

b[c[a[j]]-1]=a[j]

c[a[j]]-=1

return b

arr=[random.randint(1,100) for i in range(20)]

print(arr)

s=counting_sort(arr)

print(s)

[54, 26, 97, 59, 34, 45, 91, 31, 69, 34, 57, 44, 32, 70, 18, 46, 22, 91, 100, 17]

[17, 18, 22, 26, 31, 32, 34, 34, 44, 45, 46, 54, 57, 59, 69, 70, 91, 91, 97, 100]

Counting Sort_analysis
from the graph we see that time complexcity of the counting_sort is linear 0(n) (n=size of the list)

In [3]: import random

import matplotlib.pyplot as plt

import time

elements=[]

times=[]

for i in range(50):

l1=[random.randint(0,1000) for j in range(random.randint(1,100))]

ln=len(l1)

elements.append(ln)

start=time.time()

counting_sort(l1)

end=time.time()

times.append(end-start)

plt.xlabel('list_length')

plt.ylabel('time_complexcity')

plt.plot(elements,times,'ro')

plt.grid()

plt.show()

5.Bucket Sort
In [4]: import random

def insertion_sort(b):

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

x = b[i]

j = i-1

while j>=0 and x<b[j]:

b[j], b[j+1] = b[j+1], b[j]

j = j-1

b[j+1] = x

return b

def max(l):

max = 0

for i in l:

if max<i:

max = i

return max

def bucket(l):

mx = max(l)

ln = len(l)

buc = []

for i in range(ln+1):

buc.append([])

for i in l:

index = int(round((i/mx)*ln,2))

buc[index].append(i)

for i in buc:

i = insertion_sort(i)

return buc

l=[random.uniform(0,1) for i in range(10)]

print(l)

for i in bucket(l):

for j in i:

print(j,end=" ")

[0.23838754158748243, 0.5742574410219573, 0.733822976682041, 0.5214269126595142, 0.9681984563774992, 0.9895505408890733, 0.8825741504144846, 0.719479667254980


8, 0.0629241676584047, 0.062139153553289916]

0.062139153553289916 0.0629241676584047 0.23838754158748243 0.5214269126595142 0.5742574410219573 0.7194796672549808 0.733822976682041 0.8825741504144846 0.96


81984563774992 0.9895505408890733
From the graph we see that time complexcity of the bucket_sort is linear 0(n) (n=size of the list)

In [6]: import matplotlib.pyplot as plt

import time

times=[]

elements=[]

l=[]

for i in range(100):

arr=[random.uniform(0,1) for i in range(random.randint(5,50))]

ln=len(arr)

elements.append(ln)

start=time.time()

b=bucket(arr)

end=time.time()

times.append(end-start)

plt.plot(elements,times,'ro')

plt.xlabel('list_length')

plt.ylabel('time_complexity')

plt.grid()

plt.show()

You might also like