"merge sort in c" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "merge sort in c" answered properly. Developers are finding an appropriate answer about merge sort in c related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like merge sort in c. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on merge sort in c. 

Merge sort in c++

By VeNOMVeNOM on Dec 08, 2020
#include <iostream>
using namespace std;
 

void merge(int arr[], int l, int m, int r)
{
    int n1 = m - l + 1;
    int n2 = r - m;
 
 
    int L[n1], 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];

 
    int i = 0;
 
    
    int j = 0;
 
    
    int 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 mergeSort(int arr[],int l,int r){
    if(l>=r){
        return;
    }
    int m = (l+r-1)/2;
    mergeSort(arr,l,m);
    mergeSort(arr,m+1,r);
    merge(arr,l,m,r);
}
 

void printArray(int A[], int size)
{
    for (int i = 0; i < size; i++)
        cout << A[i] << " ";
}
 

int main()
{
    int arr[] = { 12, 11, 13, 5, 6, 7 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Given array is \n";
    printArray(arr, arr_size);
 
    mergeSort(arr, 0, arr_size - 1);
 
    cout << "\nSorted array is \n";
    printArray(arr, arr_size);
    return 0;
}

Add Comment

2

sorting array in c

By crispen_devcrispen_dev on Sep 19, 2020
 #include <stdio.h>
    void main()
    {
 
        int i, j, a, n, number[30];
        printf("Enter the value of N \n");
        scanf("%d", &n);
 
        printf("Enter the numbers \n");
        for (i = 0; i < n; ++i)
            scanf("%d", &number[i]);
 
        for (i = 0; i < n; ++i) 
        {
 
            for (j = i + 1; j < n; ++j)
            {
 
                if (number[i] > number[j]) 
                {
 
                    a =  number[i];
                    number[i] = number[j];
                    number[j] = a;
 
                }
 
            }
 
        }
 
        printf("The numbers arranged in ascending order are given below \n");
        for (i = 0; i < n; ++i)
            printf("%d\n", number[i]);
 }

Add Comment

0

merge sort

By adriancmirandaadriancmiranda on May 31, 2020
// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

function merge(list, start, midpoint, end) {
    const left = list.slice(start, midpoint);
    const right = list.slice(midpoint, end);
    for (let topLeft = 0, topRight = 0, i = start; i < end; i += 1) {
        if (topLeft >= left.length) {
            list[i] = right[topRight++];
        } else if (topRight >= right.length) {
            list[i] = left[topLeft++];
        } else if (left[topLeft] < right[topRight]) {
            list[i] = left[topLeft++];
        } else {
            list[i] = right[topRight++];
        }
    }
}

function mergesort(list, start = 0, end = undefined) {
    if (end === undefined) {
        end = list.length;
    }
    if (end - start > 1) {
        const midpoint = ((end + start) / 2) >> 0;
        mergesort(list, start, midpoint);
        mergesort(list, midpoint, end);
        merge(list, start, midpoint, end);
    }
    return list;
}

mergesort([4, 7, 2, 6, 4, 1, 8, 3]);

Add Comment

2

All those coders who are working on the C++ based application and are stuck on merge sort in c can get a collection of related answers to their query. Programmers need to enter their query on merge sort in c related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about merge sort in c for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of merge sort in c while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "merge sort in c". Visit this developer's friendly online web community, CodeProZone, and get your queries like merge sort in c resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "merge sort in c"

View All C++ queries

C++ queries related to "merge sort in c"

merge sort merge sort in c++ merge sort code in c++ merge sort c++ vector merge sort c++ github merge sort in c sort char array c++ using insertion sort Write a program to sort an array 100,200,20, 75,89.198, 345,56,34,35 using Bubble Sort. The program should be able to display total number of passes used for sorted data in given data set. sort char array c++ using insertion sort descending order how to merge string array in C++ merge images opencv c++ merge vector c++ how to sort a vector in reverse c++ how to sort an array c++ how to sort in descending order c++ how to sort a vector in c++ vector sort in reverse order c++ sort in descending order c++ stl how to sort a string in c++ sort a string alphabetically c++ bucket sort algorithm c++ simple -vector reverse sort cpp bubble sort in c++ c++ how to sort numbers in ascending order binary sort c++ how to sort vector in c++ c++ sort function time complexity sort vector struct c++ how to sort an array in c++ c++ sort array of ints define my own compare function sort C++ stl how to sort in descending order in c++ sort vector descending sort a vector of strings according to their length c++ sort string vector of words alphabetically c++ . Shell sort in c++ vector sort c++ The number of swaps required in selection sort stl sort in c++ how to make a selection sort C++ sort vector in descending order c++ sort std vector sort what is time complexity of insertion sort Heap sort in c++ array sort c++ insertion sort in c++ program sort function in cpp sort vector c++ quick sort in c++ how to sort array in c++ bubble sort c++ template Radix Sort in c++ quick sort predefined function in c++ c++ set sort order code for bubble sort in c++ c++ sort topological sort cp algorithms sort inbuilt function in c++ sort vector of strings c++ stl sort insertion sort in c++ sort a vector c++ sort vector of pairs c++ heap sort heapify and max heap in binary tree sort tuple c++ turbo sort codechef solution c++ buble sort sort strings by length and by alphabet sort n characters in descending order c++ sort using comparator anonymous function c++ how to sort string containing numbers in c++ Sort by the distance between pairs c++ c++ bubble sort heap sort internal implementation using c++ extra parameter in comparator function for sort write a c++ program that reads ten strings and store them in array of strings, sort them and finally print the sorted strings sort using lambda c++ sort vector in descending order c++ sort in descending order c++ how to sort a vector bubble sort program in c++ sort in c++ sort c++ c++ sort vector of objects by property mergge sort c++ sort function sort vector topological sort Bubble Sort C++ c++ sort vector of objects by property.

Browse Other Code Languages

CodeProZone