merge sort c 2b 2b vector

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

showing results for - "merge sort c 2b 2b vector"
Elena
15 Nov 2017
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
merge sort code in c 2b 2bmerge sort c 2b 2b one arraysimplement the merge sort algorithm c 2b 2bmerge sort c 2b 2b one arraymerge sort descending order c 2b 2bmerge sort c 2b 2b 5cmerge sorting code in c 2b 2bmerge sort c 2b 2b programmergesort cppmerge sort ascending c 2b 2bmerge sort program in cppwrite 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 2 way merge sort code in cppmerge sort array c 2b 2bc 2b 2b merge sorted vectorsmerge sort cppsort set c 2b 2bstep through merge sort cppmerge sort in c 2b 2b stlmergesort in c 2b 2bmerge sort algorithm c 2b 2bc 2b 2b merge sort two vectorsmerge sort c 2b 2b with vectormerge sort javamerge function merge sort c 2b 2bmerge sort program in cmerge sort cpp stlmerge sort c 2b 2b 27merge sort c 2b 2b implementationmerge and sort in c 2b 2bmerge sort stl cppmerge sort algorithm c 2b 2b arraymerge sort c 2b 2balgoritm merge sort c 2b 2bmerge sort program in c 2b 2bhow to import merge sort to c 2b 2bpseudocode merge sort c 2b 2bmerge sort vector c 2b 2bhow value returnd in recursive merge sort in c 2b 2bmerge sort c 2b 2b code explanationclrs merge sort c 2b 2bmax and min using merge sortmerge sort for c 2b 2bmerge sort code c 2b 2bmergesort c 2b 2bmerge sort two arrays cpphow many vector does mergesort create c 2b 2bmerge sort ccontoh mergesort c 2b 2bc 2b 2b merge sort libraryc 2b 2b merge sort vectorcpp merge and sorthow to perform merge sort in c 2b 2bmerge sort in c 2b 2b03merge sort cpp codeimport merge sort in c 2b 2bimplements the merge sort for any data typemerge sort cpp implementationstl has merge sort algorithm c 2b 2bmerge sort in cppmerge sort a vector c 2b 2bmerge sort in c 2bc 2b 2b mergesortc 2b 2b merge sort functionmerge sort function in cppmerge sort in c 2b 2bmerge sort code c 2b 2bc 2b 2b merge sortc 2b 2b merge sort using comparatormerge sort descending c 2b 2bstd merge sort c 2b 2bmerge sort c 2b 2b codemerge sort using vectorsmerge sorting c 2b 2bmerge sort source code in cc 2b 2b merge sort methodmerge sort recursive program in c 2b 2bc 2b 2b merge sort time complexitymerge sort algorithm cppc 2b 2b merge sortingmerge sort function c 2b 2bmerge and mergesort function github c 2b 2bmerge sort in descending order c 2b 2bmerge sort c 2b 2b vector