0

I know I could use the sort() function, but I am trying to do this without using that.

from random import randint
# Create variables
numberArray = [0]*20

# Populate array
for i in range(0,20):
    numberArray[i] = randint(0,300)

# Sort array into ascending order
print("Sorting array into ascending order...")

sortedAscending = False
while sortedAscending == False:
    for i in range(0,20):
        sortedAscending = True
        if i != (len(numberArray)-1):
            if numberArray[i] > numberArray[i+1]:
                temp = numberArray[i]
                numberArray[i] = numberArray[i+1]
                numberArray[i+1] = temp
                sortedAscending = False
                for j in range(0,20):
                    print(numberArray[j])
                print("END OF ARRAY")
                print()

for i in range(0,20):
    print(numberArray[i])

It starts sorting the array, but then fails to sort any numbers after a few loops. Please help. Note: the loop with "END OF ARRAY" is for debugging.

4
  • 1
    (1) Show how it fails, don't just say that it fails. (2) StackOverflow's focus is on practical questions (see stackoverflow.com/help/on-topic); exercises are... not ideal. Commented Sep 5, 2017 at 16:04
  • 1
    Welcome to Stack Overflow! It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation. Commented Sep 5, 2017 at 16:05
  • (This is... a bubble sort, I think? You might edit the title to say you're trying to implement a bubble sort, so people who want to help with practical problems can see that and go elsewhere, rather than clicking through and being annoyed). Commented Sep 5, 2017 at 16:06
  • Note: this is not an array, it is a list. Commented Sep 5, 2017 at 16:10

2 Answers 2

1

try this and let me know if it works :) (I've tested it 2-3 times so far it has been working well)

from random import randint
# Create variables
numberArray = [0]*20

# Populate array
for i in range(0,20):
    numberArray[i] = randint(0,300)
# Sort array into ascending order
print("Sorting array into ascending order...")

sortedAscending = False
while sortedAscending == False:
    sortedAscending = True # this should be before the for loop
    for i in range(0,20):
        if i != (len(numberArray)-1):
            if numberArray[i] > numberArray[i+1]:
                temp = numberArray[i]
                numberArray[i] = numberArray[i+1]
                numberArray[i+1] = temp
                sortedAscending = False
                for j in range(0,20):
                    print(numberArray[j])
                print("END OF ARRAY")
                print()

for i in range(0,20):
    print(numberArray[i])

I moved the sortedAscending = True out of the for loop

Sign up to request clarification or add additional context in comments.

Comments

0

You have implemented bubble sort.

In python swapping the values are pretty simple. Suppose you want to swap the values of numberArray[i] and numberArray[i+1] then simply you can use this.

numberArray[i], numberArray[i+1] = numberArray[i+1], numberArray[i]

Here is your code. This code should work fine for you.

from random import randint
# Create variables
numberArray = [0]*20

# Populate array
for i in range(0,20):
    numberArray[i] = randint(0,300)
# Sort array into ascending order
print("Sorting array into ascending order...")

sortedAscending = False
while sortedAscending == False:
    sortedAscending = True # this should be before the for loop
    for i in range(0,20):
        if i != (len(numberArray)-1):
            if numberArray[i] > numberArray[i+1]:
                 numberArray[i], numberArray[i+1] = numberArray[i+1], numberArray[i]
                 sortedAscending = False
for i in range(0,20):
    print(numberArray[i])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.