Knowee
Questions
Features
Study Tools

Questiona) Write a program to read a set of numbers (between 10 to 20) from the keyboard and storethem in an array.b) Sort the numbers in ascending order with the Insertion sorting algorithm.c) Calculate how many times it executes the while of the algorithm.

Question

Questiona) Write a program to read a set of numbers (between 10 to 20) from the keyboard and storethem in an array.b) Sort the numbers in ascending order with the Insertion sorting algorithm.c) Calculate how many times it executes the while of the algorithm.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python program that accomplishes the tasks you've asked for:

def insertion_sort(arr):
    count = 0
    for i in range(1, len(arr)):
        key = arr[i]
        j = i-1
        while j >=0 and key < arr[j] :
                arr[j+1] = arr[j]
                j -= 1
                count += 1
        arr[j+1] = key
    return count

# a) Read a set of numbers from the keyboard and store them in an array
numbers = []
while len(numbers) < 10 or len(numbers) > 20:
    numbers = input("Enter a set of numbers between 10 to 20, separated by spaces: ").split()
    numbers = [int(num) for num in numbers]

# b) Sort the numbers in ascending order with the Insertion sorting algorithm
insertion_sort(numbers)
print("Sorted numbers: ", numbers)

# c) Calculate how many times it executes the while of the algorithm
count = insertion_sort(numbers)
print("The while loop in the algorithm executed {} times.".format(count))

This program first reads a set of numbers from the keyboard and stores them in an array. It then sorts the numbers in ascending order using the Insertion sorting algorithm. Finally, it calculates how many times the while loop in the algorithm executes.

This problem has been solved

Similar Questions

Identify the valid sequence of statements to read integer data from the keyboard.*1 pointimport java.util.Scanner;int num = scan.nextInt();Scanner scan = new Scanner(System.in);Scanner scan = new Scanner(System.in); import java.util.Scanner; int num = scan.nextInt();import java.util.Scanner; Scanner scan = new Scanner(System.in); int num = scan.nextInt();Scanner scan = new Scanner(System.in); int num = scan.nextInt(); import java.util.Scanner;

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

Single File Programming QuestionProblem Statement:Thiru is working on a grading system for his class of students. He needs a program that takes input for student scores, inserts a new score at the beginning and end of the existing scores, and then displays the modified list of scores.Write a program to help Thiru achieve this.Input format :The first line of input is an integer, the value n, indicating the number of elements in the array.The second line of input consists of n space-separated integers, representing the elements of the array arr[i].The third line of input consists of two integers M and P, representing the value to be inserted at the beginning and ending of the array, separated by a space.Output format :The output is a single line containing n + 2 space-separated integers, which represent the modified array after inserting the element at the beginning and ending of the existing scores.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:1 ≤ n ≤ 101 ≤ arr[i] ≤ 1001 ≤ M, P ≤ 100Sample test cases :Input 1 :53 4 5 6 72 8Output 1 :2 3 4 5 6 7 8 Input 2 :14590 78Output 2 :90 45 78 Input 3 :1098 37 48 28 16 18 20 100 25 131 19Output 3 :1 98 37 48 28 16 18 20 100 2

Type the program's outputa = int(input())b = int(input())c = int(input())while a < b: print(a) if a > c: break a += 2 Input2144Output

Single File Programming QuestionProblem StatementEmma, a computer science student, is organizing a list of names for a project. She needs a program to input a set of names and arrange them in alphabetical order. She wants to write a program that takes the number of names (n) as input and then receives n names. The program should then arrange the names in alphabetical order and print the sorted list.Emma approached you for help, and you are tasked with creating a program to assist her in achieving this.Input format :The first line consists of an integer N, representing the number of names.The next N lines of input consist of a string in each line, representing the names.Output format :The output displays strings, representing the sorted names in alphabetical order, separated by a line.Refer to the sample outputs for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:2 ≤ N ≤ 103 ≤ Length of each name ≤ 14Sample test cases :Input 1 :5KarthickRamKumarUmaSeethaOutput 1 :KarthickKumarRamSeethaUmaInput 2 :8KlausCatherineeeTylerDamonElenaStefanBonnieElijahOutput 2 :BonnieCatherineeeDamonElenaElijahKlausStefanTylerInput 3 :10WilliamOliviaJamesEmmaLiamAvaBenjaminSophiaMiaShakespeareOutput 3 :AvaBenjaminEmmaJamesLiamMiaOliviaShakespeareSophiaWilliamInput 4 :2EveCharliePuthOutput 4 :CharliePuthEve

1/3

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.