Home > database >  How to print results of selection sort algorithm after first swap
How to print results of selection sort algorithm after first swap

Time:05-22

So, I am trying to do the Kattis.com problem Mjehuric:

Goran has five wooden pieces arranged in a sequence. There is a number between 1 and 5 inscribed on every piece, so that every number appears on exactly one of the five pieces. Goran wants to order the pieces to form the sequence 1,2,3,4,5 and does it like this:

  1. If the number on the first piece is greater than the number on the second piece, swap them.
  2. If the number on the second piece is greater than the number on the third piece, swap them.
  3. If the number on the third piece is greater than the number on the fourth piece, swap them.
  4. If the number on the fourth piece is greater than the number on the fifth piece, swap them.
  5. If the pieces don’t form the sequence 1,2,3,4,5, go to step 1.

Write a program that, given the initial ordering of the pieces, outputs the ordering after each swap.

Output

After any two pieces are swapped, output the ordering of the pieces, on a single line separated by spaces.

Sample Input 1:

2 1 5 3 4

Sample Output 1:

1 2 5 3 4
1 2 3 5 4
1 2 3 4 5

Sample Input 2:

2 3 4 5 1

Sample Output 2:

2 3 4 1 5
2 3 1 4 5
2 1 3 4 5
1 2 3 4 5

For the first sample input, my code yields the output of:

1 2 5 3 4
1 2 5 3 4
1 2 3 5 4
1 2 3 4 5

However, my code does not print after each swap, it prints after each iteration through that first for loop. My code is attached below:

My question is, how would I adapt my code to make the algorithm work to print only after a swap has been made?

My Code:

s = input("Enter your input: ")
s2 = s.replace(' ', '')
list = []
for i in s2:
  list.append(i)

for i in range(len(list) - 1):
   min_index = i
   for j in range(i   1, len(list)):
      if list[j] < list[min_index]:
         min_index = j
   list[i], list[min_index] = list[min_index], list[i]
   string = ''

   for i in list:
     string  = ' '   i
   print(string)

CodePudding user response:

What you're trying to do is implement a Bubble sort.

Here's a Bubble sort implementation that (optionally) prints the list being sorted after each swap.

def bubble(list_, p=False):
    e = len(list_)
    while e > 1:
        es = 0
        for i in range(1, e):
            if list_[i-1] > list_[i]:
                list_[i-1], list_[i] = list_[i], list_[i-1]
                if p:
                    print(list_)
                es = i
        e = es
    return list_

bubble([2,1,5,3,4], p=True)

Output:

[1, 2, 5, 3, 4]
[1, 2, 3, 5, 4]
[1, 2, 3, 4, 5]

CodePudding user response:

To make your code only print the swaps, add a condition to the second part (the printing part) of your code:

if i != min_index:

So the loop becomes this:

for i in range(len(list) - 1):
   min_index = i
   for j in range(i   1, len(list)):
      if list[j] < list[min_index]:
         min_index = j
   if i != min_index:  # <-- added, and rest of code is part of this block
      list[i], list[min_index] = list[min_index], list[i]
      string = ''
    
      for i in list:
         string  = ' '   i
      print(string)

However, there are some issues that remain:

  • Your code will not do the same swaps as given in the step-by-step instructions. For the second sample the output will be different. The instructions require that the swaps should only be swapping neighboring values. You have in fact implemented selection sort, while you are asked to implement bubble sort.

  • It is not good to use list as a name, as that is already the name of a native Python function

  • The shorter way to convert the input to a list is to use the split() method.

  • The shorter way to output a list of strings as a space separated string, is to use the join() method.

So adapt as follows:

s = input("Enter your input: ")
lst = s.split(" ")

unordered = True
while unordered:
    unordered = False
    for i in range(len(lst) - 1):
        if lst[i] > lst[i   1]: # Compare neighbor
            unordered = True
            lst[i], lst[i   1] = lst[i   1], lst[i]
            print(" ".join(lst))
  • Related