In: Computer Science
#include <stdio.h>
#include <stdlib.h>
// The below function Merges two subarrays of arr[].
// First subarray is arr[low..mid]--left to middle
// Second subarray is arr[mid+1..high]--middle+1 element to right
most element
void merge(int arr[], int low, int mid, int high)
{
int i, j, k;
int len1 = mid - low + 1;
int len2 = high - mid;
//create two temporary arrays
int A[len1], B[len2];
/* Copy data to temporary arrays A[] and B[] */
for (i = 0; i < len1; i++)
A[i] = arr[low + i]; //Copies first half of array
for (j = 0; j < len2; j++)
B[j] = arr[mid + 1 + j]; //Copies second half of array
/* Merge the temporary arrays again into arr[low..high]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = low; // Initial index of merged subarray
while (i < len1 && j < len2) {
if (A[i] <= B[j]) {
arr[k] = A[i];
i++;
}
else {
arr[k] = B[j];
j++;
}
k++;
}
/* The remaining elements of A[] should be copied, if
there
are any */
while (i < len1) {
arr[k] = A[i];
i++;
k++;
}
/* The remaining elements of B[] should be copied, if
there
are any */
while (j < len2) {
arr[k] = B[j];
j++;
k++;
}
}
/* low is for left most index and high is right index of
the
sub-array of arr to be sorted */
void mergeSort(int arr[], int low, int high)
{
if (low < high) {
// checks if the left index is less than right index and avoids
overflow for
int mid = low + (high - low) / 2;
// Sort first half from low to mid
mergeSort(arr, low, mid);
//Sort second half from mid+1 to high
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high); //Call merge function
}
}
/* Function to print the array */
void print(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
/* main program to call the mergesort functions and read
inputs*/
int main()
{
int n; printf("enter number of elements");
scanf_s("%d", &n);
int arr[n];
if (n <= 50 && n >= 0)// condition to check if input
less than 50
{
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Given array is \n");
print(arr, n);
mergeSort(arr, 0, n - 1);
printf("\nSorted array is \n");
print(arr, n);
return 0;
}
else
printf("invalid count entered");//since count should be between 0
and 50
}
why it has an error
I wrote c program in visual studio. but it has error int A[len1], B[len2];
and
Solution:
Nothing is wrong with your code.
The code is compiling fine in dev c++ .
There is nothing wrong with the code.
It is running correctly.
Code:
#include <stdio.h>
#include <stdlib.h>
// The below function Merges two subarrays of arr[].
// First subarray is arr[low..mid]--left to middle
// Second subarray is arr[mid+1..high]--middle+1 element to right most element
void merge(int arr[], int low, int mid, int high)
{
int i, j, k;
int len1 = mid - low + 1;
int len2 = high - mid;
//create two temporary arrays
int *A = (int*) malloc (len1 * sizeof(int));
int *B = (int*) malloc (len2 * sizeof(int));
/* Copy data to temporary arrays A[] and B[] */
for (i = 0; i < len1; i++)
A[i] = arr[low + i]; //Copies first half of array
for (j = 0; j < len2; j++)
B[j] = arr[mid + 1 + j]; //Copies second half of array
/* Merge the temporary arrays again into arr[low..high]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = low; // Initial index of merged subarray
while (i < len1 && j < len2) {
if (A[i] <= B[j]) {
arr[k] = A[i];
i++;
}
else {
arr[k] = B[j];
j++;
}
k++;
}
/* The remaining elements of A[] should be copied, if there
are any */
while (i < len1) {
arr[k] = A[i];
i++;
k++;
}
/* The remaining elements of B[] should be copied, if there
are any */
while (j < len2) {
arr[k] = B[j];
j++;
k++;
}
free(A);
free(B);
}
/* low is for left most index and high is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[], int low, int high)
{
if (low < high) {
// checks if the left index is less than right index and avoids overflow for
int mid = low + (high - low) / 2;
// Sort first half from low to mid
mergeSort(arr, low, mid);
//Sort second half from mid+1 to high
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high); //Call merge function
}
}
/* Function to print the array */
void print(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
/* main program to call the mergesort functions and read inputs*/
int main()
{
int n; printf("enter number of elements");
scanf_s("%d", &n);
int *arr = (int*) malloc (n * sizeof(int));
if (n <= 50 && n >= 0)// condition to check if input less than 50
{
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Given array is \n");
print(arr, n);
mergeSort(arr, 0, n - 1);
printf("\nSorted array is \n");
print(arr, n);
free(arr);
return 0;
}
else
printf("invalid count entered");//since count should be between 0 and 50
}
Output: