Question

I need help of how to include the merge sort code into this counter code found...

I need help of how to include the merge sort code into this counter code found below:

#include<iostream>
using namespace std;
int bubble_counter=0,selection_counter=0; // variables global

void bubble_sort(int [], int);
void show_array(int [],int);
void selectionsort(int [], int );

int main()
{
int a[7]={4,1,7,2,9,0,3};
show_array(a,7);
//bubble_sort(a,7);
selectionsort(a,7);
show_array(a,7);
cout<<"Bubble counter = "<<bubble_counter<<endl;
cout<<"Selection Counter = "<<selection_counter<<endl;

return 0;
}
void show_array(int array[],int size)
{
for( int i=0 ; i<size; i++)
{
cout<<array[i]<< " ";
}
cout<<endl;
}
void bubble_sort( int array[], int size)
{
bool swap;
int temp;

do
{
swap =false;
for(int count=0; count< (size -1);count ++)
{ bubble_counter++;
if(array[count]>array[count +1])
{
temp =array[count];
array[count]=array[count +1];
array[count +1]= temp;
swap = true;
}
}
  
}while(swap);

}
void selectionsort(int array [], int size)
{
int startscan, minindex, minivalue;

for (startscan = 0; startscan < (size - 1); startscan++)
{
minindex = startscan;
minivalue = array[startscan];
for(int index = startscan + 1; index < size; index++)
{ selection_counter++;
if (array[index] <minivalue)
{
minivalue = array[index];
minindex = index;
}
}
array[minindex] = array[startscan];
array[startscan] = minivalue;
}
}

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

Updated C++ Program:

#include<iostream>
using namespace std;

int bubble_counter=0, selection_counter=0; // variables global

void bubble_sort(int [], int);
void show_array(int [],int);
void selectionsort(int [], int);

int mergeSort(int[], int, int);
int mergeArrays(int[], int, int, int);


int main()
{
int a[7]={4,1,7,2,9,0,3};
int mergeCounter=0;
show_array(a,7);
//bubble_sort(a,7);
//selectionsort(a,7);

mergeCounter = mergeSort(a, 0, 6); //Merge Sort

show_array(a,7);
cout<<"Bubble counter = "<<bubble_counter<<endl;
cout<<"Selection Counter = "<<selection_counter<<endl;
cout<<"Merge Sort Counter = "<<mergeCounter<<endl;
return 0;
}

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

void bubble_sort( int array[], int size)
{
bool swap;
int temp;

do
{
swap =false;
for(int count=0; count< (size -1);count ++)
{
bubble_counter++;
if(array[count]>array[count +1])
{
temp =array[count];
array[count]=array[count +1];
array[count +1]= temp;
swap = true;
}
}
}while(swap);
}

void selectionsort(int array [], int size)
{
int startscan, minindex, minivalue;

for(startscan = 0; startscan < (size - 1); startscan++)
{
minindex = startscan;
minivalue = array[startscan];
for(int index = startscan + 1; index < size; index++)
{
selection_counter++;
if (array[index] <minivalue)
{
minivalue = array[index];
minindex = index;
}
}
array[minindex] = array[startscan];
array[startscan] = minivalue;
}
}


//Merge Sort implementation
//Function that sorts the array
int mergeSort(int arr[], int low, int high)
{
int mid;
int merge_counter = 0;

//Loop till both boundaries meet
if(low < high)
{
//Calculating middle position of array
mid=(low+high)/2;

//Operating on left side of array
merge_counter = merge_counter + mergeSort(arr, low, mid);

//Operating on right side of array
merge_counter = merge_counter + mergeSort(arr, mid+1, high);

//Merging arrays
merge_counter = merge_counter + mergeArrays(arr, low, mid, high);
}
return merge_counter;
}

//Function that sorts two arrays into single array
int mergeArrays(int arr[],int low,int mid,int high)
{
int cmpCnt=0;
int i, j, k, temp[50];

//Initializing indexes
i = low;
k = low;

j = mid + 1;

//Loop till either of array reaches boundary
while (i <= mid && j <= high)
{
//Comparing elements
if (arr[i] < arr[j])
{
//Adding to array from left side
temp[k] = arr[i];

//Incrementing index
k++;
i++;
cmpCnt++;
}
else
{
//Adding to array from left side
temp[k] = arr[j];

//Incrementing index
k++;
j++;
cmpCnt++;
}
}

//Adding elements from left side array
while (i <= mid)
{
temp[k] = arr[i];
k++;
i++;
}

//Adding elements from right side array
while (j <= high)
{
temp[k] = arr[j];
k++;
j++;
}

//Copying values
for (i = low; i < k; i++)
{
arr[i] = temp[i];
}

return cmpCnt;
}
______________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
I need help of how to include the merge sort code into this counter code found...
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
  • How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

    How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() {    int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • read in numbers into array , print out, sort - my program reads in and prints...

    read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++ #include <iostream> using namespace std; void sortarray(int *tarray, int ); void displayarray(int *[], int); void averageshowarray(int *[], int); int main() {     int Size;     int count;     cout << "enter the size: " << endl;     cin >> Size;     int...

  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code...

    Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { ​int temp = numbers[i];​ /* put numbers[i] somewhere to keep it safe */ ​numbers[i] = numbers[j]; /* put numbers[j] at index i */ ​numbers[j] = temp;​ /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

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