Question

Complete the following function with Bubble Sort #include <iostream> using std::cout; using std::endl; void bubble(int arr[],...

Complete the following function with Bubble Sort

#include <iostream>

using std::cout;
using std::endl;

void bubble(int arr[], int size)
{
/*YOUR CODE GOES HERE */
}

void printlist(int arr[], int size)
{
for(int i = 0; i < size; i++)
cout << arr[i] << ", ";
cout << endl;
}

int main()
{
int list[] = {20, 56, 23, 2, 1, 90, 1002, 103, 342, 12};

cout << "Before: " << endl;
printlist(list, 10);
bubble(list, 10);

cout << "After: " << endl;
printlist(list, 10);
}

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

#include <iostream>

using std::cout;

using std::endl;

void bubble(int arr[], int size)

{

int i, j;

for (i = 0; i < size-1; i++)

// Last i elements are already in place

for (j = 0; j < size-i-1; j++)

if (arr[j] > arr[j+1]) {

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

}

/*YOUR CODE GOES HERE */

}

void printlist(int arr[], int size)

{

for(int i = 0; i < size; i++)

cout << arr[i] << ", ";

cout << endl;

}

int main()

{

int list[] = {20, 56, 23, 2, 1, 90, 1002, 103, 342, 12};

cout << "Before: " << endl;

printlist(list, 10);

bubble(list, 10);

cout << "After: " << endl;

printlist(list, 10);

}

Add a comment
Know the answer?
Add Answer to:
Complete the following function with Bubble Sort #include <iostream> using std::cout; using std::endl; void bubble(int arr[],...
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
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