Sunday, August 20, 2023

Merge Sort Algorithm

 #include<stdio.h>

#include<stdlib.h>


int A[50], n;


// CheckFor:  Create new Array 

void CreateArray(){

     

     // CheckFor: Declaration of local variable

     int i = 0;

          

     // LoopFor: Accepet the array elements using random function

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

          A[i] = rand(); 

}


// CheckFor:  Display the Array

void DisplayArray(){


     // CheckFor: Declaration of local variable     

     int i = 0;

     

     // Checkfor: Display the array elements

     printf("The array elements are: \t");

     

     // LoopFor: Display the array elements

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

          printf("%d \t", A[i]); 

}


// CheckFor: Sort the Array Element using Merge Sort Algorithm

//           Combine the all element after division

void MergeSort(int low, int mid, int high)

{

     // CheckFor: Declaration of local variable

     int t[50], i, j, k;

     

     // CheckFor: Initialise the low, mid variable

     i = low;

     

     j = mid+1;

     

     k = low;

     

     // LoopFor: Move i from 0 to mid and j from mid+1 to high

     while ( (i <= mid) && (j <= high))

     {

          // CheckFor: If First half element is greater than second half element

          //           and copy the element into temporary array 

          if (A[i] >= A[j])

             t[k++] = A[j++];

          else

               t[k++] = A[i++];        

     }

     

     

     // LoopFor: Check remaining element of first half array

     while( i <= mid)

          t[k++] = A[i++];

     

     // LoopFor: Check remaining element of second half array           

     while( j <= high)

          t[k++] = A[j++];

     

     // LoopFor: Copy the all sorted element from temporary array to original array     

     for(i=1; i <= high; i++)

        A[i] = t[i];

}


// CheckFor: Sort the Array Element using Merge Sort Algorithm

//           Divide the array recursively on the basis of divide and conquer

void MergeSortDivide(int low, int high)

{

     // CheckFor: Declaration of local variable

     int mid = 0;

     

     // CheckFor: Empty array element

     if (low != high)

     {

             // CheckFor: Find the mid element of the array

             mid = ((low + high)/ 2);

             // CheckFor: Divide the array from low to mid

             MergeSortDivide(low, mid);

             // CheckFor: Divide the array from mid+1 to high

             MergeSortDivide(mid+1, high);

             // CheckFor: Combine and sort the array.

             MergeSort(low, mid, high);

     }

}                 

 

int main(){

    

    printf("How many element we want to enter into Array: ");

    scanf("%d", &n);

    

    CreateArray();

    DisplayArray();

    

    MergeSortDivide(0, n-1);

    printf("\n");

    DisplayArray();

    printf("\n");

    system("pause");

    

    return 0;

}


No comments:

Post a Comment

SYBSc (CS) Sem III : Data Structure Slip 20 Que - 2

    SAVITIBAI PHULE UNIVERSITY OF PUNE S. Y. B.Sc. (Computer Science) Semester III Practical Examination       SUBJECT: CS-233 Practical cou...