Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,25 @@
|
|||
import java.util.*;
|
||||
|
||||
public class SortComp1 {
|
||||
public static void main(String[] args) {
|
||||
List<String> items = Arrays.asList("violet", "red", "green", "indigo", "blue", "yellow", "orange");
|
||||
List<String> sortedItems = new ArrayList<>();
|
||||
Comparator<String> interactiveCompare = new Comparator<String>() {
|
||||
int count = 0;
|
||||
Scanner s = new Scanner(System.in);
|
||||
public int compare(String s1, String s2) {
|
||||
System.out.printf("(%d) Is %s <, =, or > %s. Answer -1, 0, or 1: ", ++count, s1, s2);
|
||||
return s.nextInt();
|
||||
}
|
||||
};
|
||||
for (String item : items) {
|
||||
System.out.printf("Inserting '%s' into %s\n", item, sortedItems);
|
||||
int spotToInsert = Collections.binarySearch(sortedItems, item, interactiveCompare);
|
||||
// when item does not equal an element in sortedItems,
|
||||
// it returns bitwise complement of insertion point
|
||||
if (spotToInsert < 0) spotToInsert = ~spotToInsert;
|
||||
sortedItems.add(spotToInsert, item);
|
||||
}
|
||||
System.out.println(sortedItems);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
import java.util.*;
|
||||
|
||||
public class OrderByPair {
|
||||
public static void main(String[] args) {
|
||||
List<String> items = Arrays.asList("violet", "red", "green", "indigo", "blue", "yellow", "orange");
|
||||
Collections.sort(items, new Comparator<String>() {
|
||||
int count = 0;
|
||||
Scanner s = new Scanner(System.in);
|
||||
public int compare(String s1, String s2) {
|
||||
System.out.printf("(%d) Is %s <, =, or > %s. Answer -1, 0, or 1: ", ++count, s1, s2);
|
||||
return s.nextInt();
|
||||
}
|
||||
});
|
||||
System.out.println(items);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue