0

Write a program that takes an integer list as input and sorts the list into descending order using selection sort. The program should use nested loops and output the list after each iteration of the outer loop, thus outputting the list N-1 times (where N is the size of the list).

This is what I have, but the output is just printing the input twice exactly the way it is entered. What am I doing wrong here?

array = []
array.append(str(input(" ")))
    
for i in range(len(array)):
    max_index = i
    for j in range(i+1, len(array)):
        if array[j] > array[max_index]:
            max_index = j
    print(array)
    array[i],array[max_index] = array[max_index],array[i]
    
    print(array)

Input:

0 10 20 30 40

Output:

['0 10 20 30 40']
['0 10 20 30 40']
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • You are sorting a list containing a single string. – mkrieger1 Dec 21 '21 at 21:16
  • Does this answer your question? [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – mkrieger1 Dec 21 '21 at 21:16
  • Your list contains *one single element*, the string `'0 10 20 30 40'`. It's already as sorted as it's ever going to be. You would need to call `.split()` on it to actually get the list of 5 items that you seem to expect - and then call `int()` on each of those items if you want them to be compared numerically rather than alphabetically. – jasonharper Dec 21 '21 at 21:16
  • I'm not familiar with the .split command. How would I use it? Would it allow input to be read and interpreted into an array so that I can then sort the array into descending order? – Jared Jennings Dec 21 '21 at 22:44

1 Answers1

0

Based on your question

What am I doing wrong here?

I'm going to take this line as a point of reference:

array.append(str(input(" ")))
>>> 0 10 20 30 40
print(array)
>>> ['0 10 20 30 40']

As you can see the output is of length 1. It only has one item which is a String. You basically append the user input as a single string. Because you don't split the items in the input.

Solution

Input

 0 10 20 30 40 50

1. This solution returns a list of Strings.

See solution 2 below for a list of int.

I just split the string based on your input in the question.

Also, notice my list comparison, I changed the comparison to int.

array = []
array=input(" ").split(" ")

for i in range(len(array)):
    max_index = i
    for j in range(i+1, len(array)):
        if int(array[j]) > int(array[max_index]):
            max_index = j
    array[i],array[max_index] = array[max_index],array[i]

    print(array)

Output

['50', '10', '20', '30', '40', '0']
['50', '40', '20', '30', '10', '0']
['50', '40', '30', '20', '10', '0']
['50', '40', '30', '20', '10', '0']
['50', '40', '30', '20', '10', '0']
['50', '40', '30', '20', '10', '0']

2. This solution returns a list of integers.

Here, I mapped the split input into the array.

array = []
array=list(map(int, input(" ").split(" ")))

for i in range(len(array)):
    max_index = i
    for j in range(i+1, len(array)):
        if array[j] > array[max_index]:
            max_index = j
    array[i],array[max_index] = array[max_index],array[i]

    print(array)

Output

[50, 10, 20, 30, 40, 0]
[50, 40, 20, 30, 10, 0]
[50, 40, 30, 20, 10, 0]
[50, 40, 30, 20, 10, 0]
[50, 40, 30, 20, 10, 0]
[50, 40, 30, 20, 10, 0]
  • Is there a way to get the output to format as [50, 10, 20, 30, 40, 0] * Remove apostraphes from the output [50, 40, 20, 30, 10, 0] [50, 40, 30, 20, 10, 0] [50, 40, 30, 20, 10, 0] [50, 40, 30, 20, 10, 0] [50, 40, 30, 20, 10, 0] *Print new line at the end – Jared Jennings Dec 23 '21 at 00:09
  • Yes, let me edit the answer real quick @JaredJennings – Leon Kipkoech Dec 23 '21 at 07:15