In [2]:
import random
import time
In [84]:
start_time = time.time()
numbers = random.sample(range(0, 100), 10)
print(numbers)
def sort_a_list(your_list):
sorted_list = []
while numbers:
largest = max(numbers)
index = numbers.index(largest)
sorted_list.append(numbers.pop(index))
return sorted_list
print(sort_a_list(numbers))
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
In [86]:
start_time = time.time()
numbers = random.sample(range(0, 1000), 100)
print(numbers)
def sort_a_list(your_list):
sorted_list100 = []
while numbers:
largest = max(numbers)
index = numbers.index(largest)
sorted_list100.append(numbers.pop(index))
return sorted_list100
print(sort_a_list(numbers))
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
In [87]:
start_time = time.time()
numbers = random.sample(range(0, 10000), 1000)
def sort_a_list(your_list):
sorted_list1000 = []
while numbers:
largest = max(numbers)
index = numbers.index(largest)
sorted_list1000.append(numbers.pop(index))
return sorted_list1000
print(sort_a_list(numbers))
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
In [60]:
# for ten numbers
start_time = time.time()
print(sorted_list)
user_input = input("Enter the number you want to search for ")
number = int(user_input)
def find_num(sorted_list, number):
pos = 0
new_list = []
for item in sorted_list:
if item == number:
new_list.append(pos)
pos = pos+1
if new_list:
return new_list
else:
return print("Not Found")
print(find_num(sorted_list, number))
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
In [66]:
# for 100 numbers
start_time = time.time()
print(sorted_list100)
user_input = input("Enter the number you want to search for ")
number = int(user_input)
def find_num(sorted_list, number):
pos = 0
new_list = []
for item in sorted_list:
if item == number:
new_list.append(pos)
pos = pos+1
if new_list:
return new_list
else:
return print("Not Found")
print(find_num(sorted_list100, number))
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
In [69]:
# for 1000 numbers
start_time = time.time()
#print(sorted_list1000)
user_input = input("Enter the number you want to search for ")
number = int(user_input)
def find_num(sorted_list, number):
pos = 0
new_list = []
for item in sorted_list:
if item == number:
new_list.append(pos)
pos = pos+1
if new_list:
return new_list
else:
return print("Not Found")
print(find_num(sorted_list1000, number))
end_time = time.time()
print("Elapsed time was %g seconds" % (end_time - start_time))
Observations: