Programming: Sort
JavaScript
let arr = [8, 4, 3, 2, 6, 7, 1, 5, 9] function quickSort(arr) { console.log(arr) if(arr.length <= 1) return arr let midIndex = parseInt(arr.length / 2) let midValue = arr.splice(midIndex, 1)[0] let left = [], right = [] for(let v of arr) { if(v < midValue) left.push(v) else right.unshift(v) } return quickSort(left).concat(midValue, quickSort(right)) } console.log(quickSort(arr))
Java:
Ovonic Bubble Sort
public class D { public static void main(String[] args) { int[] arr = {8, 4, 3, 7, 5, 1, 6, 9, 2}; int iterCount = 0, swapCount = 0, lowCursor = 0, highCursor = arr.length - 1; boolean flag; while (lowCursor < highCursor) { flag = false; for (int i = lowCursor; i < highCursor; ++i) { iterCount++; if (arr[i] > arr[i + 1]) { swapCount++; flag = true; arr[i] = arr[i] ^ arr[i + 1]; arr[i + 1] = arr[i] ^ arr[i + 1]; arr[i] = arr[i] ^ arr[i + 1]; } } highCursor--; for (int i = highCursor; i > lowCursor; --i) { iterCount++; if (arr[i - 1] > arr[i]) { swapCount++; flag = true; arr[i] = arr[i] + arr[i - 1]; arr[i - 1] = arr[i] - arr[i - 1]; arr[i] = arr[i] - arr[i - 1]; } } lowCursor++; if (!flag) break; } for (int i = 0; i < arr.length; ++i) System.out.print(arr[i] + "\t"); System.out.println(); System.out.printf("iterCount: %d\n", iterCount); System.out.printf("swapCount: %d\n", swapCount); } }
Ovonic bubble sort 略优于one-way bubble sort, swap一样, iter次数少一些
Insertion Sort
public class D { public static void main(String[] args) { int[] arr = {8, 4, 3, 7, 5, 1, 6, 9, 2}; insertSort(arr); System.out.println(Arrays.toString(arr)); } private static void insertSort(int[] array) { int pivot, cursor; for (int i = 1; i < array.length; ++i) { pivot = array[i]; cursor = i - 1; while (cursor >= 0) { if (pivot < array[cursor]) { array[cursor + 1] = array[cursor]; } else { // array[cursor + 1] = pivot; // 如果pivot为极小值, 则并没有进入else, 此步未执行 break; } cursor--; } array[cursor + 1] = pivot; // 1. else中break后进入 2. 未break, while正常结束(此时cursor==-1) } } }
public class D { public static void main(String[] args) { int[] arr = {8, 4, 3, 7, 5, 1, 6, 9, 2}; insertSort(arr); System.out.println(Arrays.toString(arr)); } private static void insertSort(int[] array) { int pivot, cursor; for (int i = 1; i < array.length; ++i) { pivot = array[i]; for (cursor = i - 1; cursor >= 0; --cursor) { if (pivot < array[cursor]) { array[cursor + 1] = array[cursor]; } else { break; } } array[cursor + 1] = pivot; } } }
Single Direction Quick Sort
public class E { public static void main(String[] args) { int[] ints = new int[10]; for (int b = 0; b < ints.length; ++b) { ints[b] = (int) Math.floor(Math.random() * 10) + 1; } System.out.println(Arrays.toString(ints)); quickSort(ints, 0, ints.length - 1); System.out.println(Arrays.toString(ints)); int[][] ints1 = new int[][]{{1, 2}, {3, 4}}; } private static void quickSort(int[] ints, int low, int high) { if (low >= high) { return; } int boundary = partition(ints, low, high); quickSort(ints, low, boundary - 1); quickSort(ints, boundary + 1, high); } private static int partition(int[] ints, int low, int high) { int pivot = ints[high]; int boundary = low; for (int cursor = low; cursor < high; ++cursor) { if (ints[cursor] < pivot) { if (cursor != boundary) { ints[cursor] = ints[cursor] ^ ints[boundary]; ints[boundary] = ints[cursor] ^ ints[boundary]; ints[cursor] = ints[cursor] ^ ints[boundary]; } boundary++; } } if (boundary != high) { ints[boundary] = ints[boundary] + ints[high]; ints[high] = ints[boundary] - ints[high]; ints[boundary] = ints[boundary] - ints[high]; } return boundary; } }
Double Direction Quick Sort
public class Main { public static void main(String[] args) { Random random = new Random(System.currentTimeMillis()); int[] ints = new int[10]; for (int b = 0; b < ints.length; ++b) { ints[b] = random.nextInt(1, 11); } System.out.println("\033[32;7m>>> " + Arrays.toString(ints) + " <<<\033[0m"); quickSort(ints, 0, ints.length - 1); System.out.println("\033[32;7m>>> " + Arrays.toString(ints) + " <<<\033[0m"); } private static void quickSort(int[] ints, int low, int high) { if (low >= high) { return; } int boundary = partition(ints, low, high); quickSort(ints, low, boundary - 1); quickSort(ints, boundary + 1, high); } private static int partition(int[] ints, int low, int high) { int pivot = ints[low]; int lowCursor = low + 1, highCursor = high; while (lowCursor < highCursor) { while (ints[highCursor] > pivot) { highCursor--; } while (lowCursor < highCursor && ints[lowCursor] <= pivot) { lowCursor++; } if (lowCursor < highCursor) { ints[lowCursor] = ints[lowCursor] + ints[highCursor]; ints[highCursor] = ints[lowCursor] - ints[highCursor]; ints[lowCursor] = ints[lowCursor] - ints[highCursor]; } } if (pivot != ints[highCursor]) { ints[low] = ints[low] ^ ints[highCursor]; ints[highCursor] = ints[low] ^ ints[highCursor]; ints[low] = ints[low] ^ ints[highCursor]; } return highCursor; } }
分类:
Programming
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-06-04 Spring: @ComponentScan @ComponentScans
2022-06-04 Web: fetch API