dsa/Sorting/MergeSort.java

76 lines
2.2 KiB
Java

// write a code of mergesort using function in java
import java.util.ArrayList;
import java.util.Scanner;
public class MergeSort{
// use notation beginning, mid and end
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
mergeSort(arr, 0, n-1);
System.out.println("The sorted array is: ");
for(int i=0; i<n; i++){
System.out.print(arr[i] + " ");
}
sc.close();
}
static void mergeSort(int arr[], int beginning, int end){
if(beginning < end){
// find the middle point
int mid = (beginning + end) / 2;
// sort first and second halves
mergeSort(arr, beginning, mid);
mergeSort(arr, mid + 1, end);
// merge the sorted halves
merge(arr, beginning, mid, end);
}
}
static void merge(int[] arr, int low, int mid, int high) {
ArrayList<Integer> temp = new ArrayList<>(); // temporary array
int left = low; // starting index of left half of arr
int right = mid + 1; // starting index of right half of arr
//storing elements in the temporary array in a sorted manner//
while (left <= mid && right <= high) {
if (arr[left] <= arr[right]) {
temp.add(arr[left]);
left++;
} else {
temp.add(arr[right]);
right++;
}
}
// if elements on the left half are still left //
while (left <= mid) {
temp.add(arr[left]);
left++;
}
// if elements on the right half are still left //
while (right <= high) {
temp.add(arr[right]);
right++;
}
// transfering all elements from temporary to arr //
for (int i = low; i <= high; i++) {
arr[i] = temp.get(i - low);
}
}
}