Skip to content
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
Arrays.sort(a2);//Sort the Array two
for(int K=0;K<N;K++){
System.out.println(a2[K]);
}
}
}