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

Commitd93ee0d

Browse files
realDuYuanChaogithub-actions
and
github-actions
authored
udpate insertion sort (TheAlgorithms#2176)
* udpate insertion sort* Formatted with Google Java FormatterCo-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent6dd5b20 commitd93ee0d

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

‎Sorts/InsertionSort.java‎

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,39 @@
33
importstaticSorts.SortUtils.less;
44
importstaticSorts.SortUtils.print;
55

6-
/**
7-
* @author Varun Upadhyay (https://github.com/varunu28)
8-
* @author Podshivalov Nikita (https://github.com/nikitap492)
9-
*/
106
classInsertionSortimplementsSortAlgorithm {
117

128
/**
13-
*This method implements theGenericInsertion Sort Sorts the arrayin increasing order
9+
* Genericinsertion sort algorithmin increasing order.
1410
*
15-
* @param array The array to be sorted
11+
* @param array the array to be sorted.
12+
* @param <T> the class of array.
13+
* @return sorted array.
1614
*/
1715
@Override
1816
public <TextendsComparable<T>>T[]sort(T[]array) {
19-
for (intj =1;j <array.length;j++) {
20-
21-
// Picking up the key(Card)
22-
Tkey =array[j];
23-
inti =j -1;
24-
25-
while (i >=0 &&less(key,array[i])) {
26-
array[i +1] =array[i];
27-
i--;
17+
for (inti =1;i <array.length;i++) {
18+
TinsertValue =array[i];
19+
intj;
20+
for (j =i -1;j >=0 &&less(insertValue,array[j]);j--) {
21+
array[j +1] =array[j];
22+
}
23+
if (j !=i -1) {
24+
array[j +1] =insertValue;
2825
}
29-
// Placing the key (Card) at its correct position in the sorted subarray
30-
array[i +1] =key;
3126
}
3227
returnarray;
3328
}
3429

35-
// DriverProgram
30+
/** DriverCode */
3631
publicstaticvoidmain(String[]args) {
37-
// Integer Input
3832
Integer[]integers = {4,23,6,78,1,54,231,9,12};
39-
4033
InsertionSortsort =newInsertionSort();
41-
4234
sort.sort(integers);
35+
print(integers);/* [1, 4, 6, 9, 12, 23, 54, 78, 231] */
4336

44-
// Output => 1 4 6 9 12 23 54 78 231
45-
print(integers);
46-
47-
// String Input
4837
String[]strings = {"c","a","e","b","d"};
49-
5038
sort.sort(strings);
51-
52-
// Output => abcde
53-
print(strings);
39+
print(strings);/* [a, b, c, d, e] */
5440
}
5541
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp