欢迎访问宙启技术站
智能推送

各种排序算法的Java实现

发布时间:2023-06-12 16:35:55

排序算法是计算机科学中最基础、最重要的算法之一。不同的排序算法适用于不同的数据结构和数据规模,有些算法优于其他算法。这里将介绍一些常见的排序算法的Java实现。

1.冒泡排序

冒泡排序是一种简单的排序算法。它重复遍历列表,每次比较相邻的两个元素,并将它们进行交换,直到整个列表都有序。

Java代码:

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

2.选择排序

选择排序是一种简单的排序算法。它通过重复找到列表中的最小值,并将其放到排序的列表的末尾。

Java代码:

void selectionSort(int arr[]) {
    int n = arr.length;
    for (int i = 0; i < n-1; i++) {
        int min_idx = i;
        for (int j = i+1; j < n; j++)
            if (arr[j] < arr[min_idx])
                min_idx = j;

        // swap arr[min_idx] and arr[i]
        int temp = arr[min_idx];
        arr[min_idx] = arr[i];
        arr[i] = temp;
    }
}

3.插入排序

插入排序是一种简单的排序算法。它通过重复将未排序的元素插入到已排序的列表中。

Java代码:

void insertionSort(int arr[]) {
    int n = arr.length;
    for (int i = 1; i < n; ++i) {
        int key = arr[i];
        int j = i - 1;

        /* Move elements of arr[0..i-1], that are
        greater than key, to one position ahead
        of their current position */
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

4.快速排序

快速排序是一种高效的排序算法。它通过选择元素作为“pivot”,并将列表划分为两个子列表,一个子列表包含“pivot”之前的所有元素,另一个子列表包含“pivot”之后的所有元素。然后,算法递归地对这两个子列表进行排序。

Java代码:

void quickSort(int arr[], int low, int high) {
    if (low < high) {
        /* pi is partitioning index, arr[p] is now
        at right place */
        int pi = partition(arr, low, high);

        // Recursively sort elements before
        // partition and after partition
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = (low - 1); // index of smaller element
    for (int j = low; j < high; j++) {
        // If current element is smaller than or
        // equal to pivot
        if (arr[j] <= pivot) {
            i++;

            // swap arr[i] and arr[j]
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    // swap arr[i + 1] and arr[high]
    int temp = arr[i + 1];
    arr[i + 1] = arr[high];
    arr[high] = temp;

    return i + 1;
}

5.归并排序

归并排序是一种高效的排序算法。它递归地将列表分成两半,对每个子列表进行排序,然后将它们合并以生成排序的列表。

Java代码:

void mergeSort(int arr[], int l, int r) {
    if (l < r) {
        // Find the middle point
        int m = (l + r) / 2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);

        // Merge the sorted halves
        merge(arr, l, m, r);
    }
}

void merge(int arr[], int l, int m, int r) {
    // Find sizes of two subarrays to be merged
    int n1 = m - l + 1;
    int n2 = r - m;

    /* Create temp arrays */
    int L[] = new int[n1];
    int R[] = new int[n2];

    /*Copy data to temp arrays*/
    for (int i = 0; i < n1; ++i)
        L[i] = arr[l + i];
    for (int j = 0; j < n2; ++j)
        R[j] = arr[m + 1 + j];

    /* Merge the temp arrays */

    // Initial indexes of first and second subarrays
    int i = 0, j = 0;

    // Initial index of merged subarry array
    int k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    /* Copy remaining elements of L[] if any */
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }

    /* Copy remaining elements of R[] if any */
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

总结

以上就是几种常见的排序算法的Java实现。它们各有优缺点,选择合适的排序算法对于排序的效率和速度至关重要。在实际编程中,应根据数据结构和具体应用场景选择合适的算法。