Skip to content

Instantly share code, notes, and snippets.

@laxmankumar2000
Created April 26, 2021 12:14
Show Gist options
  • Save laxmankumar2000/d3a6c45e6ad0eb42377f6976cc1529a2 to your computer and use it in GitHub Desktop.
Save laxmankumar2000/d3a6c45e6ad0eb42377f6976cc1529a2 to your computer and use it in GitHub Desktop.
Error_In_Merge_Short
package Sortting.MergeSort;
public class MergeSortExample {
int arr[];
public MergeSortExample(int size)
{
arr = new int[size];
}
public void MergeSort(int arr[] , int lb , int ub)
{
if (lb<ub)
{
int mid = (lb+ub)/2;
MergeSort(arr,lb,mid);
MergeSort(arr,mid+1,ub);
Merge(arr,lb,mid,ub);
}
}
public void Merge(int arr[] , int lb , int mid , int ub)
{
int i,j,k;
i=lb;
j=mid+1;
k=lb;
while(i<=mid && j<=ub)
{
if (arr[i]<=arr[j])
{
arr[k] = arr[i];
i++;
}
else
{
arr[k] = arr[j];
j++;
}
k++;
}
if (i>mid)
{
while(j<=ub) {
arr[k] = arr[j];
j++;
k++;
}
}
else
{
while(i<=mid)
{
arr[k]= arr[i];
i++;
k++;
}
}
}
}
class test{
public static void main(String[] args) {
int arr1[] = {90,23,45,89};
MergeSortExample obj = new MergeSortExample(arr1.length);
obj.MergeSort(arr1,0, arr1.length-1);
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment