-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplate.java
More file actions
40 lines (37 loc) · 1 KB
/
Copy pathTemplate.java
File metadata and controls
40 lines (37 loc) · 1 KB
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
package com.vollen.sort;
import edu.princeton.cs.algs4.*;
public class Template{
public static void sort(Comparable[] a ){
}
protected static boolean less(Comparable v, Comparable w){
if(v == null){ return true;}
if(w == null){ return false;}
return v.compareTo(w) < 0;
}
protected static void exch(Comparable[] a, int i, int j){
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
protected static void show(Comparable[] a){
for(int i = 0; i < a.length; i++){
StdOut.print(a[i] + " ");
}
StdOut.println();
}
public static boolean isSorted(Comparable[] a){
for(int i = 1; i < a.length; i++){
if(less(a[i], a[i - 1])){
return false;
}
}
return true;
}
public static void main(String[] args) {
In in = new In(args[0]);
String [] a = In.readStrings();
sort(a);
assert(isSorted(a));
show(a);
}
}