I am trying to compare the running time between bubble sort and insert sort. It does this on 10 different lists with the first list containing 1000 numbers and then increases by 1000 each time.
The following loop creates the lists and times each list using both methods. It is SUPPOSED to assign each time to a list, and then to another list because the goal is to have a list of lists that I can later use as data points for a graph. Assume sort_1 and sort_2 are implemented sorting algorithms
counter = 0
empty_list = []
list_limit = 1000
data_point = []
list_of_data_points = []
while list_limit <= 10000:
while counter < list_limit:
num = random.randint(1, 10000)
empty_list.append(num)
counter += 1
copied_list = list(empty_list)
bubble_time = sort_1(copied_list)
insert_time = sort_2(copied_list)
data_point.clear()
data_point.append(bubble_time)
data_point.append(insert_time)
list_of_data_points.append(data_point)
print(list_of_data_points)
list_limit += 1000
Now the reason print(list_of_data_points) is in the loop is because this is where the error is happening. Everything else is working perfectly, but this is what I get when I run the loop:
[[0.10695279999999996, 0.00023099999999998122]]
[[0.43489919999999993, 0.0004752999999999563], [0.43489919999999993, 0.0004752999999999563]]
[[0.9825091000000001, 0.0007314000000000487], [0.9825091000000001, 0.0007314000000000487], [0.9825091000000001, 0.0007314000000000487]]
As you can see, it will calculate the times just fine. However, each time it is supposed to add the data_point into the list, it seems to be clearing the current list and making each data point the same value.
I am confused because I thought the append() method was supposed to just add the current data point list into the larger list of lists (i.e. list_of_data_points. Is there something wrong with my while loop or is this a list issue that I am misunderstanding?