0

I'm trying to spiff-up my skills and thought I would try to write my own little sorting algorithm:

import random
from random import randint

int_list = []
for i in range(10):     #Creates a 10-entry list of randon ints
    int_list.append(random.randint(0,10))
print "Unsorted list:\t" + str(int_list)

def sorter(int_list):
    for i in range(len(int_list)-1):
        while int_list[i] > int_list[i+1]:
            temp = int_list[i]
            int_list[i] = int_list[i+1]
            int_list[i+1] = temp
            continue
        return int_list

print "\"Sorted\" list:\t" + str(sorter(int_list))

When I run this script it only sorts the first two entries of the list. My understanding of continue was that it would keep looping through my while loop while the while statement was True.

5
  • 2
    Actually, since it is at the end of the while-loop, the continue is doing nothing. You can (should) remove it no problem. Commented Jan 31, 2014 at 21:23
  • Should it be at the beginning of the loop, or not in it at all? Commented Jan 31, 2014 at 21:23
  • continue just stops the execution of the current iteration and starts the next. Commented Jan 31, 2014 at 21:24
  • 1
    As a side not you don't need a temp variable to swap two values, simply do: int_list[i], int_list[i+1] = int_list[i+1], int_list[i] :) Commented Jan 31, 2014 at 21:25
  • @Matt - In addition to the answers, I think you could use a reference on continue. Commented Jan 31, 2014 at 21:30

2 Answers 2

1

Your while actually operates like an if, looks like you're trying to bubble-sort and you're not implementing it correctly (you should keep iterating until the iteration doesn't preform swap even once) - which is why you don't really sort.

Second, the pythonic way to "swap" is not:

temp = int_list[i]
int_list[i] = int_list[i+1]
int_list[i+1] = temp

but rather:

int_list[i], int_list[i+1] = int_list[i+1], int_list[i]
Sign up to request clarification or add additional context in comments.

2 Comments

In regards to the 'pythonese', if you will, at the end of your answer: Does that work like int_list[i] = int_list[i+1] and int_list[i+1] = int_list[i] effectively equating the first entry on the left side with the first entry on the right? Aka: Could I utilize the same structure with an n-length equality statement (rather than the 2-length shown above)?
@Matt why won't you try ? ;)
0

Your return int_list statement is indented too far - it is within the for loop, so your function quits at the end of the first iteration.

Also,

int_list = []
for i in range(10):     #Creates a 10-entry list of randon ints
    int_list.append(random.randint(0,10))

could be reduced to

int_list = [random.randint(0, 10) for i in range(10)]

and the while is only ever going to run 0 or 1 times (you can just use if).

Also, it looks like you are just doing the first pass of a bubble sort - it will move the highest value to the end of the list, but will not result in a completely sorted list. You need another for loop to do this repeatedly ('bubbling up' values).

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.