-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMergingSortiedList.cpp
More file actions
49 lines (39 loc) · 1.75 KB
/
MergingSortiedList.cpp
File metadata and controls
49 lines (39 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
using namespace std;
int main(){
int n1,n2;
cin>>n1; //taking first input and creating array to store first array
int arr1[n1];
for(int i=0;i<n1;i++){
cin>>arr1[i];
}
cin>>n2; //taking second input and creating array to store second array
int arr2[n2];
for(int i=0;i<n2;i++){
cin>>arr2[i];
}
int temp1=0,temp2=0,temp=0; //declaring temperory variables for furher process
int arr[n1+n2]; //declaring array to store full sorted array
while(temp1!= n1 && temp2!=n2){ //checking conditions whether all elements in any one of the array compare or not
if(arr1[temp1]>arr2[temp2]){ //comparing elements in two arrays
arr[temp++]=arr2[temp2++]; // assinging snaller one to array and going for next element in array which contain small element
}
else if(arr2[temp2]>arr1[temp1]){// assinging snaller one to array and going for next element in array which contain small element
arr[temp++]=arr1[temp1++];
}
else{ //if both elements are same then assigning both the arrray and going for next elements in arrays
arr[temp++]=arr1[temp1++];
arr[temp++]=arr2[temp2++];
}
}
for(int i=temp1;i<n1;i++){ //after checking complete elements in any on of the array adding remaining elements of other array to full sorted array
arr[temp++]=arr1[i];
}
for(int i=temp2;i<n2;i++){ //after checking complete elements in any on of the array adding remaining elements of other array to full sorted array
arr[temp++]=arr2[i];
}
for(int i=0;i<temp;i++){
cout<<arr[i]<<" "; //printing sorted array
}
return 0;
}