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

This is for a beginners c++ class For this assignment, you’ll be creating a Bubb

ID: 3863318 • Letter: T

Question

This is for a beginners c++ class

For this assignment, you’ll be creating a Bubble Sort algorithm. This algorithm is capable of taking a list of numbers and placing them in order, however it is also known as one of the least efficient means of doing so.

For your assignment, you’ll need to not only sort the given input (and display it in its final sorted form) but you’ll also need to keep track of how many comparisons, and reassignments happen. So comparing two numbers will be counted as a single action. Swapping the location of two variables will meanwhile be considered THREE actions, one to store a variable to be swapped in a temporary storage location, another to swap the first of the variables to the second location, and a third to move the variable from the temporary storage location back to where it’s going to go.

So to summarize, comparing two objects in the array will count as ONE action, and swapping two objects in the array will count as THREE actions.

Your results should not only display the sorted version of the array, but also a count of the total number of actions that took place while sorting. Please sort the following three lists.

{1,3,7,5,2,4,6,8,9,10}

{10,9,8,7,6,5,4,3,2,1}

{1,10,2,9,3,8,4,7,5,6}

An example output might look like:

Sorted List: (1,2,3,4,5,6,7,8,9,10) 124 Actions Performed.

Explanation / Answer

#include <iostream>
using namespace std;

int bubble_sort (int arr[], int n)
{
    int action=0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n - i - 1; ++j)
{
   action++;
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
action++;
arr[j] = arr[j + 1];
action++;
arr[j + 1] = temp;
action++;


}
}
return action;
}      

int main(int argc, char const *argv[])
{
   int a[10] = {1,3,7,5,2,4,6,8,9,10};
   int b[10] = {10,9,8,7,6,5,4,3,2,1};
   int c[10] = {1,10,2,9,3,8,4,7,5,6};
   cout<<"Array a before sorting ";

   for (int i = 0; i < 10; ++i)
   {
       cout<<a[i]<<" ";
   }
   int ac=bubble_sort(a,10);
   cout<<" Array a after sorting ";
   for (int i = 0; i < 10; ++i)
   {
       cout<<a[i]<<" ";
   }
   cout<<" Actions performed for array a is "<<ac<<endl;


   cout<<"Array b before sorting ";

   for (int i = 0; i < 10; ++i)
   {
       cout<<b[i]<<" ";
   }
   ac=bubble_sort(b,10);
   cout<<" Array b after sorting ";
   for (int i = 0; i < 10; ++i)
   {
       cout<<b[i]<<" ";
   }
   cout<<" Actions performed for array b is "<<ac<<endl;


   cout<<" Array c before sorting ";

   for (int i = 0; i < 10; ++i)
   {
       cout<<c[i]<<" ";
   }
   ac=bubble_sort(c,10);
   cout<<" Array c after sorting ";
   for (int i = 0; i < 10; ++i)
   {
       cout<<c[i]<<" ";
   }
   cout<<" Actions performed for array c is "<<ac<<endl;


   return 0;
}

=======================================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ g++ bbu.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Array a before sorting
1 3 7 5 2 4 6 8 9 10
Array a after sorting
1 2 3 4 5 6 7 8 9 10
Actions performed for array a is 66
Array b before sorting
10 9 8 7 6 5 4 3 2 1
Array b after sorting
1 2 3 4 5 6 7 8 9 10
Actions performed for array b is 180

Array c before sorting
1 10 2 9 3 8 4 7 5 6
Array c after sorting
1 2 3 4 5 6 7 8 9 10
Actions performed for array c is 105

===========================================================

Let me know if you have any doubt or problem

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote