Open main menu

Biolecture.org β

Changes

Sort & Search

3,440 bytes added, 02:36, 3 December 2018
Created page with "<pre> <span style="font-size:28px"><strong>practice </strong></span> def binary_search(num_list,target): first = 0 last = len(num_list) - 1 found = False while fi..."
<pre>
<span style="font-size:28px"><strong>practice </strong></span>
def binary_search(num_list,target):
first = 0
last = len(num_list) - 1
found = False
while first &lt;= last and not found:
midpoint = (first + last) // 2
if num_list[midpoint] == target:
found = true
else:
if target &lt; num_list[midpoint]:
last = midpoint -1
else:
first = midpoint + 1
return found
def bubble_sort(num_list):
for pass_num in range(len(num_list)-1,0,-1):
for i in range(pass_num):
if num_list[i]&gt;num_list[i+1]:
temp=num_list[i]
num_list[i]=num_list[i+1]
num_list[i+1]=temp
return num_list
def selection_sort(num_list):
for fill_slot in range(0, len(num_list)-1):
pos_of_min=fill_slot
for location in range(fill_slot, len(num_list)):
if num_list[location]&lt;num_list[pos_of_min]:
pos_of_min = location
temp=num_list[fill_slot]
num_list[fill_slot]=num_list[pos_of_min]
num_list[pos_of_min]=temp
return num_list
def insertion_sort(num_list):
for index in range(1,len(num_list)):
current_value=num_list[index]
position=index
while position&gt;0 and num_list[position-1]&gt;current_value:
num_list[position]=num_list[posiont-1]
position-=1
num_list[position]=current_value
return num_list
count=int(input(&quot;Enter the number of numbers:&quot;))
num_list=[]
for i in range(0,ocunt):
number=int(input(&quot;Enter the number:&quot;))
num_list.append(number)
print(&quot;The number list is&quot;,num_list)
a=str(input(&quot;Enter the types of sort(bubble, selection, insertion):&quot;))
while True:
if a==&#39;bubble&#39;:
num_list[fill_slot]=num_list[pos_of_min]
num_list[pos_of_min]=temp
return num_list
def insertion_sort(num_list):
for index in range(1, len(num_list)):
current_value=num_list[index]
position=index
while position&gt;0 and num_list[position-1]&gt;current_value:
num_list[position]=num_list[position-1]
position-=1
num_list[position]=current_value
return num_list
count=int(input(&quot;Enter the number of numbers:&quot;))
num_list=[]
for i in range(0,count):
number=int(input(&quot;Enter the number:&quot;))
num_list.append(number)
print(&quot;The number list is&quot;, num_list)

a=str(input(&quot;Enter the types of sort(bubble, selection, insertion):&quot;))
while True:
if a==&#39;bubble&#39;:
bubble_sort(num_list)
break
elif a==&#39;selection&#39;:
selection_sort(num_list)
break
elif a==&#39;insertion&#39;:
insertion_sort(num_list)
break
else:
a = input(&quot;Enter the types of sort(bubble, seletion, insertion):&quot;)

# If &quot;bubble&quot;, call bubble_sort(num_list)
# If &quot;selection&quot;, call selection_sort(num_list)
# If &quot;insertion&quot;, call insertion_sort(num_list)
# If anything else, we should request user to input again!

print(&quot;The sorted number list is&quot;, num_list)
target= int(input(&quot;Enter the target number to find: &quot;))
result = binary_search(num_list, target)

if result == True:
print(&quot;We found!&quot;)
else:
print(&quot;we cannot found!&quot;)</pre>
Anonymous user