



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




2
3
client data type sorting library construct callbacks compare sort 4
5
public class Date implements Comparable
private static boolean less(Comparable v, Comparable w) { return (v.compareTo(w) < 0 ); } private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i]; a[i] = a[j]; a[j] = t; }
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; }
13
in final order! 14
15
int min = i; for (int j = i+ 1 ; j < N; j++) if (less(a[j], a[min])) min = j; exch(a, i, min);
16
public class Selection { private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0 ; } private static void exch(Comparable[] a, int i, int j) { Comparable swap = a[i]; a[i] = a[j]; a[j] = swap; } public static void sort(Comparable a[]) { for (int i = 0 ; i < a.length; i++) { int min = i; for (int j = i+ 1 ; j < a.length; j++) if (less(a[j], a[min])) min = j; exch(a, i, min); } } } selection sort a[]
import java.io.File; public class Files { public static void main(String[] args) { File directory = new File(args[ 0 ]); File[] files = directory.listFiles(); Selection.sort(files); for (int i = 0 ; i < files.length; i++) System.out.println(files[i]); } } % java Files. Insertion.class Insertion.java InsertionX.class InsertionX.java Selection.class Selection.java Shell.class Shell.java ShellX.class ShellX.java index.html
19
29
random sorted reverse sorted