Skip to main content

How to Write Merge Sort in C

               How to Write Merge Sort in C

#include<stdio.h>
/// Merge Sort
void merge(int arr[],int l,int m,int r);
void merge_sort(int arr[],int l,int r);
void print_arr(int arr[],int size);
void merge_sort(int arr[],int l,int r){
    if(l<r){
        int m=(l+r)/2;
        merge_sort(arr,l,m);
        merge_sort(arr,m+1,r);
        merge(arr,l,m,r);
    }
}
void merge(int arr[],int l,int m,int r){
    int i,j,k;
    int n1=m-l+1;
    int n2=r-m;
    int L[n1];
    int R[n2];
    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];
    }
    i=0;
    j=0;
    k=l;
    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 print_arr(int arr[],int size){
    for(int i=0;i<size;i++){
        printf("%d ",arr[i]);
    }
    printf("\n");

}
int main(){

   int arr[]={11,9,7,2,5,15};
   int size_arr=sizeof(arr)/sizeof(arr[0]);
   printf("Before Array: ");
   print_arr(arr,size_arr);
   merge_sort(arr,0,size_arr-1);
   printf("After Sorted Array; ");
   print_arr(arr,size_arr);


    return 0;
}

Comments

Popular posts from this blog

The Future of Artificial Intelligence in 2024: Trends and Predictions

Introduction: Artificial Intelligence (AI) has been transforming industries for years, and 2024 promises even more groundbreaking advancements. With AI becoming an integral part of everyday life, it's important to stay ahead of the curve to understand how this technology will evolve. From advancements in machine learning to breakthroughs in natural language processing, let’s take a look at the top trends and predictions for AI in 2024. 1. AI in Healthcare: Revolutionizing Diagnostics and Treatment In 2024, AI is poised to revolutionize the healthcare industry by improving diagnostics, treatment plans, and patient care. With machine learning algorithms being trained on large sets of medical data, AI tools are becoming increasingly proficient at diagnosing diseases, predicting outbreaks, and personalizing treatment plans. From detecting cancer at earlier stages to enabling robotic surgeries, AI will continue to save lives and reduce healthcare costs. 2. The Rise of Autonomous Syste...

C program to count total number of notes in given amount

 #include <iostream> using namespace std; int main() {     // 500,100,50,20,10,5,2,1     int n;     int count1,count2,count3,count4,count5,count6,count7,count8;     count1=count2=count3=count4=count5=count6=count7=count8=0;     cin>>n;         if(n>=500){           count1=n/500;           n=n%500;         }         if(n>=100){             count2=n/100;             n=n%100;         }         if(n>=50){             count3=n/50;       ...

How Augmented Reality is Changing the Gaming Industry

How Augmented Reality is Changing the Gaming Industry Introduction: Augmented Reality (AR) is taking the gaming industry by storm, offering immersive experiences that blend the virtual and real worlds. In this post, we explore how AR is changing gaming. We also include insightful YouTube videos showcasing some of the most groundbreaking AR games and technologies. The Rise of AR in Gaming: AR technology overlays the real world with digital content, creating unique interactive experiences. Unlike Virtual Reality (VR), where players are fully immersed in a digital environment, AR augments the real world by adding virtual elements. This has created new opportunities for both game developers and players. Key innovations in AR gaming: Mobile AR gaming: Mobile devices have become a powerful AR platform. Games like Pokémon GO and Harry Potter: Wizards Unite show how AR can transform the real world into a gaming playground. Players can explore their surroundings while interacting with virtual c...