Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import random
- lst = random.sample(range(1,100),10)
- print("Imput list:{}".format(lst))
- def split(lst, start, end):
- #Splits list into two parts.
- i = start - 1
- spliter = lst[end]
- for j in range(start, end):
- if lst[j] <= spliter:
- i = i + 1
- lst[i], lst[j] = lst[j], lst[i]
- lst[i+1], st[end] = lst[end], lst[i+1]
- return i + 1
- def quick_sort(lst,start, end):
- """Sort list of items using quick sort algoritthm.
- This is iterative implementation of quick sort algoritm
- """
- stack = []
- stack.append(start)
- stack.append(end)
- while stack:
- end = stack.pop()
- start = stack.pop()
- S = split(lst, start, end)
- if s - 1 > start:
- stack.append(start)
- stack.append(s - 1)
- if S + 1 <end:
- stack.append(s + 1)
- stack.append(end)
- quick_sort(lst, 0, len(lst - 1))
- print("Output list {}". format(lst))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement