MCPcopy Create free account
hub / github.com/chaharnishant11/CodeIn10DSA / merge

Method merge

Sorting/Homework/mergeSort.cpp:3–35  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2public:
3 vector<int> merge(vector<int> &A, vector<int>& B){
4 int sizeA=A.size();
5 int sizeB=B.size();
6 int i=0;
7 int j=0;
8 vector<int> ans;
9
10 //Compare and fill ans vector
11 while (i<sizeA && j<sizeB){
12 if(A[i]<B[j]){
13 ans.push_back(A[i]);
14 i=i+1;
15 } else{
16 ans.push_back(B[j]);
17 j=j+1;
18 }
19 }
20
21 //Add remaining elements from A
22 while(i<sizeA){
23 ans.push_back(A[i]);
24 i=i+1;
25 }
26
27 //Add remaining elements from B
28 while(j<sizeB){
29 ans.push_back(B[j]);
30 j=j+1;
31 }
32
33 return ans;
34
35 }
36
37 vector<int> mergeSort(vector<int>& nums,int start, int end) {
38 //Base Case

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected