Open main menu

Biolecture.org β

Sort & Search

<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 <= last and not found:
        midpoint = (first + last) // 2
        if num_list[midpoint] == target:
            found = true
        else:
            if target < 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]>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]<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>0 and num_list[position-1]>current_value:
            num_list[position]=num_list[posiont-1]
            position-=1
            num_list[position]=current_value
    return num_list
count=int(input("Enter the number of numbers:"))
num_list=[]
for i in range(0,ocunt):
    number=int(input("Enter the number:"))
    num_list.append(number)
print("The number list is",num_list)
a=str(input("Enter the types of sort(bubble, selection, insertion):"))
while True:
    if a=='bubble':
        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>0 and num_list[position-1]>current_value:
            num_list[position]=num_list[position-1]
            position-=1
            num_list[position]=current_value
    return num_list
count=int(input("Enter the number of numbers:"))
num_list=[]
for i in range(0,count):
    number=int(input("Enter the number:"))
    num_list.append(number)
print("The number list is", num_list)

a=str(input("Enter the types of sort(bubble, selection, insertion):"))
while True:
    if a=='bubble':
        bubble_sort(num_list)
        break
    elif a=='selection':
        selection_sort(num_list)
        break
    elif a=='insertion':
        insertion_sort(num_list)
        break
    else:
        a = input("Enter the types of sort(bubble, seletion, insertion):")

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

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

if result == True:
    print("We found!")
else:
    print("we cannot found!")