-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelection.cs
87 lines (77 loc) · 2.62 KB
/
Selection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/******************************************************************************
* http://algs4.cs.princeton.edu/21elementary/Selection.java.html
* Data files: http://algs4.cs.princeton.edu/21elementary/tiny.txt
* http://algs4.cs.princeton.edu/21elementary/words3.txt
* SORTING
* Sorts a sequence of strings from standard input using selection sort.
*
* % more tiny.txt
* S O R T E X A M P L E
*
* % java Selection < tiny.txt
* A E E L M O P R S T X [ one string per line ]
*
* % more words3.txt
* bed bug dad yes zoo ... all bad yet
*
* % java Selection < words3.txt
* all bad bed bug dad ... yes yet zoo [ one string per line ]
*
******************************************************************************/
namespace SedgewickWayne.Algorithms
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
/**
* The {@code Selection} class provides static methods
* for sorting an array using selection sort.
<a href="http://algs4.cs.princeton.edu/21elementary">Section 2.1</a>
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
*/
public static class Selection<T>
where T : IComparable<T>
{
/**
* Rearranges the array in ascending order, using the natural order.
* @param a the array to be sorted
*/
public static void Sort(T[] a)
{
int n = a.Length;
for (int i = 0; i < n; i++)
{
int min = i;
for (int j = i + 1; j < n; j++)
{
if (SortingHelper<T>.less(a[j], a[min])) min = j;
}
SortingHelper<T>.exch(a, i, min);
Debug.Assert(SortingHelper<T>.isSorted(a, 0, i));
}
Debug.Assert(SortingHelper<T>.isSorted(a));
}
/**
* Rearranges the array in ascending order, using a comparator.
* @param a the array
* @param comparator the comparator specifying the order
*/
public static void Sort(T[] a, IComparer<T> comparator)
{
int n = a.Length;
for (int i = 0; i < n; i++)
{
int min = i;
for (int j = i + 1; j < n; j++)
{
if (SortingHelper<T>.less(a[j], a[min], comparator)) min = j;
}
SortingHelper<T>.exch(a, i, min);
Debug.Assert(SortingHelper<T>.isSorted(a, 0, i, comparator));
}
Debug.Assert(SortingHelper<T>.isSorted(a, comparator));
}
}
}