Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Binary Sort - Find the mid value, guess, determine is our items is greater or less, reassign middle
- def binary_Search(list, item):
- low = 0
- high = len(list)-1
- while low<=high:
- mid = low+high:
- guess = list[mid]
- if guess == item:
- return mid
- if guess > item:
- high = mid-1
- else:
- low = mid + 1
- return None
- #Selection Sort (This example sorts an array from smallest to largest)
- # Find Smallest function -> Returns the current smallest value in the array.
- def findSmallest(arr):
- smallest = arr[0]
- smallest_index = 0
- for i in range(1,len(arr)):
- if arr[i]<smallest:
- smallest = arr[i]
- smallest_index = i
- return smallest_index
- def selectionSort(arr):
- newArr = []
- for i in range(len(arr)):
- smallest = findSmallest(arr) #finds the smallest item in the current array and returns the index
- newArr.append(arr.pop(smallest)) # pop that item from our current array and add it to our new one
- return newArr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement