Skip to content

MergeSort #165

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
98 changes: 98 additions & 0 deletions scripts/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import java.util.*;

class Merge_Sort
{
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

static void mergeSort(int arr[], int l, int r)
{
GfG g = new GfG();
if (l < r)
{
int m = (l+r)/2;
mergeSort(arr, l, m);
mergeSort(arr , m+1, r);
g.merge(arr, l, m, r);
}
}

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T>0)
{
int n = sc.nextInt();
Merge_Sort ms = new Merge_Sort();
int arr[] = new int[n];
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();

GfG g = new GfG();
mergeSort(arr,0,arr.length-1);

ms.printArray(arr);
T--;
}
}
}


// } Driver Code Ends
/* The task is to complete merge() which is used
in below mergeSort() */
class GfG
{
// Merges two subarrays of arr[]. First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int low, int mid, int high)
{
int n1=mid-low+1;
int n2=high-mid;
int L[]=new int[n1];
int R[]=new int[n2];

for(int i=0;i<n1;i++)
L[i]=arr[low+i];

for(int j=0;j<n2;j++)
R[j]=arr[mid+1+j];

int k=low,i=0,j=0;
while(i<n1 && j<n2)
{
if(L[i]<=R[j])
{
arr[k]=L[i];
i++;
}
else
{
arr[k]=R[j];
j++;
}
k++;
}

while(i<n1)
{
arr[k]=L[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=R[j];
j++;
k++;
}


}
}