Advertisement
firudddin

Untitled

May 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. import random
  2. while True:
  3.     '''#listin yaradilmasi
  4.    list1 = []
  5.    for i in range(16):
  6.        a = random.randint(-1000,1000)
  7.        list1.append(a)
  8.    print("Listiniz :",list1)
  9. '''
  10.     list1 = [6,8,3,5,9,3,4]
  11.  
  12.  
  13. #-----------------------------------------------------------------------------------------
  14. #quick sort
  15.     def quicksort(list1, start, end):
  16.         if start >= end:
  17.             return
  18.         left = start
  19.         right = end
  20.         pivot = list1[right]         #pivot olaraq sonuncu element secilir
  21.         while left <= right:
  22.             while list1[left] < pivot:
  23.                 left += 1
  24.             while list1[right] > pivot:
  25.                 right -= 1
  26.             if left <= right:
  27.                 list1[left], list1[right] = list1[right], list1[left]
  28.                 left += 1
  29.                 right -= 1
  30.                 print("Current sorted list :" ,list1)                     #her deyisimi gosterir
  31.         quicksort(list1, start, right)
  32.         quicksort(list1, left, end)
  33.  
  34.  
  35.     quicksort(list1,0,len(list1) - 1)                                     #quick_sort ise salinir
  36.     print("Current list : ",list1,"\n\n")
  37.  
  38.  
  39. #-------------------------------------------------------------------------------------------------
  40. #dublicate controlu evvelce yazdim, cunki daha sonrs gelen binary searchin icinde isledecem
  41.  
  42.     def dublicate_control(eded,list1):
  43.         count = -1
  44.         for i in range(len(list1) - 1):
  45.             count += 1
  46.             if eded == list1[i]:
  47.                 print("Bu eded listde {} indekslerindedir".format(count))
  48.  
  49. #-------------------------------------------------------------------------------------------------
  50. #binary search
  51.  
  52.     def binary_search(eded,list1, first = None, last = None):
  53.        
  54.         for i in range(0,len(list1) - 1):                #sort olunub olunmadigini yoxlayiriq
  55.             if list1[i] > list1[i + 1]:
  56.                 return "List has not been sorted \nBinary search will not start"
  57.            
  58.  
  59.            
  60.         if first == None and last == None:  #eger bu parametrler daxil edilmeyibse onlara ozu qiymet verir
  61.             first = 0
  62.             last = len(list1) - 1
  63.         if last >= first:
  64.             mid = (first + last) // 2
  65.             if eded == list1[mid]:
  66.                 print("Binary search : seperated list: ",list1)
  67.                 return dublicate_control(eded,list1)
  68.             elif eded < list1[mid]:
  69.                 print("Binary search : seperated list: ",list1)
  70.                 return binary_search(eded, list1, first, mid - 1)
  71.             else:
  72.                 print("Binary search : seperated list: ",list1)
  73.                 return binary_search(eded,list1, mid + 1, last)
  74.         else:
  75.             return "\nItem doesn't exist in this list"                #ededin olmamasini gosterir
  76.  
  77. #programlar ise salinir
  78.    
  79. #----------------------------------------------------------------------------------------------------
  80.     eded = int(input("Enter the number that you want to search : "))
  81.     binary_search(eded, list1)
  82.  
  83. # davam edek ?
  84.     davam = input("Davam etmek ucun 'C' basin , bitirmek ucun 'E'")                                
  85.     if davam == "C":
  86.         continue
  87.     elif davam == "E":
  88.         print("\n-------------------------Program bitdi-------------------------")
  89.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement