Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ merge sort

#include<bits/stdc++.h>
using namespace std;
void merge(int a[],int low,int mid,int high)
{
    int b[10] ={0};
    int k=0;
    int i=low,j=mid+1;
    
    while(i<=mid&&j<=high)
    {
        if(a[i]<=a[j])
        {
            b[k++] = a[i];
            i++;
        }
        else{
            b[k++]= a[j];
            j++;
        }
    }
    while(i<=mid)
    {
        b[k++] = a[i++];
    }
    while(j<=high)
    {
        b[k++] = a[j++];
        
    }
    int p= low;
    for(int l = 0;l<k;l++)
    {
        a[p++] = b[l];
    }
    
    
}
void mergesort(int a[],int low,int high)
{
    if(high<=low)
    return ;
    int mid;
    mid= (low + (high-low)/2);
    
    mergesort(a,low,mid);
    mergesort(a,mid+1,high);
    merge(a,low,mid,high);
}
int main()
{
    //merge sort;
    int a[10] = {10,80,12,63,1,2,45,95,68,11};
    mergesort(a,0,9);
    for(int i=0;i<10;i++)
{    cout<<a[i]<<" ";}
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: count word accurances in a string c++ 
Cpp :: convert int to binary string c++ 
Cpp :: convert int to enum c++ 
Cpp :: c++ extend class 
Cpp :: c++ measure time in microseconds 
Cpp :: copy array c++ 
Cpp :: prints out the elements in the array c++ 
Cpp :: how to read and parse a json file with rapidjson 
Cpp :: http.begin not working 
Cpp :: random number cpp 
Cpp :: change to lowercase character c++ 
Cpp :: how to make calculaor in c++ 
Cpp :: iteraate through a vector 
Cpp :: c++ string contains 
Cpp :: binary file in c++ 
Cpp :: vector fin element c++ 
Cpp :: cpp Sieve algorithm 
Cpp :: How to pause a c++ program. 
Cpp :: splice string in c++ 
Cpp :: find max element in array c++ 
Cpp :: check if char in string c++ 
Cpp :: continue c++ 
Cpp :: c++ split string by several space 
Cpp :: c++ pi float 
Cpp :: C++ Area and Perimeter of a Rectangle 
Cpp :: overload of << c++ 
Cpp :: concatenate two vectors c++ 
Cpp :: find kth max and min element in an array 
Cpp :: ++i and i++ 
Cpp :: c detect os 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =