• API
• FAQ
• Tools
• Archive
daily pastebin goal
17%
SHARE
TWEET

# Untitled

a guest Jan 23rd, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import matplotlib.pyplot as plt
2. import time
3. import random
4. import numpy as np
5.
6. #create randomized arrays for the algorithm to sort
7. #size of the array is set to 10
8. #max is set to 50 as the maximum value of the randomy generated values in the array
9. def create_array(size=10,max=50):
10.     from random import randint
11.     return [randint(0,max) for _ in range(size)]
12.
13. start = time.time()
14. step = 0
15.
16. def mergeSort(nlist):
17.     if len(nlist)>1:
18.         mid = len(nlist)//2 #splitting the list in half
19.         lefthalf = nlist[:mid]
20.         righthalf = nlist[mid:]
21.         global step
22.         step += 1
23.
24.         mergeSort(lefthalf)
25.         mergeSort(righthalf)
26.         i=j=k=0
27.         while i < len(lefthalf) and j < len(righthalf):
28.             step += 1
29.             if lefthalf[i] < righthalf[j]:
30.                 nlist[k]=lefthalf[i]
31.                 i=i+1
32.             else:
33.                 nlist[k]=righthalf[j]
34.                 j=j+1
35.             k=k+1
36.
37.         while i < len(lefthalf):
38.             step += 1
39.             nlist[k]=lefthalf[i]
40.             i=i+1
41.             k=k+1
42.
43.         while j < len(righthalf):
44.             step += 1
45.             nlist[k]=righthalf[j]
46.             j=j+1
47.             k=k+1
48.
49. end = time.time()
50.
51.
52.
53. test= create_array()
54. print test
55. mergeSort(test)
56. print (test)
57. print(end - start)
58. print (step)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top