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

11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort progr

ID: 3817507 • Letter: 1

Question

11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I Fig. 6.15: fig06 15.c 2 Sorting an array's values into ascending order. Finclude 4 #define SIZE 10 6 function main begins program execution 7 int main(void) 9 initialize a lo int a CSIZEJ 12, 6, 4, 8, 10, 12, 89, 68, 45, 37) 12 putsc Data items in original order output original array IS for (size t i 0; i SIZE ++i) a[i]) Fig. 6.15 I Sorting an array's values into ascending order. (Part I of 3) bubble sort loop to control number of passes for (unsigned int pass 1: pass SIZE: pass) loop to control number of comparisons per pass for (size t f 1: ++i) compare adjacent ements and swap then if first element is greater than second element if Cali] at 1]) int hold at a hold Fig. 6.15 I Sorting an aray's values into ascending order. Par 2 3) 2. Declare an array of float of size 365 to store daily temperatures fo write C program to find: The hottest and coldest days of the year

Explanation / Answer

//sorting an array's values into assending order.

#include<stdio.h>
#define SIZE 10
//function main begins program execution
int main(void)
{
    int i,j,n;
   int temp =0;
        int swap;

   //initialize a

   int a[SIZE]={2,6,4,8,10,12,89,68,45,37};

   puts("Data item in original order");
  
   //outputs original order

   for( size_t i=0; i<SIZE; ++i)
   {
           printf("%d",a[i]);
   }

   //sorting of array using bubble sort

  
   for( i = 0 ; i<SIZE-1 ; i++)
   {
       swap = 0;//checks for the swap of two numbers

        for(j = 0 ;j<SIZE-1-1 ; j++)
        {
                 if(a[j]> a[j+1])
                {
               temp = a[j];
               a[j] = a[j+1];
               a[j+1]=temp;
               swap=1;
              
                    }
           }
           if(!swap)
       break;
   }

   //prints the sorted array

   for( size_t i=0; i<SIZE; ++i)
   {
           printf("%d",a[i]);
   }  
}

If we try to keep track that no swap is made in a given iteration then there is no need for next iteration.
Total comparisons= 9 * (10-2) = 72
swap=20.
if we make use of the fact that there is no need to traverse till the end of the inner loop then we can reduce many comparisions
the total comparisions reduced= total number of rows(which is 44).and swap remains the same.


//declare variables
#include<stdio.h>
#define SIZE 365

//function main begins program execution
int main(void)
{
    int i;
  
       //initialize temperature

  
   float temp[SIZE];
   float avg,max,min,sum=0.0;

   // enter the temperature of year

   for(i=0;i<SIZe-1;i++)
   {
  
       scanf("%f"temp[i]);
   }



    // initialize the first element as maximum and minimum temperatures

    max = temp[0];
    min = temp[0];


     //Finding maximum and minimum temperatures in the year.
   
    for(i=1; i<size; i++)
    {
        /* If current temperature of array is greater than max */
        if(temp[i]>max)
        {
            max = temp[i];
        }

        /* If current temperature of array is smaller than min */
        if(temp[i]<min)
        {
            min = temp[i];
        }
    }

  
    // Prints the maximum and minimum element
   
    printf("Maximum temperature of a year = %d ", max);
    printf("Minimum temperature of a year = %d", min);


// to find the average temperature of each month
// average of january
for(i=0;i<30;i++)
{
sum += temp[i];
  

    avg = sum / 31;
    printf("Avg = %.2f", avg);

}
//average of febraury
for(i=0;i<28;i++)
{
sum += temp[i];
  

    avg = sum / 28;
    printf("Avg = %.2f", avg);

}
//average ofmarch
for(i=0;i<30;i++)
{
sum += temp[i];
  

    avg = sum / 31;
    printf("Avg = %.2f", avg);

}
//average of april
for(i=0;i<29;i++)
{
sum += temp[i];
  

    avg = sum / 30;
    printf("Avg = %.2f", avg);

}
//average of may
for(i=0;i<30;i++)
{
sum += temp[i];
  

    avg = sum / 31;
    printf("Avg = %.2f", avg);

}
//average of june
for(i=0;i<29;i++)
{
sum += temp[i];
  

    avg = sum / 30;
    printf("Avg = %.2f", avg);

}
//average of july
for(i=0;i<30;i++)
{
sum += temp[i];
  

    avg = sum / 31;
    printf("Avg = %.2f", avg);

}
//average of august
for(i=0;i<30;i++)
{
sum += temp[i];
  

    avg = sum / 31;
    printf("Avg = %.2f", avg);

}
//average of september
for(i=0;i<29;i++)
{
sum += temp[i];
  

    avg = sum / 30;
    printf("Avg = %.2f", avg);

}
//average of october
for(i=0;i<30;i++)
{
sum += temp[i];
  

    avg = sum / 31;
    printf("Avg = %.2f", avg);

}
//average of november
for(i=0;i<29;i++)
{
sum += temp[i];
  

    avg = sum / 30;
    printf("Avg = %.2f", avg);

}
//average of december
for(i=0;i<30;i++)
{
sum += temp[i];
  

    avg = sum / 31;
    printf("Avg = %.2f", avg);

}
}