Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- while True:
- '''#listin yaradilmasi
- list1 = []
- for i in range(16):
- a = random.randint(-1000,1000)
- list1.append(a)
- print("Listiniz :",list1)
- '''
- list1 = [6,8,3,5,9,3,4]
- #-----------------------------------------------------------------------------------------
- #quick sort
- def quicksort(list1, start, end):
- if start >= end:
- return
- left = start
- right = end
- pivot = list1[right] #pivot olaraq sonuncu element secilir
- while left <= right:
- while list1[left] < pivot:
- left += 1
- while list1[right] > pivot:
- right -= 1
- if left <= right:
- list1[left], list1[right] = list1[right], list1[left]
- left += 1
- right -= 1
- print("Current sorted list :" ,list1) #her deyisimi gosterir
- quicksort(list1, start, right)
- quicksort(list1, left, end)
- quicksort(list1,0,len(list1) - 1) #quick_sort ise salinir
- print("Current list : ",list1,"\n\n")
- #-------------------------------------------------------------------------------------------------
- #dublicate controlu evvelce yazdim, cunki daha sonrs gelen binary searchin icinde isledecem
- def dublicate_control(eded,list1):
- count = -1
- for i in range(len(list1) - 1):
- count += 1
- if eded == list1[i]:
- print("Bu eded listde {} indekslerindedir".format(count))
- #-------------------------------------------------------------------------------------------------
- #binary search
- def binary_search(eded,list1, first = None, last = None):
- for i in range(0,len(list1) - 1): #sort olunub olunmadigini yoxlayiriq
- if list1[i] > list1[i + 1]:
- return "List has not been sorted \nBinary search will not start"
- if first == None and last == None: #eger bu parametrler daxil edilmeyibse onlara ozu qiymet verir
- first = 0
- last = len(list1) - 1
- if last >= first:
- mid = (first + last) // 2
- if eded == list1[mid]:
- print("Binary search : seperated list: ",list1)
- return dublicate_control(eded,list1)
- elif eded < list1[mid]:
- print("Binary search : seperated list: ",list1)
- return binary_search(eded, list1, first, mid - 1)
- else:
- print("Binary search : seperated list: ",list1)
- return binary_search(eded,list1, mid + 1, last)
- else:
- return "\nItem doesn't exist in this list" #ededin olmamasini gosterir
- #programlar ise salinir
- #----------------------------------------------------------------------------------------------------
- eded = int(input("Enter the number that you want to search : "))
- binary_search(eded, list1)
- # davam edek ?
- davam = input("Davam etmek ucun 'C' basin , bitirmek ucun 'E'")
- if davam == "C":
- continue
- elif davam == "E":
- print("\n-------------------------Program bitdi-------------------------")
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement