Please create 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. Functionally, a Bubble Sort will proceed from left to right through an array, comparing pairs on its way. If the first half of the pair it's looking at is larger than the second half of the pair, it will swap them, then continue to the next pair. It will then repeat this process several times over until the list is fully sorted. Please view the example below: You'll notice the underlined numbers as we go. Once the Bubble Sort visits the LAST sorted element in an array, it doesn't have to bother with that element anymore, because it's already where it will be in the final sorted array. This can be accomplished in a simple code by keeping track of where the final sorted variable is located with just a simple integer. For this 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. 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 two lists.{1,2,3,4,5,6,7,8,9,10}{10,9,8,7,6,5,4,3,2,1}

Respuesta :

Answer:

#include<iostream>

using namespace std;

void swap(int & a,int & b){

       int temp=a;

       a=b;

       b=temp;

}

void bubbleSort(int array[],int N){

       int comparisonCount=0;

       int swapCount=0;

       for(int i=0;i<N-1;i++){

               for(int j=0;j<N-1-i;j++){

                   comparisonCount++;

                       if(array[j]<array[j+1]){

                               swapCount++;

                               swap(array[j],array[j+1]);

                   }

               }

       }

       cout<<"Total Comparison :"<<comparisonCount<<"\n";

       cout<<"Total swaps      :"<<swapCount<<"\n";

       cout<<"Total Operations :"<<comparisonCount+3*swapCount<<"\n";

}

void displayArray(int Array[],int N){

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

               cout<<Array[i]<<" ";

       cout<<"\n\n";  

}

int main(){

       int Array[10]={1,3,7,5,2,4,6,8,9,10};

       cout<<"Array is :";

       displayArray(Array,10);

       bubbleSort(Array,10);

       cout<<"Sorted Array: ";

       displayArray(Array,10);

       return 0;

}

ACCESS MORE