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 arrays values into ascending order. Finclude <stdio.h> 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 arrays 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 arays 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
media%2F15c%2F15c72689-126f-4dd0-b0be-06
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//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\n", 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);

}
}

Add a comment
Know the answer?
Add Answer to:
11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT...

    //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT_SECURE_NO_WARNINGS //for windows os only #include <stdio.h> #include <stdlib.h> // Fig. 6.15: fig06_15.c // Sorting an array's values into ascending order. #include <stdio.h> #define SIZE 10 // function main begins program execution int main(void) {    // initialize a    int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };    printf("Data items in original order");    // output original array    for (int i = 0;...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

    Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT