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

Commitcebd052

Browse files
realDuYuanChaogithub-actions
and
github-actions
authored
selection sort (TheAlgorithms#2177)
* selection sort* Formatted with Google Java FormatterCo-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parentd93ee0d commitcebd052

File tree

1 file changed

+18
-39
lines changed

1 file changed

+18
-39
lines changed

‎Sorts/SelectionSort.java‎

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

3-
/**
4-
* @author Varun Upadhyay (https://github.com/varunu28)
5-
* @author Podshivalov Nikita (https://github.com/nikitap492)
6-
* @see SortAlgorithm
7-
*/
83
publicclassSelectionSortimplementsSortAlgorithm {
94

105
/**
11-
*This method swaps the two elementsinthe array
6+
*Generic selection sort algorithminincreasing order.
127
*
13-
* @param <T>
14-
* @param arr, i, j The array for the swap and the indexes of the to-swap elements
15-
*/
16-
public <T>voidswap(T[]arr,inti,intj) {
17-
Ttemp =arr[i];
18-
arr[i] =arr[j];
19-
arr[j] =temp;
20-
}
21-
22-
/**
23-
* This method implements the Generic Selection Sort
24-
*
25-
* @param arr The array to be sorted Sorts the array in increasing order
8+
* @param arr the array to be sorted.
9+
* @param <T> the class of array.
10+
* @return sorted array.
2611
*/
2712
@Override
2813
public <TextendsComparable<T>>T[]sort(T[]arr) {
2914
intn =arr.length;
3015
for (inti =0;i <n -1;i++) {
31-
// Initial index of min
32-
intmin =i;
33-
16+
intminIndex =i;
3417
for (intj =i +1;j <n;j++) {
35-
if (arr[min].compareTo(arr[j]) >0) {
36-
min =j;
18+
if (arr[minIndex].compareTo(arr[j]) >0) {
19+
minIndex =j;
3720
}
3821
}
39-
40-
// Swapping if index of min is changed
41-
if (min !=i) {
42-
swap(arr,i,min);
22+
if (minIndex !=i) {
23+
Ttemp =arr[i];
24+
arr[i] =arr[minIndex];
25+
arr[minIndex] =temp;
4326
}
4427
}
45-
4628
returnarr;
4729
}
4830

49-
// DriverProgram
31+
/** DriverCode */
5032
publicstaticvoidmain(String[]args) {
5133

5234
Integer[]arr = {4,23,6,78,1,54,231,9,12};
53-
5435
SelectionSortselectionSort =newSelectionSort();
55-
5636
Integer[]sorted =selectionSort.sort(arr);
37+
for (inti =0;i <sorted.length -1; ++i) {
38+
assertsorted[i] <=sorted[i +1];
39+
}
5740

58-
// Output => 1 4 6912235478231
59-
SortUtils.print(sorted);
60-
61-
// String Input
6241
String[]strings = {"c","a","e","b","d"};
6342
String[]sortedStrings =selectionSort.sort(strings);
64-
65-
// Output => ab c de
66-
SortUtils.print(sortedStrings);
43+
for (inti =0;i <sortedStrings.length -1; ++i) {
44+
assertstrings[i].compareTo(strings[i +1]) <=0;
45+
}
6746
}
6847
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp