Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Asks a user to enter 20 grades. Each grade is put into a slot in an array. Print

ID: 3691098 • Letter: A

Question

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. 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. PLEASE MAKE IS SIMPLE AS IT CAN BE AND IN C language or C++. thank you!!!

Explanation / Answer

#include<iostream>

using namespace std;

//function prototype
void bubblesort( float a[] , int n);


int main(){
  
   int n = 20;
  
   float grade[20];
  
   cout<<"Enter 20 grades : "<<endl;
  
   for(int i=0; i<n; i++){
       cin>>grade[i];
   }
  
   cout<<" Unsorted Data: "<<endl;
   for(int i=0; i<n; i++){
       cout<<grade[i]<<" ";
   }
   cout<<endl;
  
   //bubble sort
   bubblesort(grade, n);
  
   cout<<"Sorted Data: "<<endl;
   for(int i=0; i<n; i++){
       cout<<grade[i]<<" ";
   }
   cout<<endl;
   return 0;
}

//function defination
void bubblesort( float a[] , int n)
{
bool swaps = false;
for( int i = 0 ; i < n - 2 ; i++ )
{
swaps=0;
for ( int j = 0 ; j < n - i - 1 ; j++ )
{
if ( a[j] > a[j + 1] )
{
float temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
swaps = true;
}
}
if( swaps == false )
{
break;
}
}
}

/*

Sample run:

nter 20 grades :
32
56.76
67
87
98
56
45
98.89
655
44
3
4
3
5
65
77
8
8
9
88

Unsorted Data:
32 56.76 67 87 98 56 45 98.89 655 44 3 4 3 5 65 77 8 8 9 88
Sorted Data:
3 3 4 5 8 8 9 32 44 45 56 56.76 65 67 77 87 88 98 98.89 655

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote