Python Forum
bubble sort random list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bubble sort random list
#1

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?
Reply
#2
Are you allowed to use the 'random' module?

Please, please don't use single character variables Smile
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
Using single letter variable names makes your code hard to read, and hard to maintain.
Just converting to meaningful names will make your code look better.
Reply
#4
the single character variables are the ones asked from the tutor.
yes, the random module is allowed.
Reply
#5
Bad tutor!
But nevertheless you have to give him what he asks for!
Reply
#6
You should check out the random module, as I presume you want to shuffle the users input. Where would be the best place to randomize the inputs?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
(Nov-03-2017, 04:19 PM)sparkz_alot Wrote: You should check out the random module, as I presume you want to shuffle the users input. Where would be the best place to randomize the inputs?
i do not know to be honest. Any hints?
Reply
#8
Well, you'll want to do it before you 'sort' the list. So if you ignore that portion of code and focus on the remaining code, there is only one place where it would make sense to put it. That's about as 'hinty' as I can be, sorry :-\
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,455 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,358 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  how to sort a list without .sort() function letmecode 3 3,424 Dec-28-2020, 11:21 PM
Last Post: perfringo
  Random selection from list Mohsky 1 9,929 Mar-31-2020, 10:27 PM
Last Post: deanhystad
  list and sort query arian29 2 2,215 Sep-18-2019, 06:19 PM
Last Post: ndc85430
  [split] Manual Sort without Sort function fulir16 2 3,160 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 48,947 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU
  sort a list alphabeticaly without changing the original list Holmen 5 4,196 Jan-27-2019, 01:49 PM
Last Post: Holmen
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 2,278 Dec-19-2018, 06:22 PM
Last Post: ichabod801
  printing list of random generated rectangles Zatoichi 8 7,334 Feb-18-2018, 06:34 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020