Question: 1. Compile de Program in C++ #include using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); // prototipos void show_array(int [], int);
1. Compile de Program in C++
#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);
// C++ program for Merge Sort
#include
using namespace std;
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
// Create temp arrays
int L[n1], R[n2];
// Copy data to temp arrays L[] and R[]
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
// Merge the temp arrays back into arr[l..r]
// Initial index of first subarray
int i = 0;
// Initial index of second subarray
int j = 0;
// Initial index of merged subarray
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
// Copy the remaining elements of
// L[], if there are any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of
// R[], if there are any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// l is for left index and r is
// right index of the sub-array
// of arr to be sorted */
void mergeSort(int arr[],int l,int r){
if(l>=r){
return;//returns recursively
}
int m = (l+r-1)/2;
mergeSort(arr,l,m);
mergeSort(arr,m+1,r);
merge(arr,l,m,r);
}
// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
for (int i = 0; i < size; i++)
cout << A[i] << " ";
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
cout << "Given array is ";
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
cout << " Sorted array is ";
printArray(arr, arr_size);
return 0;
}
// This code is contributed by Mayank Tyagi
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;
}
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
