GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — selection sort (#2177) · JavaInCloud/Java@cebd052

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Sorts
    • SelectionSort.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Sorts/SelectionSort.java‎

Copy file name to clipboard
+18-39Lines 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 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.