1i - Second Smallest   Take an unknown number, larger than two, of positive integers as input. Assume that the first number is always smaller than the second, all numbers are unique and the input consists of at least three integers. Print the second smallest integer. Example: Please enter some numbers: 10 12 2 5 15 The second smallest number is: 5 Put your program in second_smallest.py. Use line.split(" ") to split the numbers in the input.   1j - Bubblesort   Bubble sort is a sorting algorithm: when given a list, for example [4,2,6,8], it will sort the list to become sorted, [2,4,6,8]. Bubble sort works by walking through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The process of walking through the list is repeated until we have traversed the entire list and no elements were swapped (this means that all adjacent elements are in the right order, and hence the list is sorted). Example run, let's see how this works on the numbers "5 1 4 2 8": First pass ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ) Here, the algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) → ( 1 4 5 2 8 ) Swap since 5 > 4 ( 1 4 5 2 8 ) → ( 1 4 2 5 8 ) Swap since 5 > 2 ( 1 4 2 5 8 ) → ( 1 4 2 5 8 ) Now, since these elements are already in order (8 > 5), the algorithm does not swap them. Second pass ( 1 4 2 5 8 ) → ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 > 2 ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) Third pass Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one additional whole pass without any swap to know it is sorted. Implement the above algorithm.  Example: Please enter some numbers: 123 45 78 6 5 48 38 98 [5, 6, 38, 45, 48, 78, 98, 123]

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 21PE
icon
Related questions
Question
  • Please do this in Python Programming and provide screenshot
  • The output should be exact same like in the examples.
  • If the output in the example is in 2 decimals, then it should be like that. 

 

1i - Second Smallest

 

Take an unknown number, larger than two, of positive integers as input. Assume that the first
number is always smaller than the second, all numbers are unique and the
input consists of at least three integers. Print the second smallest integer.

Example:

Please enter some numbers: 10 12 2 5 15
The second smallest number is: 5

Put your program in second_smallest.py. Use line.split(" ") to split the numbers in the input.

 

1j - Bubblesort

 

Bubble sort is a sorting algorithm: when given a list, for example [4,2,6,8], it will sort the list to become sorted, [2,4,6,8]. Bubble sort works by walking through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The process of walking through the list is repeated until we have traversed the entire list and no elements were swapped (this means that all adjacent elements are in the right order, and hence the list is sorted).

Example run, let's see how this works on the numbers "5 1 4 2 8":

First pass

5 1 4 2 8 ) → ( 1 5 4 2 8 ) Here, the algorithm compares the first two elements, and swaps since 5 > 1.

( 1 5 4 2 8 ) → ( 1 4 5 2 8 ) Swap since 5 > 4

( 1 4 5 2 8 ) → ( 1 4 2 5 8 ) Swap since 5 > 2

( 1 4 2 5 8 ) → ( 1 4 2 5 8 ) Now, since these elements are already in order (8 > 5), the algorithm does not swap them.

Second pass

1 4 2 5 8 ) → ( 1 4 2 5 8 )

( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 > 2

( 1 2 4 5 8 ) → ( 1 2 4 5 8 )

( 1 2 4 5 8 ) → ( 1 2 4 5 8 )

Third pass

Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one additional whole pass without any swap to know it is sorted.

Implement the above algorithm. 

Example:

Please enter some numbers: 123 45 78 6 5 48 38 98
[5, 6, 38, 45, 48, 78, 98, 123]

 

Please put your program in bubble_sort.py

Bubble sort is actually a quite inefficient way of sorting: it takes a lot of steps compared to other algorithms such as mergesort.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Types of Loop
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