Java排序算法代码集(7个)

0.注

以下代码是以前参考视频课程或者其他人的博客写的,现在是复习+做个备份在CSDN上,大部分都忘了为何这样写了,希望各位如果拿某部分代码跑出了问题,可以把用例留个言让我检查反思一下,感激不尽~

1.冒泡排序

public static void bubbleSort(int[] arr){
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr.length - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

2.插入排序

public static void insertionSort(int[] arr){
    for (int i = 1; i < arr.length; i++) {
        for (int j = i; j > 0; j--) {
            if (arr[j] < arr[j - 1]) {
                int temp = arr[j - 1];
                arr[j - 1] = arr[j];
                arr[j] = temp;
            } else break;
        }
    }
}

3.选择排序

public static void selectionSort(int[] arr){
    for (int i = 0; i < arr.length; i++) {
        int min = arr[i];
        int index = i;
        for (int j = i + 1; j < arr.length; j++) {
            if (min > arr[j]) {
                min = arr[j];
                index = j;
            }
        }
        int temp = arr[i];
        arr[i] = min;
        arr[index] = temp;
    }
}

4.快速排序

版本1:

public static int[] quickSort(int[] array, int start, int end) {
    if (start < 0 || end >= array.length || start > end) return null;
    int smallIndex = partition(array, start, end);
    if (smallIndex > start)
        quickSort(array, start, smallIndex - 1);
    if (smallIndex < end)
        quickSort(array, smallIndex + 1, end);
    return array;
}

public static int partition(int[] array, int start, int end) {
    int pivot = (int) (start + Math.random() * (end - start + 1));
    int smallIndex = start - 1;
    swap(array, pivot, end);
    for (int i = start; i <= end; i++)
        if (array[i] <= array[end]) {
            smallIndex++;
            if (i > smallIndex)
                swap(array, i, smallIndex);
        }
    return smallIndex;
}

public static void swap(int[] array, int i, int j) {
    int temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}

版本2:

public static int[] quickSortV2(int[] num, int left, int right) {
    if(left>=right) return num;
    
    int pivot=num[left];
    int i=left;
    int j=right;
    while(i<j){
        while(num[j]>=pivot && i<j){
            j--;
        }
        while(num[i]<=pivot && i<j){
            i++;
        }
        
        if(i<j){
            int temp=num[i];
            num[i]=num[j];
            num[j]=temp;
        }
    }
    
    num[left]=num[i];
    num[i]=pivot;
    quickSortV2(num,left,i-1);
    quickSortV2(num,i+1,right);
    return num;
}

版本3:
来源:Github(校园网抽风打不开,看不到是谁的github里翻出来的代码了,想起来的时候补一下~)

/**
* 三数中值分割法
* @param a 原始数组
* @param left 左边界
* @param right 右边界
* @return 返回枢元
*/
private static <AnyType extends Comparable<? super AnyType>> AnyType median3(AnyType[] a, int left, int right) {
	int center = (left + right) / 2;
	if (a[center].compareTo(a[left]) < 0) {
		swapReferences(a, left, center);
	}
	if (a[right].compareTo(a[left]) < 0) {
		swapReferences(a, left, right);
	}
	if (a[right].compareTo(a[center]) < 0) {
		swapReferences(a, center, right);
	}
	swapReferences(a, center, right - 1);
	return a[right - 1];
}

private static <AnyType extends Comparable<? super AnyType>> void swapReferences(AnyType[] a, int i, int j) {
	AnyType tmp = a[i];
	a[i] = a[j];
	a[j] = tmp;
}

5.归并排序

public static int[] MergeSort(int[] array) {
    if (array.length < 2) return array;
    int mid = array.length / 2;
    int[] left = Arrays.copyOfRange(array, 0, mid);
    int[] right = Arrays.copyOfRange(array, mid, array.length);
    return merge(MergeSort(left), MergeSort(right));
}

public static int[] merge(int[] left, int[] right) {
    int[] result = new int[left.length + right.length];
    int  i = 0, j = 0;
    for (int index = 0; index < result.length; index++) {
        if (i >= left.length)
            result[index] = right[j++];
        else if (j >= right.length)
            result[index] = left[i++];
        else if (left[i] > right[j])
            result[index] = right[j++];
        else
            result[index] = left[i++];
    }
    return result;
}

6.希尔排序

public static void shellSort(int[] arr){
    for (int i = arr.length / 2; i > 0; i /= 2) {
        for (int j = i; j < arr.length; j++) {
            for (int k = j; k > 0  && k - i >= 0; k -= i) {
                if (arr[k] < arr[k - i]) {
                    int temp = arr[k - i];
                    arr[k - i] = arr[k];
                    arr[k] = temp;
                } else break;
            }
        }
    }
}

7.堆排

static int len;

public static int[] HeapSort(int[] array) {
    len = array.length;
    if (len < 1) return array;
    buildMaxHeap(array);
    while (len > 0) {
        swap(array, 0, len - 1);
        len--;
        heapify(array, 0);
    }
    return array;
}

public static void buildMaxHeap(int[] array) {
    for (int i = (len - 1) / 2; i >= 0; i--) {
        heapify(array, i);
    }
}

public static void heapify(int[] array, int i) {
    int mi = i;
    if (i * 2 < len && array[i * 2] > array[mi])
        mi = i * 2;
    if (i * 2 + 1 < len && array[i * 2 + 1] > array[mi])
        mi = i * 2 + 1;
    if (mi != i) {
        swap(array, mi, i);
        heapify(array, mi);
    }
}

你可能感兴趣的:(排序算法,java,算法)