0

I followed an algorithm with a while loop, but one of the parameters of the question was that I use nested for loops, and I'm not sure how to do that.

This is the while loop:

i = len(lst)
while i > 0:
    big = lst.index(max(lst[0:i]))
    lst[big], lst[i-1] = lst[i-1], lst[big]
    i = i - 1
    return lst

This is the question it's answering:

Input: [5,1,7,3]

First, find the largest number, which is 7.
Swap it and the number currently at the end of the list, which is 3. Now we have: [5,1,3,7]
Now, find the largest number, not including the 7, which is 5.
Swap it and the second to last number, which is 3. Now we have: [3,1,5,7].
Now, find the third largest number (excluding the first two), which is 3.
Swap it and the third to last number, which is 1.

Output: [1, 3, 5, 7]

4
  • 1
    What is the initial value of i? Commented Apr 16, 2019 at 5:04
  • please explain what your code does Commented Apr 16, 2019 at 5:07
  • It'll be better if you can paste the actual question as well here Commented Apr 16, 2019 at 5:19
  • its the selection sort in reverse order of iteration that is going from N-> 0 instead of going from 0 -> N , Id say just google it :-) Commented Apr 16, 2019 at 5:24

3 Answers 3

2

What you're seeing in the algorithm is a selection sort. And here's your second solution which you asked (nested for loops):

def insertion_sort(arr):
    l = len(arr)
    for i in range(l-1, -1, -1):
        m = -10000 # it should be lower than min(arr)
        idx = -1
        for key, val in enumerate(arr[:i+1]):
            if m < val:
                m = val
                idx = key
        if idx != -1:
            arr[i], arr[idx] = arr[idx], arr[i]
    return arr

And a quick test:

arr = list(range(10))[::-1]
print(arr)
# prints [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
result = insertion_sort(arr)
print(result)
# prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Sign up to request clarification or add additional context in comments.

Comments

0

This looks like a (rather slow) sorting algorithm - namely bubble sort. It's iterating from the end of the list lst. Then it's searching for the maximum value in the first n-1 elements, and swapping them with the end. It will, however, fail, if the maximum value is already at the end, because then it will automatically swap the max(n-1) with the n value. You'll need to add a check for this.

So from a first look, I'm not sure if i is defined before, but let's assume it's defined at the length of the list lst, as it seems to be. So let's start with the outer loop - as have a while loop that looks like it's counting down from i to 0. This is the opposite of an increasing for-loop, so we can create a reserved range:

rev_range = range(0,len(lst))
rev_range.reverse() 
for j in rev_range:
    # perform the sort

We now have the outer loop for the counting-down while loop. The sort itself iterates forward until it finds the maximum. This is a forward for loop.

# sorting
max_val_so_far_index=lst[j]
# lst[:j-1] gets the first j-1 elements of the list
for k in lst[:j-1]:
    if lst[k] > lst[max_val_so_far_index]:
        max_val_so_far_index = k
# now we have the index of the maximum value
# swap
temp = lst[j]
lst[j] = lst[max_val_so_far_index]
lst[max_val_so_far_index]=temp

Let's put the two components together to get:

rev_range = range(0,len(lst))
rev_range.reverse() 
for j in rev_range:
    # perform the sort
    # sorting
    #print j
    max_val_so_far_index=j

    # get the first j items
    for k in range(j):
        if lst[k] > lst[max_val_so_far_index]:
            max_val_so_far_index = k
    # now we have the index of the maximum value
    # swap
    temp = lst[j]
    lst[j] = lst[max_val_so_far_index]
    lst[max_val_so_far_index]=temp

At the end lst is sorted.

Comments

-1

The algorithm in the question is just another form of a bubble sort. The original algorithm uses two nested for loops. You can find a good explaination here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.