Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # Binary Sort - Find the mid value, guess, determine is our items is greater or less, reassign middle
  2. def binary_Search(list, item):
  3. low = 0
  4. high = len(list)-1
  5. while low<=high:
  6. mid = low+high:
  7. guess = list[mid]
  8. if guess == item:
  9. return mid
  10. if guess > item:
  11. high = mid-1
  12. else:
  13. low = mid + 1
  14. return None
  15.  
  16. #Selection Sort (This example sorts an array from smallest to largest)
  17.  
  18. # Find Smallest function -> Returns the current smallest value in the array.
  19. def findSmallest(arr):
  20. smallest = arr[0]
  21. smallest_index = 0
  22. for i in range(1,len(arr)):
  23. if arr[i]<smallest:
  24. smallest = arr[i]
  25. smallest_index = i
  26. return smallest_index
  27.  
  28. def selectionSort(arr):
  29. newArr = []
  30. for i in range(len(arr)):
  31. smallest = findSmallest(arr) #finds the smallest item in the current array and returns the index
  32. newArr.append(arr.pop(smallest)) # pop that item from our current array and add it to our new one
  33. return newArr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement