1

As part of a python assignment I'm trying to implement list sorting (ascending) without using the sort() method, I think my logic is correct but I keep getting an error:

sample=[23,44,12,1,6,87]
temp=0
for i in range(0,len(sample)):
    if sample[i] > sample[i+1]:
        sample[i]=temp
        sample[i]=sample[i+1]
        sample[i+1]=temp

This keeps giving me a list: index out of range error which I know is being caused by the fact that when the i == 3 the code still does i+1.

Need help with this..

I changed the code to:

for i in range(0,len(sample)-1):
    if sample[i] > sample[i+1]:
        temp=sample[i]
        sample[i]=sample[i+1]
        sample[i+1]=temp

that eliminates the error but doesn't sort the list

jwesonga
  • 4,249
  • 16
  • 56
  • 83
  • 1
    A quick google search will yield many possible sort algorithms. I suggest you start there. At the very least, there should be at least one in your textbook. – Austin Marshall Oct 10 '11 at 21:03
  • 2
    Change `for i in range(0,len(sample)):` to `for i in range(0,len(sample)-1):`. Now you just need to fix your algorithm. – Steven Rumbalski Oct 10 '11 at 21:04
  • 1
    What Steven said + your swapping logic with temp is wrong too, temp is never on the left hand side of the = in your code – Aditya Mukherji Oct 10 '11 at 21:09
  • 1
    I'd suggest getting a serious textbook and understand what you are doing, before just "getting the code to work". – Savino Sguera Oct 10 '11 at 21:11
  • This is your homework. Presumably your professor has covered sorting algorithms. If not, see http://faculty.cs.niu.edu/~hutchins/csci241/sorting.htm and try to translate one of those into Python. If you have errors with the code you write we will always be willing to give pointers. – Steven Rumbalski Oct 10 '11 at 21:27
  • Steven is not a college assignment, i'm trying to learn python and part of the process is writing a list sort without using the sort() method. I did try to translate without any success that's why I posted the question. – jwesonga Oct 10 '11 at 21:42

4 Answers4

1

check this: http://faculty.cs.niu.edu/~hutchins/csci241/sorting.htm

python bubble sorting:

sample=[23,44,12,1,6,87]

sorted = False
while not sorted:
    sorted = True
    for i in range(len(sample) - 1):
        if sample[i] > sample[i+1]:
            sorted = False
            sample[i], sample[i+1] = sample[i+1], sample[i]

print sample

altered from here: Bubble Sort Homework

Community
  • 1
  • 1
onatm
  • 816
  • 1
  • 11
  • 29
1

I hoped you understood why you got the index out of range error.

That was one of the problems but now let's examine your code because you have some "conceptual" error, in the sense that you are not fully understanding what the code you made does.

This is what your code actually does, and this is how you should think whenever you get errors like this:

sample=[23,44,12,1,6,87]
temp=0


for i in range(0,len(sample)-1):
    if sample[i] > sample[i+1]:
        temp=sample[i]
        sample[i]=sample[i+1]
        sample[i+1]=temp

First line: Your i will get the values 0,1,2 to 5 (in this case).

Second line: It will check whether to members of the list that are next to each other.

It will check if the first (from left to right) is bigger that the second, it will check 23 > 44, that will be false, so it won't execute the rest of the code.

No i will be 1, so you will be checking 44 > 12, this is true, so your code will execute, and successfully swap those two.

This is a good moment to learn a slick way to swap variables in python, without using temporary variables.

sample[i], sample[i+1] = sample[i+1], sample[i]

Read more about it, google python variable swap, if you want.

So back to the original problem.

You have successfully swapped 44 and 12, this list stands [23,12,44,1,6,87].

But if you continue to do so it will:

Swap 44 with 1

Swap 44 with 6

Not swap 44 with 87.

But the list will now be [23,12,1,6,44,87] and your code will stop executing itself because i is now 5.

Did you catch the problem?

You would need to re-run it several times to actually order it, since it doesn't check whether the list has be organized, rather it swaps members that are next to each other.

So if you would (for this specific list) run:

for j in range(3):
    for i in range(0,len(sample)-1):
        if sample[i] > sample[i+1]:
            sample[i], sample[i+1] =sample[i+1], sample[i]

The list would be ordered.

Sorting ordered collections like lists is a much studied "field".

I would recommend you reading up on sorting algorithms starting by very simple ones, like bubble-sort since that is "where you are going" with your algorithm (like onatm suggested).

There are "fun" ways to learn this algorithm, for example, check this out.

Read up on this too if you are interested.

This site illustrates quite well how the algorithms, work their way to a sorted list.

Good luck, and please comment if you have any doubt.

Community
  • 1
  • 1
Trufa
  • 39,971
  • 43
  • 126
  • 190
0

len(t) is referencing something outside of the code you posted. You want

for i in range(0,len(sample)-1):

Furthermore, the above statement will increment i for you, no need for the i += 1 statement. Python also will handle the initialization of i for you, so the i = 0 statement is similarly unnecessary. After these are fixed, you'll see that your sort is still incorrect, but you might be on a better path to discover why.

brc
  • 5,281
  • 2
  • 29
  • 30
0

I'm not too familiar with Python, but I believe it's when you do:

for i in range(0,len(sample)):
    if sample[i] > sample[i+1]:

I would try changing it to:

for i in range(0,len(sample) - 1):
    if sample[i] > sample[i+1]:

Hope this helps.

Dan W
  • 5,718
  • 4
  • 33
  • 44
  • This is not the only problem, that will solve the immediate error but won't solve the algorithms since it won't order it, check out my answer for a more detailed information :) – Trufa Oct 10 '11 at 23:58