十种排序算法(Java版本)

本文介绍了Java中常用的几种基础排序算法,包括冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序、桶排序、基数排序以及计数排序,详细展示了它们的实现代码和工作原理。
摘要由CSDN通过智能技术生成

一,冒泡排序(Bubble Sort):

public class BubbleSort {

    public static 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]) {

                    int temp = arr[j];

                    arr[j] = arr[j + 1];

                    arr[j + 1] = temp;

                }

            }

        }

    }

}

二,选择排序(Selection Sort):

public class SelectionSort {

    public static void selectionSort(int[] arr) {

        int n = arr.length;

        for (int i = 0; i < n - 1; i++) {

            int minIndex = i;

            for (int j = i + 1; j < n; j++) {

                if (arr[j] < arr[minIndex]) {

                    minIndex = j;

                }

            }

            int temp = arr[minIndex];

            arr[minIndex] = arr[i];

            arr[i] = temp;

        }

    }

}

三,插入排序(Insertion Sort):

public class InsertionSort {

    public static void insertionSort(int[] arr) {

        int n = arr.length;

        for (int i = 1; i < n; i++) {

            int key = arr[i];

            int j = i - 1;

            while (j >= 0 && arr[j] > key) {

                arr[j + 1] = arr[j];

                j--;

            }

            arr[j + 1] = key;

        }

    }

}

四,希尔排序(Shell Sort):

public class ShellSort {

    public static void shellSort(int[] arr) {

        int n = arr.length;

        for (int gap = n / 2; gap > 0; gap /= 2) {

            for (int i = gap; i < n; i++) {

                int temp = arr[i];

                int j = i;

                while (j >= gap && arr[j - gap] > temp) {

                    arr[j] = arr[j - gap];

                    j -= gap;

                }

                arr[j] = temp;

            }

        }

    }

}

五,归并排序(Merge Sort):

public class MergeSort {

    public static void mergeSort(int[] arr, int left, int right) {

        if (left < right) {

            int mid = (left + right) / 2;

            mergeSort(arr, left, mid);

            mergeSort(arr, mid + 1, right);

            merge(arr, left, mid, right);

        }

    }

 

    public static void merge(int[] arr, int left, int mid, int right) {

        int n1 = mid - left + 1;

        int n2 = right - mid;

        int[] L = new int[n1];

        int[] R = new int[n2];

        for (int i = 0; i < n1; i++) {

            L[i] = arr[left + i];

        }

        for (int j = 0; j < n2; j++) {

            R[j] = arr[mid + 1 + j];

        }

        int i = 0, j = 0, k = left;

        while (i < n1 && j < n2) {

            if (L[i] <= R[j]) {

                arr[k] = L[i];

                i++;

            } else {

                arr[k] = R[j];

                j++;

            }

            k++;

        }

        while (i < n1) {

            arr[k] = L[i];

            i++;

            k++;

        }

        while (j < n2) {

            arr[k] = R[j];

            j++;

            k++;

        }

    }

}

六,快速排序(Quick Sort):

public class QuickSort {

    public static void quickSort(int[] arr, int low, int high) {

        if (low < high) {

            int pi = partition(arr, low, high);

            quickSort(arr, low, pi - 1);

            quickSort(arr, pi + 1, high);

        }

    }

 

    public static int partition(int[] arr, int low, int high) {

        int pivot = arr[high];

        int i = low - 1;

        for (int j = low; j < high; j++) {

            if (arr[j] < pivot) {

                i++;

                int temp = arr[i];

                arr[i] = arr[j];

                arr[j] = temp;

            }

        }

        int temp = arr[i + 1];

        arr[i + 1] = arr[high];

        arr[high] = temp;

        return i + 1;

    }

}

七,堆排序(Heap Sort):

public class HeapSort {

    public static void heapSort(int[] arr) {

        int n = arr.length;

        for (int i = n / 2 - 1; i >= 0; i--) {

            heapify(arr, n, i);

        }

        for (int i = n - 1; i > 0; i--) {

            int temp = arr[0];

            arr[0] = arr[i];

            arr[i] = temp;

            heapify(arr, i, 0);

        }

    }

 

    public static void heapify(int[] arr, int n, int i) {

        int largest = i;

        int left = 2 * i + 1;

        int right = 2 * i + 2;

        if (left < n && arr[left] > arr[largest]) {

            largest = left;

        }

        if (right < n && arr[right] > arr[largest]) {

            largest = right;

        }

        if (largest != i) {

            int temp = arr[i];

            arr[i] = arr[largest];

            arr[largest] = temp;

            heapify(arr, n, largest);

        }

    }

}

八,桶排序(Bucket Sort):

public void bucketSort(int[] arr, int bucketSize) {

    int max = Arrays.stream(arr).max().getAsInt();

    int min = Arrays.stream(arr).min().getAsInt();

    int bucketCount = (max - min) / bucketSize + 1;

    List<List<Integer>> buckets = new ArrayList<>(bucketCount);

    for (int i = 0; i < bucketCount; i++) {

        buckets.add(new ArrayList<>());

    }

 

    for (int num : arr) {

        int bucketIndex = (num - min) / bucketSize;

        buckets.get(bucketIndex).add(num);

    }

 

    int index = 0;

    for (List<Integer> bucket : buckets) {

        Collections.sort(bucket);

        for (int num : bucket) {

            arr[index++] = num;

        }

    }

}

九,基数排序(Radix Sort):

public void radixSort(int[] arr) {

    int max = Arrays.stream(arr).max().getAsInt();

    for (int exp = 1; max/exp > 0; exp *= 10) {

        countingSortByDigit(arr, exp);

    }

}

 

public void countingSortByDigit(int[] arr, int exp) {

    int n = arr.length;

    int[] output = new int[n];

    int[] count = new int[10];

    Arrays.fill(count, 0);

 

    for (int i = 0; i < n; i++) {

        count[(arr[i]/exp)%10]++;

    }

 

    for (int i = 1; i < 10; i++) {

        count[i] += count[i-1];

    }

 

    for (int i = n-1; i >= 0; i--) {

        output[count[(arr[i]/exp)%10]-1] = arr[i];

        count[(arr[i]/exp)%10]--;

    }

 

    for (int i = 0; i < n; i++) {

        arr[i] = output[i];

    }

}

十,计数排序(Counting Sort):

public void countingSort(int[] arr) {

    int n = arr.length;

    int max = Arrays.stream(arr).max().getAsInt();

    int[] count = new int[max+1];

    int[] output = new int[n];

 

    for (int i = 0; i < n; i++) {

        count[arr[i]]++;

    }

 

    for (int i = 1; i <= max; i++) {

        count[i] += count[i-1];

    }

 

    for (int i = n-1; i >= 0; i--) {

        output[count[arr[i]]-1] = arr[i];

        count[arr[i]]--;

    }

 

    for (int i = 0; i < n; i++) {

        arr[i] = output[i];

    }

}

 

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值