Skip to content

Commit cebd052

Browse files
realDuYuanChaogithub-actions
andauthored
selection sort (TheAlgorithms#2177)
* selection sort * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent d93ee0d commit cebd052

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
package Sorts;
22

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

105
/**
11-
* This method swaps the two elements in the array
6+
* Generic selection sort algorithm in increasing 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> void swap(T[] arr, int i, int j) {
17-
T temp = 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 <T extends Comparable<T>> T[] sort(T[] arr) {
2914
int n = arr.length;
3015
for (int i = 0; i < n - 1; i++) {
31-
// Initial index of min
32-
int min = i;
33-
16+
int minIndex = i;
3417
for (int j = 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+
T temp = arr[i];
24+
arr[i] = arr[minIndex];
25+
arr[minIndex] = temp;
4326
}
4427
}
45-
4628
return arr;
4729
}
4830

49-
// Driver Program
31+
/** Driver Code */
5032
public static void main(String[] args) {
5133

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

58-
// Output => 1 4 6 9 12 23 54 78 231
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 => a b c d e
66-
SortUtils.print(sortedStrings);
43+
for (int i = 0; i < sortedStrings.length - 1; ++i) {
44+
assert strings[i].compareTo(strings[i + 1]) <= 0;
45+
}
6746
}
6847
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy