Make a program that: Asks a user to enter 20 grades. Each grade is put into a sl
ID: 3701658 • Letter: M
Question
Make a program that: Asks a user to enter 20 grades. Each grade is put into a slot in an array. Print out the original values. Sort the grades, using a bubble sort, from highest grade to lowest grade. Then print out the sorted grades. Then, make another function with an improved bubble sort -- explanations on changes are here. Make sure to test this function before you submit it.: Note: The traditional bubble sort is not efficient enough for large arrays. Make the following simple modifications to improve its performance. 1. After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass and so on. (This will match the Wikipedia version) 2. The data in the array may already be in the proper or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass whether any swaps have been made. If none has been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed. C program Make a program that: Asks a user to enter 20 grades. Each grade is put into a slot in an array. Print out the original values. Sort the grades, using a bubble sort, from highest grade to lowest grade. Then print out the sorted grades. Then, make another function with an improved bubble sort -- explanations on changes are here. Make sure to test this function before you submit it.: Note: The traditional bubble sort is not efficient enough for large arrays. Make the following simple modifications to improve its performance. 1. After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass and so on. (This will match the Wikipedia version) 2. The data in the array may already be in the proper or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass whether any swaps have been made. If none has been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed. C program Make a program that: Asks a user to enter 20 grades. Each grade is put into a slot in an array. Print out the original values. Sort the grades, using a bubble sort, from highest grade to lowest grade. Then print out the sorted grades. Then, make another function with an improved bubble sort -- explanations on changes are here. Make sure to test this function before you submit it.: Note: The traditional bubble sort is not efficient enough for large arrays. Make the following simple modifications to improve its performance. 1. After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass and so on. (This will match the Wikipedia version) 2. The data in the array may already be in the proper or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass whether any swaps have been made. If none has been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed. C programExplanation / Answer
ScreenShot
--------------------------------------------------------------------------------------------------------------------------------------
Program
//Header file
#include <stdio.h>
#include <stdbool.h>
//Buble sort initialization
int bubleSort(int arr[],int size);
//Main function
int main(void) {
//Array initialization
int elemArray[20];
//Enter elements into the array
printf("Enter 20 elements in to the array:");
for(int i=0;i<20;i++){
scanf("%d",&elemArray[i]);
}
//print unsorted array
printf(" Unsorted array :");
for(int i=0;i<20;i++){
printf(" %d",elemArray[i]);
}
//Call buble sort
bubleSort(elemArray,20);
//print sorted array
printf(" Sorted array: ");
for ( int i = 0 ; i < 20 ; i++ )
printf("%d ", elemArray[i]);
return 0;
}
//Buble sort definition
int bubleSort(int arr[],int size){
for (int i = 0 ; i <size-1; i++)
{
for (int j = 0 ; j <size - i - 1; j++)
{
if (arr[j] < arr[j+1])
{
int swap = arr[j];
arr[j] = arr[j+1];
arr[j+1] = swap;
}
}
}
return arr;
}
------------------------------------------------------------------------------------------------------------------------
Program for improved bubble sort
#include <stdio.h>
#include <stdbool.h>
void bubbleSort(int *arr,int size);
int main(void) {
//Array initialization
int elemArray[20];
//Enter elements into the array
printf("Enter 20 elements in to the array:");
for(int i=0;i<20;i++){
scanf("%d",&elemArray[i]);
}
//print unsorted array
printf(" Unsorted array :");
for(int i=0;i<20;i++){
printf(" %d",elemArray[i]);
}
//improved bubblesort
bubbleSort(elemArray,20);
//print sorted array
printf(" improved Sorted array: ");
for ( int i = 0 ; i < 20 ; i++ )
printf("%d ", elemArray[i]);
return 0;
}
//improved buble sort
void bubbleSort(int *array, int n)
{
for(int i=0; i<n; i++)
{
bool check = false;
for(int j=0; j<n-i-1; j++)
{
if(array[j]<array[j+1])
{
check = true;
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
// No Swapping happened, array is sorted
if(!check){
return;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.