The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
6
By Guest on 13th May 2019 07:12:51 PM | Syntax: TEXT | Views: 0



New paste | Download | Show/Hide line no. | Copy text to clipboard
  1. from random import randint
  2. from time import time
  3. lista=[]
  4.  
  5. def bubbleSort(lst,y=0):
  6.     for n in range(y,len(lst)):
  7.         for i in range(n):
  8.             if lst[i]>lst[i+1]:
  9.                 x=lst[i]
  10.                 lst[i]=lst[i+1]
  11.                 lst[i+1]=x
  12.         y+=1
  13.  
  14.  
  15. def selectionSort(lst):
  16.     for sortID in range(len(lst)-1,0,-1):
  17.         maxID=0
  18.         for x in range(1, sortID+1):
  19.             if lst[x]>lst[maxID]:
  20.                 y=lst[maxID]
  21.                 lst[maxID]=lst[x]
  22.                 lst[x]=y
  23.         y=lst[sortID]
  24.         lst[sortID]=lst[maxID]
  25.         lst[maxID]=y
  26.  
  27. def insertionSort(lst):
  28.     for i in range(1,len(lst)):
  29.         key=lst[i]
  30.         x=i
  31.         while x>0 and lst[x-1]>key:
  32.             lst[x]=lst[x-1]
  33.             x=x-1
  34.         lst[x]=key
  35.  
  36. def randomLista(n):
  37.     for i in range(1, n):
  38.         lista.append(randint(0,n-1))
  39.     return lista
  40.  
  41.  
  42. rannum=int(input("Unesite velicinu liste: "))
  43.  
  44. rezultat=[["Bubble",0,0,0,0], ["Select",0,0,0,0], ["Insert",0,0,0,0]]
  45.  
  46. for i in range(1,5):
  47.     a=time()
  48.     bubbleSort(randomLista(rannum))
  49.     b=time()
  50.     rezultat[0][i]=round(b-a,2)
  51.     a=time()
  52.     selectionSort(randomLista(rannum))
  53.     b=time()
  54.     rezultat[1][i]=round(b-a,2)
  55.     a=time()
  56.     insertionSort(randomLista(rannum))
  57.     b=time()
  58.     rezultat[2][i]=round(b-a,2)
  59.  
  60.  
  61. for x in rezultat:
  62.     print(x)



  • Recent Pastes