Problem:
Java Code for Merge sort using Divide and Conquer method
Solution:
package com.basics;
import java.util.Arrays;
import java.util.Scanner;
public class MergeSort {
static int[] array;
int arrayLimit;
public static void main(String[] args) {
MergeSort mergeSort = new MergeSort();
mergeSort.initializeTheArrays();
System.out.println("Before sorting an array is " + Arrays.toString(array));
mergeSort.doMergeSort(array, 0, array.length - 1);
System.out.println("After sorting an array is " + Arrays.toString(array));
}
public void initializeTheArrays(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the array limit");
arrayLimit = scanner.nextInt();
array = new int[arrayLimit];
System.out.println("enter the array elements");
for (int i = 0; i < array.length; i++) {
System.out.println("enter the element");
array[i] = scanner.nextInt();
}
scanner.close();
}
public void doMergeSort(int[] a, int l, int r){
int m;
if(l < r){
m = (l + r) / 2;
doMergeSort(a, l, m);
doMergeSort(a, m + 1, r);
merge(a, l, m, r);
}
}
public void merge(int[] a, int l, int m, int r){
int n1 = m - l + 1;
int n2 = r - m;
int[] leftArray = new int[n1];
int[] rightArray = new int[n2];
for (int i = 0; i < leftArray.length; ++i) {
leftArray[i] = a[l+i];
}
for (int i = 0; i < rightArray.length; ++i) {
rightArray[i] = a[m+1+i];
}
int i = 0;
int j = 0;
int k = l;
while (i < n1 && j < n2)
{
if (leftArray[i] <= rightArray[j])
{
a[k] = leftArray[i];
i++;
}
else
{
a[k] = rightArray[j];
j++;
}
k++;
}
while (i < n1)
{
a[k] = leftArray[i];
i++;
k++;
}
while (j < n2)
{
a[k] = rightArray[j];
j++;
k++;
}
}
}
Output:
enter the array limit
7
enter the array elements
enter the element
123
enter the element
3
enter the element
456
enter the element
7
enter the element
89
enter the element
90
enter the element
34
Before sorting an array is [123, 3, 456, 7, 89, 90, 34]
After sorting an array is [3, 7, 34, 89, 90, 123, 456]
Java Code for Merge sort using Divide and Conquer method
Solution:
package com.basics;
import java.util.Arrays;
import java.util.Scanner;
public class MergeSort {
static int[] array;
int arrayLimit;
public static void main(String[] args) {
MergeSort mergeSort = new MergeSort();
mergeSort.initializeTheArrays();
System.out.println("Before sorting an array is " + Arrays.toString(array));
mergeSort.doMergeSort(array, 0, array.length - 1);
System.out.println("After sorting an array is " + Arrays.toString(array));
}
public void initializeTheArrays(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the array limit");
arrayLimit = scanner.nextInt();
array = new int[arrayLimit];
System.out.println("enter the array elements");
for (int i = 0; i < array.length; i++) {
System.out.println("enter the element");
array[i] = scanner.nextInt();
}
scanner.close();
}
public void doMergeSort(int[] a, int l, int r){
int m;
if(l < r){
m = (l + r) / 2;
doMergeSort(a, l, m);
doMergeSort(a, m + 1, r);
merge(a, l, m, r);
}
}
public void merge(int[] a, int l, int m, int r){
int n1 = m - l + 1;
int n2 = r - m;
int[] leftArray = new int[n1];
int[] rightArray = new int[n2];
for (int i = 0; i < leftArray.length; ++i) {
leftArray[i] = a[l+i];
}
for (int i = 0; i < rightArray.length; ++i) {
rightArray[i] = a[m+1+i];
}
int i = 0;
int j = 0;
int k = l;
while (i < n1 && j < n2)
{
if (leftArray[i] <= rightArray[j])
{
a[k] = leftArray[i];
i++;
}
else
{
a[k] = rightArray[j];
j++;
}
k++;
}
while (i < n1)
{
a[k] = leftArray[i];
i++;
k++;
}
while (j < n2)
{
a[k] = rightArray[j];
j++;
k++;
}
}
}
Output:
enter the array limit
7
enter the array elements
enter the element
123
enter the element
3
enter the element
456
enter the element
7
enter the element
89
enter the element
90
enter the element
34
Before sorting an array is [123, 3, 456, 7, 89, 90, 34]
After sorting an array is [3, 7, 34, 89, 90, 123, 456]
No comments:
Post a Comment