0

I am trying to write a section of a larger program which will generate a list of random integers. the first randomly generated list should have X elements, and then generate another list of random integers with X + Y elements, and so on, sequentially adding Y to the number of elements until I get to a specified point. Each generated list will also be sorted using the selection sort method. I am using several different sort methods (selection, bubble, merge, quick, radix...) to calculate the execution time of each method for increasing input sizes. As far as the selection sort portion, I have this so far but the output I'm getting is 100 lists of 100 numbers. Clearly I'm still pretty new to Python.

Hoping for a breakthrough, thanks!

import time
import random

start_timeSelection = time.clock()
lst = []
count = 100
def selectionSort(lst):
    count = 100
    lst = [int(999*random.random()) for i in range(count)]
    for i in range(len(lst) - 1):
        currentMin = lst[i]
        currentMinIndex = i

        for j in range(i + 1, len(lst)):
            if currentMin > lst[j]:
                currentMin, currentMinIndex = lst[j], j

        if currentMinIndex != i:
            lst[currentMinIndex], lst[i] = lst[i], currentMin
        print(lst)
    while count < 300:
        count += 100
selectionSort(lst)

s = (time.clock() - start_timeSelection)
print("Selection Sort execution time is: ", s, "seconds")
melpomene
  • 84,125
  • 8
  • 85
  • 148
Chief
  • 11
  • 4
  • I don't understand your program but `while count < 300: count += 100` is dead code: The `count` variable isn't used afterwards so there's no point in modifying it. – melpomene Dec 10 '16 at 23:49

2 Answers2

0

Here's a short example that uses a generator and list comprehension to generate lists of random values with increasing length.

import random


def generate_list(start_len, incr_len):
    i = 0
    while True:
        yield [random.random() for j in range(start_len + i * incr_len)]
        i += 1

gl = generate_list(start_len=2, incr_len=3)
print(next(gl))  # [0.3401864808412862, 0.33105346208017106]
print(next(gl))  # [0.5075146706165449, 0.5802519757892776, 0.5244104797659368, 0.8235816542342208, 0.3669745504311662]
antonagestam
  • 4,532
  • 3
  • 32
  • 44
0

here is what you are looking for. This code creates a list containing a random number of elements ranging from 0-9 (but you can change the range). Also it creates 10 lists(this number can also be changed). Each new list is a random length + the previous list's length:

from random import randint
X_plus_Y = 0
x = 0

while x < 10: #change 10 to make desired number of lists
    lst = []
    num_of_elements = randint(0,9) #change 9 for different random range
    X_plus_Y += num_of_elements
    print("add "+str(num_of_elements)+" equals " + str(X_plus_Y))

    for i in range(X_plus_Y):
        lst.append(randint(0,9))

    print(lst)
    print("\n")
    x += 1

hope this helped

jjislam
  • 557
  • 3
  • 8
  • do you want the previous random integers to stay in the list or do you want brand new integers for each new list? – jjislam Dec 11 '16 at 00:22