Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active September 12, 2018 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/fca8e277b76f0907053da6e58c990aee to your computer and use it in GitHub Desktop.
Save thmain/fca8e277b76f0907053da6e58c990aee to your computer and use it in GitHub Desktop.
public class MergeSort {
private int arrSize;
private int [] arrAux;
private int [] arrInput;
public MergeSort(int [] arrInput){
this.arrInput = arrInput;
arrSize = arrInput.length;
arrAux = new int [arrSize];
}
public int[] mergeSorting(){
sort(0,arrSize-1);
return arrInput;
}
public void sort(int low, int high){
if(low<high){
int mid = low+((high-low))/2;
sort(low,mid);
sort(mid+1,high);
merge(low, mid, high);
}
}
public void merge(int low, int mid, int high){
//copy the entire array in the Auxilary array
for(int i=low;i<=high;i++){
arrAux[i] = arrInput[i];
}
int i = low;
int j = mid+1;
int k = low;
while(i<=mid && j<=high){
if(arrAux[i]<=arrAux[j]){
arrInput[k]=arrAux[i];
i++;
}
else{
arrInput[k]=arrAux[j];
j++;
}
k++;
}
while(i<=mid){
arrInput[k]=arrAux[i];
i++;
k++;
}
while(j<=high){
arrInput[k]=arrAux[j];
j++;
k++;
}
}
public void displayArray(int [] b){
for(int i=0;i<b.length;i++){
System.out.print(" " + b[i]);
}
}
public static void main(String[] args){
int [] a = {2,1,6,3,9,4,5,10};
MergeSort m = new MergeSort(a);
int [] b = m.mergeSorting();
m.displayArray(b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment