Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Use the teacher's program and insert the merge sort and buy the coutner .haber if it is the fastest ordering. 2. Take the teacher's

1. Use the teacher's program and insert the merge sort and buy the coutner .haber if it is the fastest ordering.
2. Take the teacher's line that has the bubble counters and the other counters so that they count the count of each sort.
---------------
// teacher's program
#include
using namespace std;
int bubble_counter=0,selection_counter=0; // variables global
void bubble_sort(int [], int); // prototipos
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 = "<
cout<<"Selection Counter = "<
return 0;
}
void show_array(int array[],int size)
{
for( int i=0 ; i
{
cout<
}
cout<
}
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 = array[index];
minindex = index;
}
}
array[minindex] = array[startscan];
array[startscan] = minivalue;
}
}
__________________
#include
using namespace std;
void merge(int *,int, int , int );
void merge_sort(int *arr, int low, int high)
{
int mid;
if (low < high){
//divide the array at mid and sort independently using merge sort
mid=(low+high)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high);
//merge or conquer sorted arrays
merge(arr,low,high,mid);
}
}
// Merge sort
void merge(int *arr, int low, int high, int mid)
{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high) {
if (arr[i] < arr[j]) {
c[k] = arr[i];
k++;
i++;
}
else {
c[k] = arr[j];
k++;
j++;
}
}
while (i <= mid) {
c[k] = arr[i];
k++;
i++;
}
while (j <= high) {
c[k] = arr[j];
k++;
j++;
}
for (i = low; i < k; i++) {
arr[i] = c[i];
}
}
// read input array and call mergesort
int main()
{
int myarray[30], num;
cout<<"Enter number of elements to be sorted:";
cin>>num;
cout<<"Enter "<
for (int i = 0; i < num; i++) { cin>>myarray[i];
}
merge_sort(myarray, 0, num-1);
cout<<"Sorted array ";
for (int i = 0; i < num; i++)
{
cout<
}
}

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

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions

Question

4. Explain the characteristics of successful mentoring programs.

Answered: 1 week ago