0% found this document useful (0 votes)
6 views2 pages

import java

The document contains a Java implementation of the Merge Sort algorithm. It includes methods for sorting an array, merging subarrays, and printing the array elements. The code has some minor corrections in the parameter order for the merge function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

import java

The document contains a Java implementation of the Merge Sort algorithm. It includes methods for sorting an array, merging subarrays, and printing the array elements. The code has some minor corrections in the parameter order for the merge function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.*; import java.util.

*;
public class Main{ public class Main{
public static void printarr(int arr[]){ public static void printarr(int arr[]){
for(int i=0;i<arr.length;i++){ for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" "); System.out.print(arr[i]+" ");
} }
System.out.println(); System.out.println();
} }
public static void mergesort(int arr[],int public static void mergesort(int arr[],int
si,int ei){ si,int ei){
if(si>=ei){ if(si>=ei){
return; return;
} }
int mid=si+(ei-si)/2; int mid=si+(ei-si)/2;
mergesort(arr,si,mid); mergesort(arr,si,mid);
mergesort(arr,mid+1,ei); mergesort(arr,mid+1,ei);
merge(arr,mid,si,ei); merge(arr,si,mid,ei); // Corrected the
parameter order
} }
public static void merge(int arr[],int si,int public static void merge(int arr[],int si,int
mid,int ei){ mid,int ei){
int temp[]=new int[ei-si+1]; int temp[]=new int[ei-si+1];
int i=si;//iterator left array int i=si;//iterator left array
int j=mid +1;//iterator right array int j=mid+1;//iterator right array
int k=0;//iterator for temp array int k=0;//iterator for temp array

while(i<=mid && j<=ei){ while(i<=mid && j<=ei){


if(arr[i]<arr[j]){ if(arr[i]<arr[j]){
temp[k]=arr[i]; temp[k]=arr[i];
i++; i++;
k++; k++;
}else{
}else{ temp[k]=arr[j];
temp[k]=arr[j]; j++;
j++; k++;
k++; }
} }
} while(i<=mid){//if present element in left
while(i<=mid){//if present element in left array
array temp[k++]=arr[i++];
temp[k++]=arr[i++]; }
} while(j<=ei){//if present element in right
while(j<=ei){//if present element in right array
array temp[k++]=arr[j++];
temp[k++]=arr[j++]; }
}
for(k=0,i=si;k<temp.length;k++,i++){//copy
for(k=0,i=si;k<temp.length;k++,i++){//copy element in original array
element in original array arr[i]=temp[k];
arr[i]=temp[k]; }
} }
} public static void main(String args[]){
public static void main(String args[]){ int arr[]={6,3,9,5,2,8};
int arr[]={6,3,9,5,2,8}; mergesort(arr,0,arr.length-1);
mergesort(arr,0,arr.length-1); printarr(arr);
printarr(arr); }
}
}
}

You might also like