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;
    }
}

 

posted @ 2023-06-04 00:52  ascertain  阅读(1)  评论(0编辑  收藏  举报