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

Appearance settings

Latest commit

 

History

History
History
107 lines (100 loc) · 2.97 KB

File metadata and controls

107 lines (100 loc) · 2.97 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.Scanner;
/**
* Merge Sort
*
*/
public class MergeSort {
private int[] array;
private int[] tempMergArr;
private int length;
/**
* Sorts {@code inputArr} with merge sort algorithm.
*
* @param inputArr
*/
public final void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[this.length];
this.mergeSort(0, this.length - 1);
}
/**
* Partitions Array into recursively smaller pieces.
*
* @param lowerIndex
* lower bound to include in the first partition
* @param higherIndex
* upper bound to include in the third partition
*/
private void mergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// Below step sorts the left side of the array
this.mergeSort(lowerIndex, middle);
// Below step sorts the right side of the array
this.mergeSort(middle + 1, higherIndex);
// Now merge both sides
this.mergeParts(lowerIndex, middle, higherIndex);
}
}
/**
* Merges partitions.
*
* @param lowerIndex
* @param middle
* @param higherIndex
*/
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
this.tempMergArr[i] = this.array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (this.tempMergArr[i] <= this.tempMergArr[j]) {
this.array[k] = this.tempMergArr[i];
i++;
} else {
this.array[k] = this.tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
this.array[k] = this.tempMergArr[i];
k++;
i++;
}
}
/**
* Gets input to sort.
*
* @return unsorted array of integers to sort
*/
public static int[] getInput() {
final int numElements = 6;
int[] unsorted = new int[numElements];
Scanner input = new Scanner(System.in);
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
for (int i = 0; i < numElements; i++) {
unsorted[i] = input.nextInt();
}
input.close();
return unsorted;
}
/**
* Main Method.
*
* @param args
*/
public static void main(String args[]) {
int[] inputArr = getInput();
MergeSort mergeSort = new MergeSort();
mergeSort.sort(inputArr);
for (int i : inputArr) {
System.out.print(i);
System.out.print(" ");
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.