Java Reference
In-Depth Information
24 swap(minPos, i);
25 }
26 }
27
28 /**
29
Finds the smallest element in a tail range of the array.
30 @param from
the first position in a to compare
31 @return
the position of the smallest element in the
32
range a[from] . . . a[a.length - 1]
33 */
34
private int
minimumPosition (
int
from)
35 {
36
int
minPos = from;
37
for
(
int
i = from +
1
; i < a.length; i++)
38
if
(a[i] < a[minPos]) minPos = i;
39
return
minPos;
40 }
41
42 /**
43
Swaps two entries of the array.
44 @param i
the first position to swap
45 @param j
the second position to swap
46 */
47
private void
swap(
int
i,
int
j)
48 {
49
int
temp = a[i] ;
50 a[i] = a[j];
51 a[j] = temp;
52 }
53
54
private int
[] a;
55 }
630
ch14/selsort/SelectionSortDemo.java
1
import
java. util.Arrays;
2
3 /**
4
This program demonstratesthe selection sort algorithm by
5
sorting an array that is filled with random numbers.
6 */