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

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...

The Future of Artificial Intelligence: Trends to Watch in 2024

     The Future of Artificial Intelligence: Trends to Watch in 2024 Introduction: Artificial Intelligence (AI) is developing rapidly, impacting various industries and changing the way we live and work. As we look ahead to 2024, several emerging trends in AI are expected to have a major impact. Here are the top AI trends to watch this year. 1. AI in Healthcare: The role of AI in healthcare is growing with advances in predictive analytics, personalized medicine, and diagnostic tools. We can expect to see more AI-driven solutions that can predict patient outcomes, recommend treatments, and even assist with surgery with greater precision. 2. Ethical AI and Regulation: As the influence of AI grows, ethical considerations and regulation are becoming increasingly important. 2024 could see more robust frameworks and policies established to ensure AI is used responsibly and address issues such as bias, privacy, and transparency. 3. AI and Cybersecurity: As cyber threats become mor...

"How to Choose the Perfect Earphones for Your Needs"

 "How to Choose the Perfect Earphones for    Your Needs" Earphones are an essential accessory for many people, whether it's for listening to music, watching videos, or making phone calls. With so many different options available, it can be challenging to choose the right pair of earphones for your needs. In this blog post, we'll discuss some factors to consider when selecting the perfect earphones for you. Sound Quality Sound quality is the most important factor to consider when choosing earphones. The quality of sound depends on the type of earphone you choose. In-ear earphones provide better sound quality and noise isolation than on-ear earphones. You should also look for earphones with a wide frequency response range for a better listening experience. Comfort Comfort is also a crucial factor when selecting earphones. Choose earphones with soft ear tips that fit your ears well to avoid discomfort during extended use. In-ear earphones with multiple ear ...