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).

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 5PE
icon
Related questions
Question

2.15 LAB: Descending selection sort with output during execution

 

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).

Important Coding Guidelines:

  • Use comments, and whitespaces around operators and assignments.
  • Use line breaks and indent your code.
  • Use naming conventions for variables, functions, methods, and more. This makes it easier to understand the code.
  • Write simple code and do not over complicate the logic. Code exhibits simplicity when it’s well organized, logically minimal, and easily readable.

Ex: If the input is:

20 10 30 40

the output is:

[40, 10, 30, 20] [40, 30, 10, 20] [40, 30, 20, 10]

Ex: If the input is:

7 8 3

the output is:

[8, 7, 3] [8, 7, 3]

Note: Use print(numbers) to output the list numbers and achieve the format shown in the example.

numbers = []
user_input = input()
tokens = user_input.split()
for token in tokens:
   numbers.append(int(tokens))
   #Convert the string input into an array of integers.
   for j in range(len(numbers)-1):
      
      # Find index of smallest remaining element
      index_largest = j
      for j in range(j+1, len(numbers)):
         
         if numbers[j] < numbers[index_largest]:
            index_largest = j
      
      # Swap numbers[i] and numbers[index_smallest]
      temp = numbers[j]
      numbers[j] = numbers[index_largest]
      numbers[index_largest] = temp
      
      print(numbers)

SCTU Class Homepage.
zy Section 2.15 - CS 233T: Fun X
= zyBooks
✰ learn.zybooks.com/zybook/CS233-2204A-01-2204A/chapter/2/section/15
My library > CS 233T: Fundamentals of Data Structures home >
2.15: LAB: Descending selection sort with output during execution
LAB
ACTIVITY
Note: Use print (numbers) to output the list numbers and achieve the format shown in the example.
422794.2543518.qx3zqy7
6
7
1 numbers = []
2 user_input = input()
3 tokens = user_input.split()
4 for token in tokens:
5
8
9
10
11
Section 2.1 - CS 233T: Fund X
12
13
14
15
- 16 17 18
2.15.1: LAB: Descending selection sort with output during execution
Thank you. Your reservation X
numbers.append(int (tokens))
#Convert the string input into an array of integers.
for j in range (len (numbers)-1):
# Find index of smallest remaining element
index_largest = j
for j in range(j+1, len(numbers)):
if numbers [j] < numbers [index_largest]:
index_largest = j
Develop mode
# Swap numbers[i] and numbers [index_smallest]
temp = numbers[j]
numbers [j] = numbers [index_largest]
Submit mode
Enter program input (optional)
20 10 30 40
main.py
Print - Your Confirmed Rese X
b Answered: CHALLENGE AC
zyBooks catalog
0/10
Load default template...
Run your program as often as you'd like, before submitting for grading. Below, type any needed
input values in the first box, then click Run program and observe the program's output in the
second box.
le
+
? Help/FAQ
Stacey Queen
X
:
3:46 PM
8/29/2022
Transcribed Image Text:SCTU Class Homepage. zy Section 2.15 - CS 233T: Fun X = zyBooks ✰ learn.zybooks.com/zybook/CS233-2204A-01-2204A/chapter/2/section/15 My library > CS 233T: Fundamentals of Data Structures home > 2.15: LAB: Descending selection sort with output during execution LAB ACTIVITY Note: Use print (numbers) to output the list numbers and achieve the format shown in the example. 422794.2543518.qx3zqy7 6 7 1 numbers = [] 2 user_input = input() 3 tokens = user_input.split() 4 for token in tokens: 5 8 9 10 11 Section 2.1 - CS 233T: Fund X 12 13 14 15 - 16 17 18 2.15.1: LAB: Descending selection sort with output during execution Thank you. Your reservation X numbers.append(int (tokens)) #Convert the string input into an array of integers. for j in range (len (numbers)-1): # Find index of smallest remaining element index_largest = j for j in range(j+1, len(numbers)): if numbers [j] < numbers [index_largest]: index_largest = j Develop mode # Swap numbers[i] and numbers [index_smallest] temp = numbers[j] numbers [j] = numbers [index_largest] Submit mode Enter program input (optional) 20 10 30 40 main.py Print - Your Confirmed Rese X b Answered: CHALLENGE AC zyBooks catalog 0/10 Load default template... Run your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the second box. le + ? Help/FAQ Stacey Queen X : 3:46 PM 8/29/2022
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

hi this helps me wiht the solution but how can I make it indented (image for reference) 

Thanks

 

1:Compare output
Output is nearly correct, but whitespace differs. See highlights below. Special character legend
Input
Your output
Expected output
20 10 30 40
[40,10,30,20] [40,30,10,20] [40,30,20,10]
[40, 10, 30, 2014
[40, 30, 10, 2014
[40, 30, 20, 10]
Transcribed Image Text:1:Compare output Output is nearly correct, but whitespace differs. See highlights below. Special character legend Input Your output Expected output 20 10 30 40 [40,10,30,20] [40,30,10,20] [40,30,20,10] [40, 10, 30, 2014 [40, 30, 10, 2014 [40, 30, 20, 10]
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Problems on Dynamic Programming
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning