Write a functional program that sorts 10 input integers (signed) using either bubble sort orselection sort algorithm. The output window must show both unsorted array of integers andsorted array of integers.
Question
Write a functional program that sorts 10 input integers (signed) using either bubble sort orselection sort algorithm. The output window must show both unsorted array of integers andsorted array of integers.
Solution
Here is a Python program that uses the Bubble Sort algorithm to sort a list of 10 integers:
def bubble_sort(nums):
# We set swapped to True so the loop looks runs at least once
swapped = True
while swapped:
swapped = False
for i in range(len(nums) - 1):
if nums[i] > nums[i + 1]:
# Swap the elements
nums[i], nums[i + 1] = nums[i + 1], nums[i]
# Set the flag to True so we'll loop again
swapped = True
# Prompt the user for 10 integers
nums = []
for i in range(10):
nums.append(int(input("Enter integer #{}: ".format(i+1))))
print("Unsorted:", nums)
bubble_sort(nums)
print("Sorted:", nums)
This program first defines a function bubble_sort that sorts a list of numbers using the Bubble Sort algorithm. It then prompts the user to enter 10 integers, stores them in a list, and prints the unsorted list. It then sorts the list using the bubble_sort function and prints the sorted list.
Similar Questions
Design a Java program that allows users to input a series of integers into an array and then sorts the array in ascending order. Implement the sorting algorithm using the bubble sort technique.Write a Java code that accomplishes the following tasks:Prompt the user to enter the number of elements in the array.Prompt the user to input each element of the array.Sort the array in ascending order using the bubble sort algorithm.Display the sorted array.Demonstrate the functionality of your program with a sample input/output scenario.Sample Input12 // No of elements 23 // Elements to store in an array674512900260725434129865Sample Output12 12 23 34 45 54 65 67 72 98 260 900
You are tasked with implementing one of the classic sorting algorithms: Bubble Sort, Insertion Sort, or Selection Sort. Your goal is to sort an array of integers in non-decreasing order using the chosen algorithm.Input FormatThe input consists of two lines:The first line contains an integer n (1 ≤ n ≤ 100), representing the number of elements in the array.The second line contains n space-separated integers, representing the elements of the unsorted array. These integers are in the range -1000 to 1000.ConstraintsImplement the chosen sorting algorithm to sort the array in-place. Do not use any built-in sorting functions.The array elements are integers in the range -1000 to 1000.Output FormatYou should output a single line with n space-separated integers, representing the elements of the array in non-decreasing order after applying the selected sorting algorithm.Sample Input 0102 3 4 5 1 6 7 8 9 0Sample Output 00 1 2 3 4 5 6 7 8 9Explanation 0It appears you have two lines of input. The first line contains the number 10, which likely represents the total number of elements in the list. The second line contains ten space-separated integers: 2 3 4 5 1 6 7 8 9 0. These are the elements you want to sort in non-decreasing order using a sorting algorithm.So, the sorted version of the list you provided is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, in non-decreasing order.Sample Input 162 4 5 4 6 2Sample Output 12 2 4 4 5 6
Bubble Sort is a popular sorting algorithm. It works by repeatedly swapping adjacent elements thatare out of order.a) Write the pseudo code of the bubblesort algorithmb) Implement the bubblesort algorithm.c) Read 8 numbers from the keyboard and store them in an array. Sort the numbers usingthe bubble sort algorithm.
Write a class to sort the given set of integers in descending order. Include a try block tolocate the array index out of bounds exception and catch it.
Write a program with a generic function that will sort array of integer, float value. The program should have a menu with appropriate options and accept the values from the user
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.