Skip to content

Commit 5560be8

Browse files
committed
MergeSort was implemented SortAlgorithm
1 parent 2c356b5 commit 5560be8

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed
Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
1+
package sort;
2+
3+
import static sort.SortUtils.print;
4+
15
/**
6+
* This method implements the Generic Merge Sort
7+
*
28
*
39
* @author Varun Upadhyay (https://github.com/varunu28)
10+
* @author Podshivalov Nikita (https://github.com/nikitap492)
11+
*
12+
*
13+
* @see SortAlgorithm
414
*
515
*/
616

7-
class MergeSort {
17+
class MergeSort implements SortAlgorithm {
18+
819

920
/**
1021
* This method implements the Generic Merge Sort
22+
* @param unsorted the array which should be sorted
23+
* @param <T> Comparable class
24+
* @return sorted array
25+
*/
26+
@Override
27+
@SuppressWarnings("unchecked")
28+
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
29+
T[] tmp = (T[]) new Comparable[unsorted.length];
30+
doSort(unsorted, tmp, 0, unsorted.length - 1);
31+
return unsorted;
32+
}
33+
34+
/**
1135
*
1236
* @param arr The array to be sorted
1337
* @param temp The copy of the actual array
1438
* @param left The first index of the array
1539
* @param right The last index of the array
1640
* Recursively sorts the array in increasing order
1741
**/
18-
19-
public static <T extends Comparable<T>> void MS(T[] arr, T[] temp, int left, int right) {
42+
private static <T extends Comparable<T>> void doSort(T[] arr, T[] temp, int left, int right) {
2043
if (left < right) {
2144
int mid = left + (right - left) / 2;
22-
MS(arr, temp, left, mid);
23-
MS(arr, temp,mid + 1, right);
45+
doSort(arr, temp, left, mid);
46+
doSort(arr, temp,mid + 1, right);
2447
merge(arr, temp, left, mid, right);
2548
}
2649

@@ -37,16 +60,15 @@ public static <T extends Comparable<T>> void MS(T[] arr, T[] temp, int left, int
3760
* merges two parts of an array in increasing order
3861
**/
3962

40-
public static <T extends Comparable<T>> void merge(T[] arr, T[] temp, int left, int mid, int right) {
41-
for (int i=left;i<=right;i++) {
42-
temp[i] = arr[i];
43-
}
63+
private static <T extends Comparable<T>> void merge(T[] arr, T[] temp, int left, int mid, int right) {
64+
System.arraycopy(arr, left, temp, left, right - left + 1);
65+
4466

4567
int i= left;
4668
int j = mid + 1;
4769
int k = left;
4870

49-
while (i<=mid && j<=right) {
71+
while (i <= mid && j <= right) {
5072
if (temp[i].compareTo(temp[j]) <= 0) {
5173
arr[k] = temp[i];
5274
i++;
@@ -69,32 +91,17 @@ public static <T extends Comparable<T>> void merge(T[] arr, T[] temp, int left,
6991
public static void main(String[] args) {
7092

7193
// Integer Input
72-
int[] arr = {4,23,6,78,1,54,231,9,12};
73-
Integer[] array = new Integer[arr.length];
74-
for (int i=0;i<array.length;i++) {
75-
array[i] = arr[i];
76-
}
77-
78-
// Copy of actual array
79-
Integer[] temp = new Integer[arr.length];
80-
81-
MS(array, temp, 0, arr.length-1);
94+
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
95+
MergeSort mergeSort = new MergeSort();
96+
mergeSort.sort(arr);
8297

8398
// Output => 1 4 6 9 12 23 54 78 231
84-
for (int i=0;i<arr.length;i++) {
85-
System.out.print(array[i] + " ");
86-
}
87-
System.out.println();
88-
89-
// String Input
90-
String[] array1 = {"c", "a", "e", "b","d"};
91-
String[] temp1 = new String[array1.length];
92-
MS(array1, temp1, 0, array1.length-1);
99+
print(arr);
93100

101+
// String Inpu
102+
String[] stringArray = {"c", "a", "e", "b","d"};
103+
mergeSort.sort(stringArray);
94104
//Output => a b c d e
95-
for(int i=0; i<array1.length; i++)
96-
{
97-
System.out.print(array1[i]+"\t");
98-
}
105+
print(stringArray);
99106
}
100107
}

Sorts/src/sort/QuickSort.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* @author Varun Upadhyay (https://github.com/varunu28)
88
* @author Podshivalov Nikita (https://github.com/nikitap492)
99
*
10+
*
11+
* @see SortAlgorithm
12+
*
1013
*/
1114
class QuickSort implements SortAlgorithm {
1215

0 commit comments

Comments
 (0)