Reputation: 147
As I learn Algorithms Book authoried by Robert Sedgewick,when I complete the Selection.java
code , I find that there is no output,I hardly know why.
Below is my code.
import java.util.Comparator;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class Selection {
public static void sort(Comparable[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int min = i;
for (int j = i + 1; j < n; j++)
if (less(a[j], a[min]))
min = j;
exch(a, i, min);
}
}
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
private static boolean less(Comparator comparator, Object v, Object w) {
return comparator.compare(v, w) < 0;
}
private static void exch(Object[] a, int i, int j) {
Object swap = a[i];
a[i] = a[j];
a[j] = swap;
}
/***************************************************************************
* Check if array is sorted - useful for debugging.
***************************************************************************/
// is the array a[] sorted?
private static boolean isSorted(Comparable[] a) {
return isSorted(a, 0, a.length - 1);
}
// is the array sorted from a[lo] to a[hi]
private static boolean isSorted(Comparable[] a, int lo, int hi) {
for (int i = lo + 1; i <= hi; i++)
if (less(a[i], a[i - 1]))
return false;
return true;
}
// is the array a[] sorted?
private static boolean isSorted(Object[] a, Comparator comparator) {
return isSorted(a, comparator, 0, a.length - 1);
}
// is the array sorted from a[lo] to a[hi]
private static boolean isSorted(Object[] a, Comparator comparator, int lo,
int hi) {
for (int i = lo + 1; i <= hi; i++)
if (less(comparator, a[i], a[i - 1]))
return false;
return true;
}
// print array to standard output
private static void show(Comparable[] a) {
for (int i = 0; i < a.length; i++) {
StdOut.println(a[i]);
}
}
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
// Selection.sort(a);
show(a);
}
}
And then is my two test
1.http://algs4.cs.princeton.edu/21elementary/tiny.txt
S O R T E X A M P L E
2.http://algs4.cs.princeton.edu/21elementary/words3.txt
bed bug dad yes zoo
now for tip ilk dim
tag jot sob nob sky
hut men egg few jay
owl joy rap gig wee
was wad fee tap tar
dug jam all bad yet
I expect the increasing order in the output,but there is no output. And I set the Run Configuration--Commman Tab --Input File as ~/tiny.txt But when I apply and run,there is no output(I use Eclipse Mars 4)
Then I guess maybe the parameter or type Comparable,Because there is a lot warning of it,but I can't handle it. Anyone could tell me how to solve the problem:)
Upvotes: 1
Views: 167
Reputation: 10555
edu.princeton.cs.algs4.StdOut
internally uses PrintWriter
initialized as out = new PrintWriter(new OutputStreamWriter(System.out, CHARSET_NAME), true);
. The println
methods just write to the stream, but does not flush. You may either do:
StdOut.print()
at the end of your writing which intern calls out.flush()
StdOut.print(Object o)
method which always calls flush()
Reference: StdOut.java
Upvotes: 1