Nov-03-2017, 01:35 PM
Hi. i have a homework and it needs to use bubble sort for a randomly created list. In more details it asks the user to input how many numbers to randomly create. Then sort out these numbers using the bubble sort. Up to now i have the following:
L = [] num = int(input("enter number of elements: ")) for i in range(num): L.insert(i, int(input("enter element: "))) print("Unsorted array is: ", L) #sort for X in range(len(L) - 1, 0, -1): for j in range(X): if L[j] > L[j + 1]: temp = L[j] L[j] = L[j + 1] L[j + 1] = temp print("sorted array is : ", L)First thing that i am stuck with is the random creation of the numbers the user will ask for. eg 5 numbers [5,98,12,65,2].
L is the list and N are the places of the numbers that the user asked for.
Requirements are:
for i from 0 up to N-2
for j from 0 up to N-2-i
if L[j+1]<L[j]:
change L[j] with L[j+1]
Could someone help me out to tide up the code please?