admin 管理员组

文章数量: 887031


2024年2月6日发(作者:paddington sydney 邮编)

} } Suite suite; //1~13 int point; public Card(int suiteValue, int point) { = f(suiteValue); = point; } public String toString() { String strPoint = ng(point); if (point > 10) { switch (point) { case 11: strPoint = "J"; break; case 12: strPoint = "Q"; break; case 13: strPoint = "K"; break; } } return () + ":" + strPoint; } public int getScore() { return * 100 + point; } } public static List createCardList(int suiteCount) { List cards = new ArrayList<>(52); for(int i = 1; i < 5; i++) { for(int j = 1; j < 14 ;++j) { (new Card(i, j)); } } List totalCards = new ArrayList<>(suiteCount ); for(int j = 0; j < suiteCount; j++) { (new ArrayList<>(cards)); } e(totalCards); return totalCards; } public static class CardComparator implements Comparator { @Override public int compare(Card o1, Card o2) { return re() - re(); } }

}各种排序算法的实现如下package lgorithm;import tch;import 4j;import tions;import ator;import ;import le;import Integer;import tors;import static ECONDS;@Slf4jpublic class SortCardTask implements Callable { public enum SortMethod {

BUBBLE_SORT,

INSERT_SORT,

QUICK_SORT,

MERGE_SORT,

TIM_SORT} private final List<> cards; private final SortMethod sortMethod; private final int taskNumber; private final AtomicInteger taskCounter; public SortCardTask(List<> cards, SortMethod method, int taskNumber, AtomicInteger taskCounter) { = cards; thod = method; mber = taskNumber; unter = taskCounter; } @Override public Long call() { Stopwatch stopwatch = Started(); ("* {} begin to sort {} cards ({} suite) by {}", mber, (), ()/52, sortMethod); Comparator<> comparator = new mparator(); switch(sortMethod) { case BUBBLE_SORT: bubbleSort(cards, comparator); break; case INSERT_SORT: insertSort(cards, comparator); break; case QUICK_SORT: quickSort(cards, comparator); break; case MERGE_SORT: mergeSort(cards, comparator); break; case TIM_SORT: timSort(cards, comparator); break; }

} (); long millis = d(MILLISECONDS); entAndGet(); return millis; } public static void bubbleSort(List aList, Comparator comparator) { boolean sorted = false; int loopCount = () - 1; while (!sorted) { sorted = true; for (int i = 0; i < loopCount; i++) { if (e((i), (i + 1)) > 0) { (aList, i, i + 1); sorted = false; } } } } public static void insertSort(List aList, Comparator comparator) { int size = (); for (int i = 1; i < size; ++i) { T selected = (i); if (size < 10) { ("{} insert to {}", selected, t(0, i).stream().map(String::valueOf).collect(g(", "))); } int j = i - 1; //find a position for insert currentElement in the left sorted collection while (j >= 0 && e(selected, (j)) < 0) { //it does not overwrite existed element because the j+1=i that is currentElement at beginging (j + 1, (j)); j--; } (j + 1, selected); } } public static void timSort(List aList, Comparator comparator) { //elStream().sorted(comparator).collect(()); (comparator); } public static void quickSort(List aList, Comparator comparator) { T[] arr = (T[]) y(); quickSort(arr, comparator, 0, () - 1); } public static void quickSort(T[] arr, Comparator comparator, int begin, int end) { if (begin < end) { int partitionIndex = partition(arr, comparator, begin, end); quickSort(arr, comparator, begin, partitionIndex-1); quickSort(arr, comparator, partitionIndex+1, end); } } private static int partition(T[] arr, Comparator comparator, int begin, int end) { ("* {} end to sort {} cards ({} suite)sort by {} spend {} milliseconds - {}" , mber, (), ()/52, sortMethod, millis, stopwatch)

T pivot = arr[end]; int i = (begin-1); for (int j = begin; j < end; j++) { if (e(arr[j], pivot) <= 0) { //arr[j] <= pivot i++; T swapTemp = arr[i]; arr[i] = arr[j]; arr[j] = swapTemp; } } T swapTemp = arr[i+1]; arr[i+1] = arr[end]; arr[end] = swapTemp; return i+1; } public static void mergeSort(List aList, Comparator comparator) { T[] arr = (T[]) y(); mergeSort(arr, 0, () - 1, comparator); } public static void mergeSort(T arr[], int l, int r, Comparator comparator) { if (l < r) { // Same as (l+r)/2, but avoids overflow for // large l and h int m = l + (r - l) / 2; // Sort first and second halves mergeSort(arr, l, m, comparator); mergeSort(arr, m + 1, r, comparator); merge(arr, l, m, r, comparator); } } public static void merge(T arr[], int l, int m, int r, Comparator comparator) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; /* create temp arrays */ T[] L = (T[])new Object[n1]; T[] R = (T[])new Object[n2]; /* Copy data to temp arrays L[] and R[] */ for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; /* Merge the temp arrays back into ]*/ i = 0; // Initial index of first subarray j = 0; // Initial index of second subarray k = l; // Initial index of merged subarray while (i < n1 && j < n2) { if (e(L[i], R[j]) <= 0) { arr[k] = L[i]; i++; }

import ator;import ;import Utils;import ;public class TimSortDemo { public static void sortMessageError(List list) { n("this is error comparator"); (list, new Comparator() { public int compare(String arg1, String arg2) { if (k(arg1) || k(arg2)) { return 0; } return eTo(arg2); } }); } public static void sortMessageCorrect(List list) { n("this is correct comparator"); (list, new Comparator() { public int compare(String arg1, String arg2) { if(k(arg1) ) { if(k(arg2)) { return 0; }else { return -1; } }else if(k(arg2)){ return 1; } return eTo(arg2); } }); } public static List createList() { List list = new ArrayList(); Random random = new Random(); for (int i = 10000; i > 0; i--) { if (i % 5000 != 0) { (t(1000) + ""); } else { (""); } } return list; } public static void main(String[] args) throws InterruptedException { sortMessageCorrect(createList()); sortMessageError(createList()); }

}}


本文标签: 算法 排序 实现