0% found this document useful (0 votes)
2 views4 pages

Assignment

This document contains an assignment for implementing the Merge Sort algorithm in Java. It includes the code for the Merge Sort implementation, which sorts an array and measures its execution time. The main method demonstrates the sorting of a sample array and prints the original and sorted arrays along with the execution time.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Assignment

This document contains an assignment for implementing the Merge Sort algorithm in Java. It includes the code for the Merge Sort implementation, which sorts an array and measures its execution time. The main method demonstrates the sorting of a sample array and prints the original and sorted arrays along with the execution time.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ASSIGNMENT – 2

NAME : [Link] REG NO :23BCE20250


SLOT : L55+L56

Question:
Implemention Of Merge Sort.
CODE :
package Mthed1;
public class TIME22{
void merge(int arr[],int left,int mid,int right){
int n1=mid-left+1;
int n2=right-mid;
int L[]=new int[n1];
int R[]=new int[n2];
for(int i=0;i<n1;i++)
L[i]=arr[left+i];
for(int j=0;j<n2;j++)
R[j]=arr[mid+1+j];
int i=0,j=0,k=left;
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++;
}
}
void sort(int arr[],int left,int right){
if(left<right){
int mid=(left+right)/2;
sort(arr,left,mid);
sort(arr,mid+1,right);
merge(arr,left,mid,right);
}
}
void printArray(int arr[]){
for(int num:arr)
[Link](num+" ");
[Link]();
}
public static void main(String[] args){
TIME22 ms1= new TIME22();
int arr[] = {45,23,78,12,89,34,56,10,67,5};
[Link]("Original Array:");
[Link](arr);
long startTime=[Link]();
[Link](arr,0,[Link]-1);
long endTime=[Link]();
[Link]("Sorted Array:");
[Link](arr);

double executionTime=(endTime-startTime)/1_000_000.0;
[Link]("Execution Time: "+executionTime +"ms");
}
OUTPUT:

You might also like