def linearSearch(list, target): found = False position = 0 while position < len(list) and not found: if list[position] == target: found = True break position = position +1 return list, found def binarySearch(list, target, min, max): middle = (min+max)/2 if target < list[middle]: return binarySearch(list, target, min, middle) if target > list[middle]: return binarySearch(list, target, middle, max) return list,list[middle] #Repeat # X ← StartofArray # Flag ← False # Repeat # If Number(X) > Number (X+ 1) Then # Temp ← Number(X) # Number (X) ← Number (X+ 1) # Number(X+I) ← Temp # Flag ← True # End If # X ← X+1 # Until EndofArray #Until Flag = False def bubbleSort(list): finish = False position = 0 count = 0 while not finish: finish = True position = 0 while position < (len(list)-1): count = count +1 if list[position] > list[position+1]: finish = False tmp = list[position] list[position] = list[position+1] list[position+1] = tmp position = position+1 return list, "count is {}".format(count) if __name__ == "__main__": list = ['yo', 'hey', 'booyah'] result = linearSearch(list, 'booyah') print(result) orderedList = [1, 3, 5, 7, 9] result = binarySearch(orderedList, 3, 0, len(orderedList)) print(result) unOrderedList = [5, 3, 2, 7, 6] result = bubbleSort(unOrderedList) print(result)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question