Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this bubble sort. I need to add a counter to check the comparisons. In order words, how often I check one element in

I have this bubble sort. I need to add a counter to check the comparisons. In order words, how often I check one element in the array against another. Am I doing it, right? If not, How can I fix it? Thank you

#include using namespace std;

void bubbleSort(int theArray[], int n) { int comparisons = 0; //counter is bool sorted = false; int pass = 1; while(!sorted && (pass the entries in theAraay[0..n-pass] sorted = true; for(int index = 0; index < n-pass; index++) { comparisons++; // At this point, all entires in theArrat[0.. index-1] // are <= theArray[index] int nextIndex = index+1; if(theArray[index] > theArray[nextIndex]) { //exchange entries swap(theArray[index], theArray[nextIndex]); sorted = false; //signel exchange } // end if } //end for //asseertion: theArray[0..n-pass-1] < theArray[n-pass]

pass++; }//end while

cout << "Number of comparison: " << comparions; }//end bubbleSort;

void displayArray(int theArray[], int size) { for(int i=0; i

int main() { int data[] = {1}; //int data1[] = {2, 1}; //int data2[] ={ 1,2}; int data3[] = {4, 1, 3, 2, 0, 7};

cout<<"The array data contains: " << endl; displayArray(data,1); cout<

cout<<"The array data3 contains: " << endl; displayArray(data3,6); cout<

int array8a[] = {1, 4, 23, 37, 2, 7,3, 9}; int array8b[] = {9,7,2, 37, 23, 4, 1, 3};

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Database Security Applications And Trends

Authors: Michael Gertz, Sushil Jajodia

1st Edition

1441943056, 978-1441943057

More Books

Students also viewed these Databases questions