Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random as rd
- import matplotlib.pyplot as plt
- def partation(a, low, high):
- global cnt
- # ind = rd.randint(low, high)
- # a[high], a[ind] = a[ind], a[high]
- pivot = a[high]
- i = low-1
- for j in range(low, high):
- cnt += 1
- if a[j] < pivot:
- i += 1
- a[i], a[j] = a[j], a[i]
- a[i+1], a[high] = a[high], a[i+1]
- return i+1
- def quickSort(a, low, high):
- if low <= high:
- proper = partation(a, low, high)
- quickSort(a, low, proper-1)
- quickSort(a, proper+1, high)
- cnt = 0
- k = 4
- arr1 = []
- for i in range(100):
- # arr1.append(rd.randint(1, 1000))
- arr1.append(1+i)
- swaps = []
- sample_range = 1000
- sample_map = []
- for i in range(1250):
- sample_map.append(0)
- for i in range(sample_range):
- arr = arr1[:]
- cnt = 0
- rd.shuffle(arr)
- quickSort(arr, 0, len(arr)-1)
- swaps.append(cnt)
- sample_map[cnt] += 1
- plt.plot(range(1250), sample_map)
- plt.xlabel("No Of Swaps")
- plt.ylabel("Frequency Of Swapes")
- plt.show()
Add Comment
Please, Sign In to add comment