Emma, a financial analyst, is working on organizing stock prices efficiently. She needs a program that sorts the given stock prices in ascending order. Your task is to assist Emma by creating a program that inputs the number of stock prices, takes input for each price, applies a bubble sort algorithm, and then displays the sorted stock prices.Input format :The first line of input consists of an integer N, representing the number of stock prices.The second line consists of N space-separated integers, representing the stock prices.Output format :
Question
Emma, a financial analyst, is working on organizing stock prices efficiently. She needs a program that sorts the given stock prices in ascending order. Your task is to assist Emma by creating a program that inputs the number of stock prices, takes input for each price, applies a bubble sort algorithm, and then displays the sorted stock prices.Input format :The first line of input consists of an integer N, representing the number of stock prices.The second line consists of N space-separated integers, representing the stock prices.Output format :
Solution
Here is a Python program that can help Emma sort the stock prices in ascending order using the bubble sort algorithm:
def bubble_sort(stock_prices):
n = len(stock_prices)
for i in range(n):
for j in range(0, n - i - 1):
if stock_prices[j] > stock_prices[j + 1] :
stock_prices[j], stock_prices[j + 1] = stock_prices[j + 1], stock_prices[j]
return stock_prices
N = int(input("Enter the number of stock prices: "))
stock_prices = list(map(int, input("Enter the stock prices: ").split()))
sorted_prices = bubble_sort(stock_prices)
print("Sorted stock prices are: ", sorted_prices)
Here's how the program works:
-
The function
bubble_sortis defined to sort a list of numbers. It uses two nested loops to repeatedly compare and swap adjacent elements if they are in the wrong order. -
The program then asks for the number of stock prices (N) and the stock prices themselves. The
inputfunction is used to get user input, andmapandsplitare used to convert this input into a list of integers. -
The
bubble_sortfunction is called with the list of stock prices, and the sorted list is stored insorted_prices. -
Finally, the sorted list of stock prices is printed out.
Similar Questions
Write a description of bubble sort in programming, give the steps to follow, keep it short
Given an array of size N, implement Bubble Sort.Input FormatThe first line of input contains an integer N - the size of an array. The second line contains the elements of the array.Output FormatFor each iteration of Bubble Sort, print the array elements.Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput65 8 10 15 3 6Output5 8 10 3 6 155 8 3 6 10 155 3 6 8 10 153 5 6 8 10 153 5 6 8 10 15
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.
Problem StatementIn a computer science class, students are assigned unique identification letters based on their seating arrangement. Each student has a specific letter, and you are tasked with creating a program to sort these letters in ascending order using the bubble sort algorithm. The sorting should be based on their ASCII values. Input the total number of students and their assigned letters, then display the sorted list, allowing the students to easily identify their seating order.Input format :The first line of input consists of an integer N, representing the total number of students.The second line consists of N space-separated characters, each representing the unique identification letter assigned to a student.Output format :The output prints the sorted list of identification letters based on their ASCII values.Refer to the sample output for formatting specifications.Code constraints :1 ≤ N ≤ 15Each identification letter is a valid ASCII character.The input characters are case-sensitive.Sample test cases :Input 1 :5a y h j oOutput 1 :a h j o y Input 2 :7Q G Y O M N BOutput 2 :B G M N O Q Y Input 3 :8a e T V z X h KOutput 3 :K T V X a e h z
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
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.