java achieve fast row and heap sort

Fast row Java implementation

Directly to the first element as a dividing element, here is the direct Java code implementation

 public class QuickSort {

        public static void main(String[] args) {
            int[] nums = {17, 8, 100, 120, 122, 6, 3, 9};
            quickSort(nums);
            for (int num : nums) {
                System.out.print(" " + num);
            }
        }

        public static void quickSort(int[] nums) {
            quickSort(nums, 0, nums.length - 1);
        }

        public static void quickSort(int[] nums, int start, int end) {
            if (start >= end) {
                return;
            }
            int low = start;
            int high = end;
            int splitKey = nums[start];
            while (start < end) {
                while (start < end && nums[end] >= splitKey) {
                    end--;
                }
                nums[start] = nums[end];
                while (start < end && nums[start] <= splitKey) {
                    start++;
                }
                nums[end] = nums[start];
            }
            nums[end] = splitKey;
            quickSort(nums, low, end - 1);
            quickSort(nums, end + 1, high);
        }

    }

HEAPSORT Java implementation

In small to large, for example, the exhaust stack includes two processes:
1, the maximum heap constructed (as an array heap data structure)
2, replaces the current top of the stack element and the tail element stack, has been ignored tail orderly stack, re-adjust the heap, repeated (remove top of the heap maximum or minimum value in the process of an orderly queue but it is still constantly get &&)

Heap row is quite simple, video: https: //www.bilibili.com/video/av55290486 from = search & seid = 16303431171103441937?
Directly on the Java implementation code:

    public class HeapSort {
        public static void main(String[] args) {
            int[] nums = {17, 8, 100, 120, 122, 6, 3, 9};
            heapSort(nums);
            for (int num : nums) {
                System.out.print(" " + num);
            }
        }

        public static void heapSort(int[] nums) {
            if (nums == null || nums.length <= 1) {
                return;
            }
            int len = nums.length;
            for (int i = len / 2 - 1; i >= 0; i--) {
                adjustHeap(nums, len, i);
            }
            for (int i = len - 1; i >= 0; i--) {
                swap(nums, i, 0);
                adjustHeap(nums, i, 0);
            }
        }

        public static void adjustHeap(int[] nums, int size, int index) {
            int largest = index;
            int left = index * 2 + 1;
            int right = index * 2 + 2;
            if (left < size && nums[left] > nums[largest]) {
                largest = left;
            }
            if (right < size && nums[right] > nums[largest]) {
                largest = right;
            }
            if (largest != index) {
                swap(nums, largest, index);
                adjustHeap(nums, size, largest);
            }
        }

        public static void swap(int[] a, int i, int j) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
        
    }
Published 54 original articles · won praise 28 · views 4216

Guess you like

Origin blog.csdn.net/qq_37174887/article/details/103148866