Movatterモバイル変換


[0]ホーム

URL:


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

Commitea12311

Browse files
realDuYuanChaogithub-actions
and
github-actions
authored
fixed merge sort (TheAlgorithms#2202)
* reformat merge sort* Formatted with Google Java FormatterCo-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent8e22810 commitea12311

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

‎Sorts/MergeSort.java‎

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
packageSorts;
22

3-
importstaticSorts.SortUtils.print;
4-
53
/**
6-
*This method implements theGenericMerge Sort
4+
* Genericmerge sort algorithm.
75
*
8-
* @author Varun Upadhyay (https://github.com/varunu28)
9-
* @author Podshivalov Nikita (https://github.com/nikitap492)
106
* @see SortAlgorithm
117
*/
128
classMergeSortimplementsSortAlgorithm {
139

1410
/**
15-
*This method implements the Generic Merge Sort
11+
*Generic merge sort algorithm implements.
1612
*
17-
* @param unsorted the array which should be sorted
18-
* @param <T> Comparable class
19-
* @return sorted array
13+
* @param unsorted the array which should be sorted.
14+
* @param <T> Comparable class.
15+
* @return sorted array.
2016
*/
2117
@Override
2218
public <TextendsComparable<T>>T[]sort(T[]unsorted) {
@@ -25,29 +21,30 @@ public <T extends Comparable<T>> T[] sort(T[] unsorted) {
2521
}
2622

2723
/**
28-
* @param arrThe array to be sorted
29-
* @param leftThe first index of the array
30-
* @param rightThe last index of the array Recursively sorts the array in increasing order
24+
* @param arrthe array to be sorted.
25+
* @param leftthe first index of the array.
26+
* @param rightthe last index of the array.
3127
*/
3228
privatestatic <TextendsComparable<T>>voiddoSort(T[]arr,intleft,intright) {
3329
if (left <right) {
34-
intmid =left +(right -left) /2;
30+
intmid =(left +right) >>>1;
3531
doSort(arr,left,mid);
3632
doSort(arr,mid +1,right);
3733
merge(arr,left,mid,right);
3834
}
3935
}
4036

4137
/**
42-
*This method implements the merge stepofthe merge sort
38+
*Merges two partsofan array.
4339
*
44-
* @param arrThe array to besorted
45-
* @param leftThe first index of the array
46-
* @param midThe middle index of the array
47-
* @param rightThe last index of the array merges two parts of an array in increasing order
40+
* @param arrthe array to bemerged.
41+
* @param leftthe first index of the array.
42+
* @param midthe middle index of the array.
43+
* @param rightthe last index of the array merges two parts of an array in increasing order.
4844
*/
4945
privatestatic <TextendsComparable<T>>voidmerge(T[]arr,intleft,intmid,intright) {
5046
intlength =right -left +1;
47+
@SuppressWarnings("unchecked")
5148
T[]temp = (T[])newComparable[length];
5249
inti =left;
5350
intj =mid +1;
@@ -72,21 +69,20 @@ private static <T extends Comparable<T>> void merge(T[] arr, int left, int mid,
7269
System.arraycopy(temp,0,arr,left,length);
7370
}
7471

75-
// Driverprogram
72+
/** Drivercode */
7673
publicstaticvoidmain(String[]args) {
74+
MergeSortmergeSort =newMergeSort();
7775

78-
// Integer Input
7976
Integer[]arr = {4,23,6,78,1,54,231,9,12};
80-
MergeSortmergeSort =newMergeSort();
8177
mergeSort.sort(arr);
78+
for (inti =0;i <arr.length -1; ++i) {
79+
assertarr[i] <=arr[i +1];
80+
}
8281

83-
// Output => 1 4 6912235478231
84-
print(arr);
85-
86-
// String Inpu
8782
String[]stringArray = {"c","a","e","b","d"};
8883
mergeSort.sort(stringArray);
89-
// Output => abcde
90-
print(stringArray);
84+
for (inti =0;i <stringArray.length -1; ++i) {
85+
assertarr[i].compareTo(arr[i +1]) <=0;
86+
}
9187
}
9288
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp