merge sort c

Solutions on MaxInterview for merge sort c by the best coders in the world

showing results for - "merge sort c"
Opal
08 Nov 2017
1#include <stdio.h>
2
3void merge(int arr[], int start, int mid, int end) {
4
5  int len1 = mid - start + 1;
6  int len2 = end - mid;
7
8  int leftArr[len1], rightArr[len2];
9
10  for (int i = 0; i < len1; i++)
11    leftArr[i] = arr[start + i];
12  for (int j = 0; j < len2; j++)
13    rightArr[j] = arr[mid + 1 + j];
14
15  int i, j, k;
16  i = 0;
17  j = 0;
18  k = start;
19
20  while (i < len1 && j < len2) {
21    if (leftArr[i] <= rightArr[j]) {
22      arr[k] = leftArr[i];
23      i++;
24    } else {
25      arr[k] = rightArr[j];
26      j++;
27    }
28    k++;
29  }
30
31  while (i < len1) {
32    arr[k] = leftArr[i];
33    i++;
34    k++;
35  }
36
37  while (j < len2) {
38    arr[k] = rightArr[j];
39    j++;
40    k++;
41  }
42}
43
44void mergeSort(int arr[], int start, int end) {
45  if (start < end) {
46
47    int mid = start + (end - start) / 2;
48    mergeSort(arr, start, mid);
49    mergeSort(arr, mid + 1, end);
50    merge(arr, start, mid, end);
51  }
52}
53
54void display(int arr[], int size) {
55  for (int i = 0; i < size; i++)
56    printf("%d ", arr[i]);
57  printf("\n");
58}
59
60int main() {
61  int arr[] = {6, 5, 12, 10, 9, 1};
62  int size = sizeof(arr) / sizeof(arr[0]);
63  
64  printf("Original array\n");
65  display(arr, size);
66  
67  mergeSort(arr, 0, size - 1);
68
69  printf("Sorted array\n");
70  display(arr, size);
71}
72
Samuel
25 Nov 2017
1// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
2// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
3
4function merge(list, start, midpoint, end) {
5    const left = list.slice(start, midpoint);
6    const right = list.slice(midpoint, end);
7    for (let topLeft = 0, topRight = 0, i = start; i < end; i += 1) {
8        if (topLeft >= left.length) {
9            list[i] = right[topRight++];
10        } else if (topRight >= right.length) {
11            list[i] = left[topLeft++];
12        } else if (left[topLeft] < right[topRight]) {
13            list[i] = left[topLeft++];
14        } else {
15            list[i] = right[topRight++];
16        }
17    }
18}
19
20function mergesort(list, start = 0, end = undefined) {
21    if (end === undefined) {
22        end = list.length;
23    }
24    if (end - start > 1) {
25        const midpoint = ((end + start) / 2) >> 0;
26        mergesort(list, start, midpoint);
27        mergesort(list, midpoint, end);
28        merge(list, start, midpoint, end);
29    }
30    return list;
31}
32
33mergesort([4, 7, 2, 6, 4, 1, 8, 3]);
Kasper
15 May 2018
1def mergeSort(arr): 
2    if len(arr) >1: 
3        mid = len(arr)//2 # Finding the mid of the array 
4        L = arr[:mid] # Dividing the array elements  
5        R = arr[mid:] # into 2 halves 
6  
7        mergeSort(L) # Sorting the first half 
8        mergeSort(R) # Sorting the second half 
9  
10        i = j = k = 0
11          
12        # Copy data to temp arrays L[] and R[] 
13        while i < len(L) and j < len(R): 
14            if L[i] < R[j]: 
15                arr[k] = L[i] 
16                i+= 1
17            else: 
18                arr[k] = R[j] 
19                j+= 1
20            k+= 1
21          
22        # Checking if any element was left 
23        while i < len(L): 
24            arr[k] = L[i] 
25            i+= 1
26            k+= 1
27          
28        while j < len(R): 
29            arr[k] = R[j] 
30            j+= 1
31            k+= 1
32  
33# Code to print the list 
34def printList(arr): 
35    for i in range(len(arr)):         
36        print(arr[i], end =" ") 
37    print() 
38  
39# driver code to test the above code 
40if __name__ == '__main__': 
41    arr = [12, 11, 13, 5, 6, 7]  
42    print ("Given array is", end ="\n")  
43    printList(arr) 
44    mergeSort(arr) 
45    print("Sorted array is: ", end ="\n") 
46    printList(arr)
Elliot
16 Apr 2020
1#include "tools.hpp"
2/*   >>>>>>>> (Recursive function that sorts a sequence of) <<<<<<<<<<<< 
3     >>>>>>>> (numbers in ascending order using the merge function) <<<<                                 */
4std::vector<int> sort(size_t start, size_t length, const std::vector<int>& vec)
5{
6	if(vec.size()==0 ||vec.size() == 1)
7	return vec;
8
9	vector<int> left,right; //===>  creating left and right vectors 
10
11	size_t mid_point = vec.size()/2; //===>   midle point between the left vector and the right vector 
12
13	for(int i = 0 ; i < mid_point; ++i){left.emplace_back(vec[i]);} //===>  left vector 
14	for(int j = mid_point; j < length; ++j){ right.emplace_back(vec[j]);} //===>  right vector 
15
16	left = sort(start,mid_point,left); //===>  sorting the left vector 
17	right = sort(mid_point,length-mid_point,right);//===>  sorting the right vector 
18	
19
20	return merge(left,right); //===>   all the function merge to merge between the left and the right
21}
22/*
23
24>>>>> (function that merges two sorted vectors of numberss) <<<<<<<<<                                    */ 
25vector<int> merge(const vector<int>& a, const vector<int>& b)
26{
27	vector<int> merged_a_b(a.size()+b.size(),0); // temp vector that includes both left and right vectors
28	int i = 0;
29	int j = 0;
30	int k = 0;
31	int left_size = a.size();
32	int right_size = b.size();
33	while(i<left_size && j<right_size) 
34	{
35		if(a[i]<b[j])
36		{
37			merged_a_b[k]=a[i];
38			i++;
39		}
40		else
41		{
42			merged_a_b[k]=b[j];
43			j++;
44		}
45		k++;
46	}
47	while(i<left_size)
48	{
49		merged_a_b[k]=a[i];
50		i++;
51		k++;
52	}
53	while(j<right_size)
54	{
55		merged_a_b[k]=b[j];
56		j++;
57		k++;
58	}
59	
60	return merged_a_b;
61
62}
queries leading to this page
merged sortmerge sort wikimerge sort un c 2b 2bmerge sort algorithm defmerge sort python complexitymerge sort in c 2b 2b programmerge sort c programmingmerge sort c 2b 2b one arrayexplain the concept of merge sort on the following data to sort the list 3a 27 2c72 2c 63 2c 42 2c 36 2c 18 2c 29 what is the best case and worst case time complexity of merge sort algorithm 3fmerge sortemerge sort merge sort a vector c 2b 2bmerge sort cmerge sort algorothmmerge sort nedirmerge sort tutorialmerge sort recursionmerge sort function c 2b 2balgoritm merge sort c 2b 2bmerge sort algorithm in placemerge method for merge sortmerging sortingalgoritmi merge sortmerge sort algorithm merge sort algorithm cppmerge sort algorithmmerge sort source codemerge sort algormax and min using merge sortmerge sort in c 2b 2b with proper formaterge sort codehow to create merge sort in cmergesort function source code stlexplain merge sortmerge sort recursive javamerge sort explanationmerge sort examplemerge sort ascending c 2b 2bmerge sort algorythmmerge soring c 2b 2bmerge sort c 2b 2bfull implementation merge sort c 2b 2bc 2b 2b merge sortingmerge sort by divide and conquerfunction mergesort 28nums 29 7b 2f 2f write merge sort code here merge function merge sort c 2b 2bmerge sorting code in c 2b 2bmerge sprt in merge sort 2c you createmerge sort javaalgorithm of mergesortspace complexity of merge sorttechnique used in merge sortunderstanding merge sortmerge sort theorymerge sort examplesmerge sort two arrays cppmerge sort in pythonwhat is the recursive call of the merge sort in data structurec 2b 2b merge sort using comparatorpython merge sortmerge sort gfg solutionmerge sort program in c simplemergesort pythonto sort an array of integers using recursive merge sort algorithm c 2b 2b merge sort two vectorsis merge sort in ordermerge sort logic in cmerge sort method in javamerge sort in c using arrayhow to perform a merge sortmerge sort algorithm pseudocodemerge function in merge sortmerge sort complexitymerge sort algotrithmmerge sort is also called asimplements the merge sort for any data typeworking merge sort algo examp 2cemerge sort in cimplement following merge sort algorithm using recursion print passes of merging algorithm contoh mergesort c 2b 2bstd merge sort c 2b 2ba recursive function that sorts a sequence of numbers in ascending order using the merge function c 2b 2bmerge sort c 2b 2b with vectordetermine the appropriate sorting algorithm corresponding to the below function 3a function 28m 2cn 29 7bif 28m 3cn 29 7bmid 3d 28m 2bn 29 2f2 3b sort 28m 2cmiddle 29 3b sort 28middle 2b1 2cn 29 3b sort 28m 2cmiddle 2cn 29 3b 7d 7d 2amerge sort big omerge sort javacode for merge sortmerge sort algomergesort codemergesort in c 2b 2bmerge sortmergesort cppmerge sort geeks for geekshow does a merge sort workmerge and mergesort function github c 2b 2bmerge sort code c 2b 2bmerge sorting c 2b 2bmerge sort c algorithmmerge sortzmerge sort implementation example2 way merge sort code in cmerge sort algorithm c 2b 2b array 2cmerge sort c merge sort program in cwhat is merge sort 3fmergesort wikimerge sort in cpp codemerge sort in c programmerge sort recursive program in c 2b 2bmerge sort speduocodemerge sort algo gfguse of merge sortalgorithm merge sortmergesort implementation javamerge sort more efficent waytwo way merge sort in crecursive merge sort cppmerge sort pythonmerge sort source code in cmerge sort using cmerge sort algorithm c 2b 2bprogram to sort an array using merge sortpseudocode merge sort c 2b 2bc 2b 2b merge sort recursive merge sorrtmerge sort baeldungmerge sort in cppmerge sort programmarge sort in cimport merge sort in c 2b 2bmerge sortyc 2b 2b merge sort functionmerge algorith merge sortmerge sort algorithmmerge sort descending order c 2b 2bmergesort c 2b 2bmerge sort pseduodmergesort complexityhow to import merge sort to c 2b 2bmerge sort to sort arraypseudocode for merge sortmerge sort c 2b 2b programmerge sort c 2b 2b 5cpseudo code for merge sortmerge sort psuedocodeif a merge sortt is divided into 5 parts recurrnce timemerge sort code in cmerge and sort algorithmmerge sort in c 2b 2b stlmerge sort using recursion cppmerge sort cpp codemerge sort alogrythmmerge sort algotithmmergesort function in c 2b 2bmerge sort in c 2b 2bwhat is merge sort used forwhat is a merge sortstl has merge sort algorithm c 2b 2bgeeksforgeeks merge sortmerge sort in javamergesort recursionmerge sort algoritmomerge sort 5cmerge sort expressed mathematicallymerge sort algorithmerge sort ascending orderc merge sort void 2a9 running merge sort on an array of size n which is already sorted is 2amerge sort using auxiliarylet p be a mergesort program to sort numbers in ascendinng order on a unknown data structure which take o 28n 5e2 29 time to find the mid element 2c rest property is unknown then recurrence relation for the same is 3fc 2b 2b merge sort time complexitymerge sort array algoalgoexpert merge sortrecursive merge sort cmerge sort algorihtmgeeks for geeks merge sort recursivemergesort complexity in already ascending ordermerge sort implementation in cc 2b 2b merge sort what is merge sort algorithmhow to indicate order in merge sortwhere we use merge sortmerge sort comdiffrent ways to make merge sort cmerge sort explainedmerge and sort in c 2b 2bmerge sort gfgmerge sort c 2b 2b one arrayssort an array a using merge sort change in the input array itself so no need to return or print anything merge sort in descending order c 2b 2bmerge sort algorithm cmerge sortingselection insertion merge sort c 2b 2bc 2b 2b merge sort librarymerge sort recursive c 2b 2b codemerge sort using vectorsmerge sort is in placedescribe the concept of merge sortstep through merge sort cppmergesort algomerge and sortc merge sort7 way split merge sortmerge sort c 2b 2b implementationmerge sort example with stepsc 2b 2b merge sorted vectorsmerge sort algorithm in cformula for dividing size of merge sorthow many passes will be needed to sort an array which contains 5 elements using merge sortmerge sort function in c 2b 2bhow to implement merge sortalgoritmo merge sortmerge sort in c 2b 2b03def merge sortfor the merge sort algorithm discussed in class 2c if the following change is made 2c the worst case runtime would bec 2b 2b code for merge sortalgorithm for merge sortmerge sort explanation in cmerge sort function in cmerge function cmerge sort and sort in c 2b 2bcontents of array before final merge sort proceduremerge sort java recursionc 2b 2b recursive merge sorthow value returnd in recursive merge sort in c 2b 2bmerge sort solvemergesort cpp codejava merge sort simplewhat is merge sortmerge sorrmerge sort using divide and conquer in csort an array using recursion time complexitymerge sort java recursive codemerge sort code c 2b 2bmerge sort cppmerge sort vector c 2b 2bhow to merge sortedtechnique of merge sortworst case complexity of merge sortmerge sort in an arraymerge sort demomerge sort implementation in c 2b 2bapplication of mergesort where gfgmerge sort in matrixmerge sort c 2b 2b 27merge sort program in cppmerge sort practice cmerge sort divides the list in i two equal parts ii n equal parts iii two parts 2c may not be equal iv n parts 2c may not be equalmergesort gfgsorting string merge sortc 2b 2b mergesortwrite an algorithm for merge sort and compute its complexity merge sort time and space complexitymerge 28 29 algo in merge sortin merge sort 2c what is the worst case complexity 3fmerge sort codewhat is the time complexity of traversing an array using merge sort methodc mergesort2 way merge sort code in cppmerge sort c 2b 2b recursivemergesortmerge sort algorithm exampleusing merge sort eggcode merge sortmerge sort site 3arosettacode orgmerger sort cmerge sort definitionmerge sort program in c 2b 2bmerge sort array c 2b 2bmerge sort function chow merge sort works 3fmerge sort code in c in ascending ordermerge sort c 2b 2b code explanationc 2b 2b merge sortmerje sort code in c 2b 2bmerge sort for c 2b 2bmergesort en csort set c 2b 2bhow does merge sort workaverage complexity of merge sorthow to perform merge sort in c 2b 2ba c code for merge sort use tabbing with binary search in the merging process merge sort implementmerge sort algorithm geekforgeeks 3fhow to merge sortmergesort cmerge sort workingwrite c functions to sort a set of integers in descending order using top down approach of merge sortshaker sort c geekssimple merge sort program in cmerge sort algoritm explainedmerge sort trong cwrite merge sort algorithm and compute its worst case and best case time complexity sort the list g 2cu 2cj 2ca 2cr 2ca 2ct in alphabetical order using merge sort merge sort in arraymerge sort code in c 2b 2bmerge sort cpp implementationmerge sorted arraymerge sort in placemerege functions merege sortmerge sort stl cppalready sorted order which sorting algo is best in merge sort and quick sortalgoritma merge sortimplementation of merge sort algorithm in c 2b 2bhow to sort in merge sortcpp merge and sortc 2b 2b merge sort codemerge sort algorithm explainedmerge sort amerge sort imerge sort in dsmerge sort in c 2bmerge sort in c 3d 3dmerge sort algorithm 3fmerge sort complete examplecode for merge sort in cmerge sort implementation javamerge sort algoithmbest merge sort implementationmerge sort techniquehow many vector does mergesort create c 2b 2bmerge sort simple algoc 2b 2b merge sort methodwhat is merge soerwrite the algorithm for merge sortbig o notation merge sortmerge sort formerge sort algorithm stepsmergesort in cmerge sort descending c 2b 2bmergesort implementation c 2b 2baverage tc of merge sortmerge sort code for cpseudo code for meerge sortc program for merge sortmerge sort sort complexitymerge sort c 2b 2b codemerge sort algorithmmerge sort in c algorithmalgo for merge sorthow does the mergesort workmerge sort cpp stlmergesort 5cno of merges require to get largest blockmerge sort pseudocodehow merge sort worksmerge sort function in cppmerge sort c