Merge Coding Sample (intake 39 section 2)

Question

Write a code to implement merge from merge sort algorithm.

in progress 0
Ask me anything 2 months 16 Answers 154 views 0

Answers ( 16 )

  1. //Id:17183103060

    #include

    using namespace std;

    void Merge(int A[],int p, int q,int r)
    {

    int n1=q-p+1,i,j,k;

    int n2=r-q;

    int L[n1],R[n2];

    for(i=0; i<n1; i++)

    {
    L[i]=A[p+i];
    }

    for(j=0; j<n2; j++)
    {
    R[j]=A[q+j+1];
    }

    i=0,j=0;

    for(k=p; i<n1&&j<n2; k++)
    {
    if(L[i]<R[j])
    {
    A[k]=L[i++];
    }
    else
    {
    A[k]=R[j++];
    }
    }

    while(i<n1)
    {
    A[k++]=L[i++];
    }

    while(j<n2)
    {
    A[k++]=R[j++];
    }
    }

    void MergeSort(int A[],int p,int r)
    {
    int q;

    if(p<r)
    {
    q=(p+r)/2;
    MergeSort(A,p,q);
    MergeSort(A,q+1,r);
    Merge(A,p,q,r);
    }

    for(int i=0; i<r+1; i++)
    {
    cout<<A[i]<<" ";
    }

    cout<<endl;

    }

    int main()
    {

    int A_Size;

    cout<>A_Size;

    int A[A_Size];

    cout<<"Enter the array elements : ";

    for(int i=0; i>A[i];
    }

    MergeSort(A,0,A_Size-1);
    }

  2. Id:17183103049
    include<stdio.h>

    void mergesort(int a[],int i,int j);
    void merge(int a[],int i1,int j1,int i2,int j2);

    int main()
    {
    int a[30],n,i;
    printf(“Enter no of elements:”);
    scanf(“%d”,&n);
    printf(“Enter array elements:”);

    for(i=0;i<n;i++)
    scanf(“%d”,&a[i]);

    mergesort(a,0,n1);

    printf(“nSorted array is :”);
    for(i=0;i<n;i++)
    printf(“%d “,a[i]);

    return 0;
    }

    void mergesort(int a[],int i,int j)
    {
    int mid;

    if(i<j)
    {
    mid=(i+j)/2;
    mergesort(a,i,mid);
    mergesort(a,mid+1,j);
    merge(a,i,mid,mid+1,j);
    }
    }

    void merge(int a[],int i1,int j1,int i2,int j2)
    {
    int temp[50];
    int i,j,k;
    i=i1;
    j=i2;
    k=0;

    while(i<=j1 && j<=j2)
    {
    if(a[i]<a[j])
    temp[k++]=a[i++];
    else
    temp[k++]=a[j++];
    }

    while(i<=j1)
    temp[k++]=a[i++];

    while(j<=j2)
    temp[k++]=a[j++];

    for(i=i1,j=0;i<=j2;i++,j++)
    a[i]=temp[j];
    }

  3. Id:17183103077

     

    #include<stdio.h>

    Void mergesort(int a[],int i,int j);

    Void merge(int a[], int i1,int j1,int i2,int j2);

    int main()

    {

    int a[100],n,i;

    printf(“Enter no of elements:”);

    Scanf(“%d”,&n);

    printf(“Enter array elements:”);

    for(i=0;i<n;i++)

    Scanf(“%d”,&a[i]);

    mergesort(a, 0,n-1);

    printf(“n sorted array is:”);

    for(i=0;i<n;i++)

    printf(“%d”,a[i]);

    return 0;

    }

    Void mergesort(int a[],int i,int j);

    {

    int mid;

    if(i<j)

    {

    mid=(i+j)/2;

    mergesort(a,i,mid);

    mergesort(a,mid+1,j);

    merge(a,i,mid,mid+1,j);

    }

    }

    Void merge(int a[],int i1,int j1,int i2,int j2);

    {

    int temp[100];

    int i,j,k;

    i=i1;

    j=i2;

    k=0;

    while(i<=j1  &&  j<=j2)

    {

    if(a[i]<a[j])

    temp[k++]=a[i++];

    else

    temp[k++]=a[j++];

    }

    while(i<=j1)

    temp[k++]=a[i++];

    while(j<=j2)

    temp[k++]=a[j++];

    for ( i=i1,j=0;i<=j2;i++,j++)

    a[i]=temp[j];

    }

     

     

     

  4. Id:17183103053

    #include<stdio.h>

    Void mergesort(int a[],int i,int j);

    Void merge(int a[], int i1,int j1,int i2,int j2);

    int main()

    {

    int a[100],n,i;

    printf(“Enter no of elements:”);

    Scanf(“%d”,&n);

    printf(“Enter array elements:”);

    for(i=0;i<n;i++)

    Scanf(“%d”,&a[i]);

    mergesort(a, 0,n-1);

    printf(“n sorted array is:”);

    for(i=0;i<n;i++)

    printf(“%d”,a[i]);

    return 0;

    }

    Void mergesort(int a[],int i,int j);

    {

    int mid;

    if(i<j)

    {

    mid=(i+j)/2;

    mergesort(a,i,mid);

    mergesort(a,mid+1,j);

    merge(a,i,mid,mid+1,j);

    }

    }

    Void merge(int a[],int i1,int j1,int i2,int j2);

    {

    int temp[100];

    int i,j,k;

    i=i1;

    j=i2;

    k=0;

    while(i<=j1 && j<=j2)

    {

    if(a[i]<a[j])

    temp[k++]=a[i++];

    else

    temp[k++]=a[j++];

    }

    while(i<=j1)

    temp[k++]=a[i++];

    while(j<=j2)

    temp[k++]=a[j++];

    for ( i=i1,j=0;i<=j2;i++,j++)

    a[i]=temp[j];

    }

  5. id- 17183103068

    #include<stdio.h>
    #include<stdlib.h>
    void merge(int arr[], int q, int p, int r)
    {
    int i, j, k;
    int n1 = p – q + 1;
    int n2 = r – p;
    int L[n1], R[n2];

    for (i = 0; i < n1; i++)
    L[i] = arr[q + i];
    for (j = 0; j < n2; j++)
    R[j] = arr[p + 1+ j];
    i = 0;
    j = 0;
    k = q;
    while (i < n1 && j < n2)
    {
    if (L[i] <= R[j])
    {
    arr[k] = L[i];
    i++;
    }
    else
    {
    arr[k] = R[j];
    j++;
    }
    k++;
    }

    while (i < n1)
    {
    arr[k] = L[i];
    i++;
    k++;
    }

    while (j < n2)
    {
    arr[k] = R[j];
    j++;
    k++;
    }
    }

    void mergeSort(int arr[], int q, int r)
    {
    if (q < r)
    {

    int p = q+(r-q)/2;

    mergeSort(arr, q, p);
    mergeSort(arr, p+1, r);

    merge(arr, q, p, r);
    }
    }

    void printArray(int arr[], int n)
    {
    int i;
    for (i=0; i < n; i++)
    printf(“%d “, arr[i]);
    printf(“n”);
    }

    int main()
    {
    int arr[100],n,i;
    printf(“enter number of element: “);
    scanf(“%d”,&n);
    printf(“enter elementsn”);
    for(i=0;i<n;i++)
    scanf(“%d”,&arr[i]);
    printf(“nGiven array is n”);
    printArray(arr,n);
    mergeSort(arr, 0, n – 1);
    printf(“nSorted array is n”);
    printArray(arr, n);
    return 0;
    }

  6. ID :17183103061

     

    #include<stdio.h>

    Void mergesort(int a[],int i,int j);

    Void merge(int a[], int i1,int j1,int i2,int j2);

    int main()

    {

    int a[100],n,i;

    printf(“Enter no of elements:”);

    Scanf(“%d”,&n);

    printf(“Enter array elements:”);

    for(i=0;i<n;i++)

    Scanf(“%d”,&a[i]);

    mergesort(a, 0,n-1);

    printf(“n sorted array is:”);

    for(i=0;i<n;i++)

    printf(“%d”,a[i]);

    return 0;

    }

    Void mergesort(int a[],int i,int j);

    {

    int mid;

    if(i<j)

    {

    mid=(i+j)/2;

    mergesort(a,i,mid);

    mergesort(a,mid+1,j);

    merge(a,i,mid,mid+1,j);

    }

    }

    Void merge(int a[],int i1,int j1,int i2,int j2);

    {

    int temp[100];

    int i,j,k;

    i=i1;

    j=i2;

    k=0;

    while(i<=j1  &&  j<=j2)

    {

    if(a[i]<a[j])

    temp[k++]=a[i++];

    else

    temp[k++]=a[j++];

    }

    while(i<=j1)

    temp[k++]=a[i++];

    while(j<=j2)

    temp[k++]=a[j++];

    for ( i=i1,j=0;i<=j2;i++,j++)

    a[i]=temp[j];

    }

     

  7. ID 17183103047

    #include <stdio.h>
    void merge_sort(int i, int j, int a[], int aux[]) {
    if (j <= i) {
    return;
    }
    int mid = (i + j) / 2;

    merge_sort(i, mid, a, aux);
    merge_sort(mid + 1, j, a, aux);

    int pointer_left = i;
    int pointer_right = mid + 1;
    int k;

    for (k = i; k <= j; k++) {
    if (pointer_left == mid + 1) {
    aux[k] = a[pointer_right];
    pointer_right++;
    } else if (pointer_right == j + 1) {
    aux[k] = a[pointer_left];
    pointer_left++;
    } else if (a[pointer_left] < a[pointer_right]) {
    aux[k] = a[pointer_left];
    pointer_left++;
    } else {
    aux[k] = a[pointer_right];
    pointer_right++;
    }
    }

    for (k = i; k <= j; k++) {
    a[k] = aux[k];
    }
    }

    int main() {
    int a[100], aux[100], n, i, d, swap;

    printf(“Enter number of elements in the array:n”);
    scanf(“%d”, &n);

    printf(“Enter %d integersn”, n);

    for (i = 0; i < n; i++)
    scanf(“%d”, &a[i]);

    merge_sort(0, n – 1, a, aux);

    printf(“Printing the sorted array:n”);

    for (i = 0; i < n; i++)
    printf(“%dn”, a[i]);

    return 0;
    }

  8. ID 17183103052

     

    #include <stdio.h>
    void merge_sort(int i, int j, int a[], int aux[]) {
    if (j <= i) {
    return;
    }
    int mid = (i + j) / 2;

    merge_sort(i, mid, a, aux);
    merge_sort(mid + 1, j, a, aux);

    int pointer_left = i;
    int pointer_right = mid + 1;
    int k;

    for (k = i; k <= j; k++) {
    if (pointer_left == mid + 1) {
    aux[k] = a[pointer_right];
    pointer_right++;
    } else if (pointer_right == j + 1) {
    aux[k] = a[pointer_left];
    pointer_left++;
    } else if (a[pointer_left] < a[pointer_right]) {
    aux[k] = a[pointer_left];
    pointer_left++;
    } else {
    aux[k] = a[pointer_right];
    pointer_right++;
    }
    }

    for (k = i; k <= j; k++) {
    a[k] = aux[k];
    }
    }

    int main() {
    int a[100], aux[100], n, i, d, swap;

    printf(“Enter number of elements in the array:n”);
    scanf(“%d”, &n);

    printf(“Enter %d integersn”, n);

    for (i = 0; i < n; i++)
    scanf(“%d”, &a[i]);

    merge_sort(0, n – 1, a, aux);

    printf(“Printing the sorted array:n”);

    for (i = 0; i < n; i++)
    printf(“%dn”, a[i]);

    return 0;
    }

  9. Id:17183103066

    #include <stdio.h>

    void merge_sort(int i, int j, int arr[], int subarr[])
    {
    if (j <= i)
    {
    return;
    }
    int mid = (i + j) / 2;

    merge_sort(i, mid, arr, subarr);
    merge_sort(mid + 1, j, arr, subarr);

    int left = i;
    int right = mid + 1;
    int k;

    for (k = i; k <= j; k++)
    {
    if (left == mid + 1)
    {
    subarr[k] = arr[right];
    right++;
    }
    else if (right == j + 1)
    {
    subarr[k] = arr[left];
    left++;
    }
    else if (arr[left] < arr[right])
    {
    subarr[k] = arr[left];
    left++;
    }
    else
    {
    subarr[k] = arr[right];
    right++;
    }
    }

    for (k = i; k <= j; k++)
    {
    arr[k] = subarr[k];
    }
    }

    int main()
    {
    int arr[100], subarr[100], n, i;

    printf(“Enter number of elements in the array:n”);
    scanf(“%d”, &n);

    printf(“Enter %d integersn”, n);

    for (i = 0; i < n; i++)
    scanf(“%d”, &arr[i]);

    merge_sort(0, n – 1, arr, subarr);

    printf(“Printing the sorted array:n”);

    for (i = 0; i < n; i++)
    printf(” %d “, arr[i]);

    return 0;
    }

  10. Sorry it a private answer.

  11. id: 17182103349

     

    #include<stdio.h>

    void mergesort(int a[],int i,int j);
    void merge(int a[],int i1,int j1,int i2,int j2);

    int main()
    {
    int a[70],n,i;
    printf(“Enter no of elements:”);
    scanf(“%d”,&n);
    printf(“Enter array elements:”);

    for(i=0;i<n;i++)
    scanf(“%d”,&a[i]);

    mergesort(a,0,n–1);

    printf(“nSorted array is :”);
    for(i=0;i<n;i++)
    printf(“%d “,a[i]);

    return 0;
    }

    void mergesort(int a[],int i,int j)
    {
    int mid;

    if(i<j)
    {
    mid=(i+j)/2;
    mergesort(a,i,mid);
    mergesort(a,mid+1,j);
    merge(a,i,mid,mid+1,j);
    }
    }

    void merge(int a[],int i1,int j1,int i2,int j2)
    {
    int temp[40];
    int i,j,k;
    i=i1;
    j=i2;
    k=0;

    while(i<=j1 && j<=j2)
    {
    if(a[i]<a[j])
    temp[k++]=a[i++];
    else
    temp[k++]=a[j++];
    }

    while(i<=j1)
    temp[k++]=a[i++];

    while(j<=j2)
    temp[k++]=a[j++];

    for(i=i1,j=0;i<=j2;i++,j++)
    a[i]=temp[j];
    }

  12. ID:17183103074

    #include <stdio.h>

    void merge_sort(int i, int j, int arr[], int subarr[])
    {
    if (j <= i)
    {
    return;
    }
    int mid = (i + j) / 2;

        merge_sort(i, mid, arr, subarr);
    merge_sort(mid + 1, j, arr, subarr);

        int left = i;
    int right = mid + 1;
    int k;


    for (k = i; k <= j; k++)
    {
    if (left == mid + 1)
    {
    subarr[k] = arr[right];
    right++;
    }
    else if (right == j + 1)
    {
    subarr[k] = arr[left];
    left++;
    }
    else if (arr[left] < arr[right])
    {
    subarr[k] = arr[left];
    left++;
    }
    else
    {
    subarr[k] = arr[right];
    right++;
    }
    }

        for (k = i; k <= j; k++)
    {
    arr[k] = subarr[k];
    }
    }


    int main()
    {
    int arr[100], subarr[100], n, i;

        printf(“Enter number of elements in the array:n”);
    scanf(“%d”, &n);

        printf(“Enter %d integersn”, n);
        for (i = 0; i < n; i++)
    scanf(“%d”, &arr[i]);

        merge_sort(0, n – 1, arr, subarr);
        printf(“Printing the sorted array:n”);
        for (i = 0; i < n; i++)
    printf(” %d “, arr[i]);

        return 0;
    }

     

  13. ID:17183103070

     

    #include<stdio.h>

    Void mergesort(int a[],int i,int j);

    Void merge(int a[], int i1,int j1,int i2,int j2);

    int main()

    {

    int a[100],n,i;

    printf(“Enter no of elements:”);

    Scanf(“%d”,&n);

    printf(“Enter array elements:”);

    for(i=0;i<n;i++)

    Scanf(“%d”,&a[i]);

    mergesort(a, 0,n-1);

    printf(“n sorted array is:”);

    for(i=0;i<n;i++)

    printf(“%d”,a[i]);

    return 0;

    }

    Void mergesort(int a[],int i,int j);

    {

    int mid;

    if(i<j)

    {

    mid=(i+j)/2;

    mergesort(a,i,mid);

    mergesort(a,mid+1,j);

    merge(a,i,mid,mid+1,j);

    }

    }

    Void merge(int a[],int i1,int j1,int i2,int j2);

    {

    int temp[100];

    int i,j,k;

    i=i1;

    j=i2;

    k=0;

    while(i<=j1  &&  j<=j2)

    {

    if(a[i]<a[j])

    temp[k++]=a[i++];

    else

    temp[k++]=a[j++];

    }

    while(i<=j1)

    temp[k++]=a[i++];

    while(j<=j2)

    temp[k++]=a[j++];

    for ( i=i1,j=0;i<=j2;i++,j++)

    a[i]=temp[j];

    }

  14. iD:17183103063

    #include

    void merge_sort(int i, int j, int arr[], int subarr[])
    {
    if (j <= i)
    {
    return;
    }
    int mid = (i + j) / 2;

    merge_sort(i, mid, arr, subarr);
    merge_sort(mid + 1, j, arr, subarr);

    int left = i;
    int right = mid + 1;
    int k;

    for (k = i; k <= j; k++)
    {
    if (left == mid + 1)
    {
    subarr[k] = arr[right];
    right++;
    }
    else if (right == j + 1)
    {
    subarr[k] = arr[left];
    left++;
    }
    else if (arr[left] < arr[right])
    {
    subarr[k] = arr[left];
    left++;
    }
    else
    {
    subarr[k] = arr[right];
    right++;
    }
    }

    for (k = i; k <= j; k++)
    {
    arr[k] = subarr[k];
    }
    }

    int main()
    {
    int arr[100], subarr[100], n, i;

    printf("Enter number of elements in the array:n");
    scanf("%d", &n);

    printf("Enter %d integersn", n);

    for (i = 0; i < n; i++)
    scanf("%d", &arr[i]);

    merge_sort(0, n – 1, arr, subarr);

    printf("Printing the sorted array:n");

    for (i = 0; i < n; i++)
    printf(" %d ", arr[i]);

    return 0;
    }

  15. ID:17183103062

    #include

    void merge_sort(int i, int j, int arr[], int subarr[])
    {
    if (j <= i)
    {
    return;
    }
    int mid = (i + j) / 2;

    merge_sort(i, mid, arr, subarr);
    merge_sort(mid + 1, j, arr, subarr);

    int left = i;
    int right = mid + 1;
    int k;

    for (k = i; k <= j; k++)
    {
    if (left == mid + 1)
    {
    subarr[k] = arr[right];
    right++;
    }
    else if (right == j + 1)
    {
    subarr[k] = arr[left];
    left++;
    }
    else if (arr[left] < arr[right])
    {
    subarr[k] = arr[left];
    left++;
    }
    else
    {
    subarr[k] = arr[right];
    right++;
    }
    }

    for (k = i; k <= j; k++)
    {
    arr[k] = subarr[k];
    }
    }

    int main()
    {
    int arr[100], subarr[100], n, i;

    printf("Enter number of elements in the array:n");
    scanf("%d", &n);

    printf("Enter %d integersn", n);

    for (i = 0; i < n; i++)
    scanf("%d", &arr[i]);

    merge_sort(0, n – 1, arr, subarr);

    printf("Printing the sorted array:n");

    for (i = 0; i < n; i++)
    printf(" %d ", arr[i]);

    return 0;
    }

  16. ID: 17182103110

     

    #include<stdio.h>

    Void mergesort(int a[],int i,int j);

    Void merge(int a[], int i1,int j1,int i2,int j2);

    int main()

    {

    int a[100],n,i;

    printf(“Enter no of elements:”);

    Scanf(“%d”,&n);

    printf(“Enter array elements:”);

    for(i=0;i<n;i++)

    Scanf(“%d”,&a[i]);

    mergesort(a, 0,n-1);

    printf(“n sorted array is:”);

    for(i=0;i<n;i++)

    printf(“%d”,a[i]);

    return 0;

    }

    Void mergesort(int a[],int i,int j);

    {

    int mid;

    if(i<j)

    {

    mid=(i+j)/2;

    mergesort(a,i,mid);

    mergesort(a,mid+1,j);

    merge(a,i,mid,mid+1,j);

    }

    }

    Void merge(int a[],int i1,int j1,int i2,int j2);

    {

    int temp[100];

    int i,j,k;

    i=i1;

    j=i2;

    k=0;

    while(i<=j1 && j<=j2)

    {

    if(a[i]<a[j])

    temp[k++]=a[i++];

    else

    temp[k++]=a[j++];

    }

    while(i<=j1)

    temp[k++]=a[i++];

    while(j<=j2)

    temp[k++]=a[j++];

    for ( i=i1,j=0;i<=j2;i++,j++)

    a[i]=temp[j];

    }

Leave an answer