Search
 
SCRIPT & CODE EXAMPLE
 

C

merge sort for strings in c

#include<stdio.h>
#include<stdlib.h>
#include<string.h> //To use the string functions like strcmp and strcpy

#define MAX 10  // This is the default size of every string 

void Merge(char* arr[],int low,int mid,int high) //Merging the Array Function
{
    int nL= mid-low+1;
    int nR= high-mid;

    char** L=malloc(sizeof(char *)*nL);
    char** R=malloc(sizeof(char *)*nR);
    int i;
    for(i=0;i<nL;i++)
    {
        L[i]=malloc(sizeof(arr[low+i]));
        strcpy(L[i],arr[low+i]);
    }
    for(i=0;i<nR;i++)
    {
        R[i]=malloc(sizeof(arr[mid+i+1]));
        strcpy(R[i],arr[mid+i+1]);
    }
    int j=0,k;
    i=0;
    k=low;
    while(i<nL&&j<nR)
    {
        if(strcmp(L[i],R[j])<0)strcpy(arr[k++],L[i++]);
        else strcpy(arr[k++],R[j++]);
    }
    while(i<nL)strcpy(arr[k++],L[i++]);
    while(j<nR)strcpy(arr[k++],R[j++]);

}


void MergeSort(char* arr[],int low,int high) //Main MergeSort function
{
    if(low<high)
    {
        int mid=(low+high)/2;
        MergeSort(arr,low,mid);
        MergeSort(arr,mid+1,high);
        Merge(arr,low,mid,high);
    }
}


int main()
{
    printf("
Enter the size of the array desired: ");
    int size;  //This is the String array size
    scanf("%d",&size);

    char** arr= malloc(sizeof(char *)* size); //Creating required string array
    printf("
Enter the strings of the array: ");

    int i;
    for(i=0;i<size;i++)
    {
        arr[i]=malloc(sizeof(char)*MAX);
        printf("
Enter String: ");
        scanf("%s",arr[i]);
    }
    MergeSort(arr,0,size-1);
    printf("
The Sorted Array is: ");
    for(i=0;i<size;i++)printf("%s ->",arr[i]);
    return 0;

}
Comment

PREVIOUS NEXT
Code Example
C :: print short in c 
C :: int_min in c 
C :: how to reverse a string in c 
C :: toupper function in c 
C :: c float 
C :: initialize array c 
C :: count distinct characters in a string C 
C :: initializa 2d array c 
C :: C Arithmetic Operators 
C :: Bitwise Operators in C/C++ 
C :: to run Blazor project using CLI 
C :: round float in c 
C :: arrays in c 
C :: how to call function after some time in vue.js 
C :: c bubble sort 
C :: number pattern in c 
C :: how to take comma separated integer input in c 
C :: c print characters 
C :: absolute value of intel intrinsic 
C :: how to free memory in c 
C :: c programming exercises 
C :: array of strings c 
C :: find sum of all odd numbers from 1 to n using for loop 
C :: realloc in c 
C :: marquee html code with right 
C :: sOY wapo ya lo c 
C :: Answer to storing information in array 
C :: how to find folders from a c program 
C :: c timespec 
C :: ESP32 timerBegin(0, cpuClock, true); 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =