Question

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, the bubble sort leaves the values as they are. if a pair is in decreasing order the bubbles sort swaps their values in the array.

The First pass cp,[ares the first two elements values of the arrow and swaps them if necessary.it then compares the second and third element values in the array. the end of this pass compares the last twon elements values in the array and swamps them if neccessary. After one pass, the largest value will be in the last element, after two passesm the largest two valuyes will be in the last two elements. explain why the bubble sort is an O(n^2) algorithm

(Enhanced Bubble Sort) Make the following simple modifications to improve the performance of the bubble sort you developed previously.

a) after the first pass, the largest value is guaranteed to be in the highest- numbered element of the array after the second pass, the two highest values are "in place"; and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make only the eight necessary comparisons on the second pass, seven on the third pass, and so on.

b) the data in the array may already be in the proper order 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 have been made, the data must already be in the proper order, so the program should terminate. if swaps have been made, at least one more pass is needed.

1 0
Add a comment Improve this question Transcribed image text
Answer #1

Time Complexity is O(n2), why ?

In an array of n elements, n-1 comparisons will be made in first pass, n-2 comparisons in second pass and so on

So, total no. of comparisions -= (n-1) + (n-2) + (n-3) +............ + 3+2+1 = n(n-1) /2

= O(n2)

C++ program:

#include <iostream>
using namespace std;


//function to swap values
void swap(int *m, int *n)
{
   int temp = *m;
   *m = *n;
   *n = temp;
}

// Enhanced Bubble Sort
void bubbleSort(int array[], int size)
{
int i, j;
bool flag;
for (i = 0; i < size-1; i++)
{
   flag = false;
  
   /* j< size-i-1 will take care of the no. of comparisons in each pass. Value of i will increase in
   each pass and hence will decrease the value of size -i-1 and hence will reduce the no.of comparisons
   in next passes */
  
   for (j = 0; j < size-i-1; j++)
   {
       if (array[j] > array[j+1])
       {
       swap(&array[j], &array[j+1]);
       flag = true;
       }
   }

   // check at the end of each pass whether any swaps have been made. if none have been made,the program will terminate
   if (flag == false)
       break;
}
}

// main program
int main()
{
   int i;
   int array[] = {14, 21, 5, 20, 77, 1, 9};
   int size = sizeof(array)/sizeof(array[0]);
   bubbleSort(array, size);
   cout<<"Array after Sorting : ";
   for (i=0; i < size-1; i++)
   cout<<array[i]<< ", ";
   cout<<array[size-1];

   return 0;
}

OUTPUT:

Output Array after Sorting : 1, 5, 9, 14, 20, 21, 77

Add a comment
Know the answer?
Add Answer to:
Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...
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
  • Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is...

    Programming language: Java Home Work No.2 due 09.11.2019 either ascending or descending SORTING: An array is said to be ordered if its values order. In an ascending ordered array, the value of each element is less than or equal to the value of the next element. That is, [each element] <= [next element]. A sort is an algorithm for ordering an array. Of the many different techniques for sorting an array we discuss the bubble sort It requires the swapping...

  • 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...

  • C++ Search & Sort

    Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides.  Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.  Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...

  • Bubble Sort Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent...

    Bubble Sort Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. Example: First Pass: ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8...

  • 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...

  • //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;...

  • Introduction BUBBLE SORT: Step-by-step example Let us take the array of numbers "5 1 4 2...

    Introduction BUBBLE SORT: Step-by-step example Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required First Pass: ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ). Here, algorithm compares the first two elements, swaps since 5 (15428)→(14528). Swap since 5 > 4 (14528)→(14258). Swap...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • 1. The bubble sort: a. Finds the smallest value and exchanges it with the first value,...

    1. The bubble sort: a. Finds the smallest value and exchanges it with the first value, then continues with the second value, third value, etc. b. Is a system of comparisons and exchanges of adjacent elements to move the largest to the bottom of the selected group of values. c. Is a system of comparisons and exchanges of elements that are non-adjacent. The gap is halved at each pass. d. None of the above. For the following array answer Q2...

  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

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