Python Forum
Bubble sort on randomized integers
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bubble sort on randomized integers
#1
Hi all,

Apologies in advance - I've just started to learn python and am struggling to grasp it so my question will be at a very "noobish" level.

Basically, I want to generate a random set of integers and then perform a bubble sort on those integers.

I have already implemented two separate programs - one that generates random integers, and one that performs a bubble sort on a specified list (code below) but I need to join them
somehow and this is where I'm lost.

If someone could kindly help me (in layman's terms) it'd be greatly appreciated   Smile

Random no. generator:
import random

def random_ints(num, lower=0, upper=100):
    return [random.randrange(lower,upper+1) for i in range(num)]
 
print random_ints(10)
Bubble sort:
 def bubbleSort(listLetters):  
    for passnum in range(len(listLetters)-1,0,-1):
        for i in range(passnum):
            if listLetters[i]>listLetters[i+1]:
                temp = listLetters[i]
                listLetters[i] = listLetters[i+1]
                listLetters[i+1] = temp                
                                                       
listLetters = ['B', 'A', 'E', 'D', 'C', 'F',]
bubbleSort(listLetters)
print(listLetters)  
Reply
#2
Well your code seems to work fine so is your question how to import one into the other?
Reply
#3
Basically yes. The bubble sort code above is on another list 'listLetters' but I want to perform a bubble on a randomly generated set of numbers. But I'm lost on how to merge (?) or as you said, import one into the other.
Reply
#4
(May-11-2017, 07:11 PM)bellevie Wrote: Basically yes. The bubble sort code above is on another list 'listLetters' but I want to perform a bubble on a randomly generated set of numbers. But I'm lost on how to merge (?) or as you said, import one into the other.

So if they are in the same file it is trivial and looks like this:
But if they are separate files let's create two files for these functions (and nothing else) and place them in the same directory.

randy.py
import random


def random_ints(num, lower=0, upper=100):
    return [random.randrange(lower,upper+1) for i in range(num)]
and bubble.py
def bubbleSort(sequence):  
    for passnum in range(len(sequence)-1, 0, -1):
        for i in range(passnum):
            if sequence[i] > sequence[i+1]:
                sequence[i], sequence[i+1] = sequence[i+1], sequence[i] 
Now create one more file in that directory to test them called example.py
import randy
import bubble


some_numbers = randy.random_ints(10)
print(some_numbers)
bubble.bubbleSort(some_numbers)
print(some_numbers) 
And run that file.

You should see something similar to the following:
Output:
[96, 64, 92, 50, 94, 4, 100, 65, 72, 51] [4, 50, 51, 64, 65, 72, 92, 94, 96, 100]
Reply
#5
Mekire - thanks so much! your response was really helpful and I understand it now  Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,307 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Python Networkx: Visualize an edge weight with a bubble/circle uvw 0 1,988 Sep-01-2021, 06:26 AM
Last Post: uvw
  a complicated randomized function which I'm stuck on jojo77m 11 3,537 Aug-27-2020, 09:24 AM
Last Post: DPaul
  Bubble sort quiz: why the result is not the same? lupoalberto 11 5,879 Apr-27-2018, 12:18 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

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