Merge Sort Java Program
- import java.util.Scanner;
- public class MergeSort
- {
- public static void sort(int[] a, int low, int high)
- {
- int N = high - low;
- if (N <= 1)
- return;
- int mid = low + N/2;
- // recursively sort
- sort(a, low, mid);
- sort(a, mid, high);
- // merging two sorted sub-arrays
- int[] temp = new int[N];
- int i = low, j = mid;
- for (int k = 0; k < N; k++)
- {
- if (i == mid)
- temp[k] = a[j++];
- else if (j == high)
- temp[k] = a[i++];
- else if (a[j]<a[i])
- temp[k] = a[j++];
- else
- temp[k] = a[i++];
- }
- for (int k = 0; k < N; k++)
- a[low + k] = temp[k];
- }
- public static void main(String[] args)
- {
- Scanner scan = new Scanner( System.in );
- System.out.println("Merge Sort");
- int n, i;
- System.out.println("Enter number");
- n = scan.nextInt();
- int arr[] = new int[ n ];
- for (i = 0; i < n; i++)
- arr[i] = scan.nextInt();
- sort(arr, 0, n);
- System.out.println("Sorted element");
- for (i = 0; i < n; i++)
- System.out.print(arr[i]+" ");
- System.out.println();
- }
- }
Merge sort time complexity
Worst case performance : O(n log n)
Best case performance : O(n log n)
Average case performance : O(n log n)
Comments
Post a Comment