Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 01a49d7

Browse files
implementation of Merge Sort algorithm using array.
1 parent 8d22965 commit 01a49d7

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.udemy.dsapart1.sortings;
2+
import java.util.Arrays;
3+
import java.util.Scanner;
4+
5+
public class MergeSortingUsingDivideAndConquer {
6+
7+
public static void main(String[] args) {
8+
Scanner scInp=new Scanner(System.in);
9+
System.out.print("\nEnter the size of Array : ");
10+
int sizeOfArr = scInp.nextInt();
11+
double[] inputArr = new double[sizeOfArr];
12+
System.out.println("\nEnter the Array elements : ");
13+
pushElementsIntoArray(inputArr);
14+
System.out.println("\nInput Array : " + Arrays.toString(inputArr));
15+
performMergeSort(inputArr);
16+
System.out.println("\nRequired Merge Sorted array : "+Arrays.toString(inputArr));
17+
}
18+
19+
20+
/**
21+
* Method to perform the Merge Sort algorithm using divide & Conquer approach.
22+
*
23+
* @param inputArr2
24+
* Time complexity - n*log n.
25+
*
26+
*/
27+
private static void performMergeSort(double[] inputArr2) {
28+
if (inputArr2.length < 2) {
29+
return;
30+
}
31+
int midIndex = (inputArr2.length / 2);
32+
double leftSubArray[] = new double[midIndex];
33+
double rightSubArray[] = new double[inputArr2.length - midIndex];
34+
// fill the elements to left sub-array
35+
for (int i = 0; i < midIndex; i++) {
36+
leftSubArray[i] = inputArr2[i];
37+
}
38+
// fill the elements to right sub-array
39+
for (int i = midIndex; i < inputArr2.length; i++) {
40+
rightSubArray[i-midIndex] = inputArr2[i];
41+
}
42+
//recursive call on sorted left-sub array & right sub-array.
43+
performMergeSort(leftSubArray);
44+
performMergeSort(rightSubArray);
45+
//Pass sorted sub-arrays
46+
doSubArrayMergeOperation(leftSubArray,rightSubArray,inputArr2);
47+
}
48+
49+
50+
/**
51+
*
52+
* This method will perform merge operation on input sorted sub-array
53+
* @param leftSubArray
54+
* @param rightSubArray
55+
* @param inputArr2
56+
*
57+
*/
58+
private static void doSubArrayMergeOperation(double[] leftSubArray, double[] rightSubArray, double[] inputArr2) {
59+
int i = 0, j = 0, k = 0;
60+
while (i < leftSubArray.length && j < rightSubArray.length) {
61+
if (leftSubArray[i] <= rightSubArray[j]) {
62+
inputArr2[k++] = leftSubArray[i++];
63+
}
64+
else {
65+
inputArr2[k++] = rightSubArray[j++];
66+
}
67+
}
68+
while (i < leftSubArray.length) {
69+
inputArr2[k++] = leftSubArray[i++];
70+
}
71+
while (j < rightSubArray.length) {
72+
inputArr2[k++] = rightSubArray[j++];
73+
}
74+
}
75+
76+
77+
public static void pushElementsIntoArray(double inputArr[]) {
78+
Scanner scInp1Obj = new Scanner(System.in);
79+
for (int i = 0; i < inputArr.length; i++) {
80+
System.out.print("\nEnter the element No " + (i + 1) + " : ");
81+
inputArr[i] = scInp1Obj.nextDouble();
82+
}
83+
}
84+
85+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /