Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge #386

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Merge #386

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions GeeksForGeeks/MergeSortWithoutExtraSpace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import java.util.Arrays;
import java.util.Collection;

/**
* MergeSort_2
*/
//Q MergeSort two Arrays without taking any Extra Space
public class MergeSortWithoutExtraSpace {

public static void main(String[] args) {
int N = 2, M = 3;
int a1[] = { 10, 12 };// first Input Array
int a2[] = { 5, 18, 20 };//second Input Array

int i=N-1;//i value will start form total number-1
int j=0;
//This function will Simple compare the value and swap the value
//if Array1 value is smaller than Array2 it will sawp it.
while(i>=0 && j<M){
if(a1[i]>a2[j]){
int temp=a1[i];
a1[i]=a2[j];
a2[j]=temp;


}i--;j++;
}
Arrays.sort(a1);//sort the Array one
RAshid602 marked this conversation as resolved.
Show resolved Hide resolved
Arrays.sort(a2);//Sort the Array two
for(int K=0;K<N;K++){
System.out.println(a2[K]);
}
}
}