Java中常用的几个基础排序算法

Java中常用的几个基础排序算法

所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。排序算法,就是如何使得记录按照要求排列的方法。排序算法在很多领域得到相当地重视,尤其是在大量数据的处理方面。一个优秀的算法可以节省大量的资源。在各个领域中考虑到数据的各种限制和规范,要得到一个符合实际的优秀算法,得经过大量的推理和分析。
本文就冒泡排序,直接排序,快速排序简单写了下实现代码。

1、冒泡排序

冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。
它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果顺序(如从大到小、首字母从从Z到A)错误就把他们交换过来。走访元素的工作是重复地进行直到没有相邻元素需要交换,也就是说该元素列已经排序完成。
这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端(升序或降序排列),就如同碳酸饮料中二氧化碳的气泡最终会上浮到顶端一样,故名“冒泡排序”。

public class Bubblesort {
    public static void main(String[] args) {
        int[] num = {12, 1, 3, 4, 6};
        for (int i = 0; i < num.length - 1; i++) {
            for (int j = 0; j < num.length - 1 - i; j++) {
                if (num[j] > num[j + 1]) {
                    int temp = num[j];
                    num[j] = num[j + 1];
                    num[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(num));
    }
}

2、选择排序

选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是:第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后再从剩余的未排序元素中寻找到最小(大)元素,然后放到已排序的序列的末尾。以此类推,直到全部待排序的数据元素的个数为零。选择排序是不稳定的排序方法。

public class Selectionsort {
    public static void main(String[] args) {
        int[] num = {12, 1, 3, 4, 6};
        for (int i = 0; i < num.length - 1; i++) {
            int t = i;
            for (int j = i + 1; j < num.length; j++) {
                if (num[t] > num[j]) {
                    t = j;
                }
            }
            if (t != i) {
                int temp = num[i];
                num[i] = num[t];
                num[t] = temp;
            }
        }
        System.out.println(Arrays.toString(num));
    }
}

3、快速排序

快速排序(Quicksort)是对冒泡排序的一种改进。 [1]
快速排序由C. A. R. Hoare在1960年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
来自百度图片

public class Quicksort {
    public static void main(String[] args) {
        int[] num = {12, 1, 3, 4, 6};
        quicksort(num, 0, num.length - 1);
        System.out.println(Arrays.toString(num));

    }

    public static void quicksort(int arr[], int left, int right) {
        if (left > right) {
            return;
        } else {
            int index = getIndex(arr, left, right);
            //递归排左边
            quicksort(arr, left, index - 1);
            //递归排右边
            quicksort(arr, index + 1, right);
        }
    }

    public static int getIndex(int[] arr, int left, int right) {
        int key = arr[left];
        while (left < right) {
            //右侧比key大right向左,否则交换
            while (arr[right] >= key && left < right) {
                right--;
            }
            arr[left] = arr[right];
            while (arr[left] <= key && left < right) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = key;
        return left;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是几种常见的 Java 版本排序算法的示例代码: 1. 冒泡排序: ``` public static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } ``` 2. 选择排序: ``` public static void selectionSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } } ``` 3. 插入排序: ``` public static void insertionSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } ``` 4. 快速排序: ``` public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } private 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; } ``` 请注意,以上代码仅供参考,您可以根据自己的需求对代 ### 回答2: 这里给你提供一些常见的Java版本的排序算法示例代码。 1. 冒泡排序(Bubble Sort)示例代码: ```java public 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]) { // 交换arr[j]和arr[j+1] int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } ``` 2. 选择排序(Selection Sort)示例代码: ```java public 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; } } // 交换arr[i]和arr[minIndex] int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } ``` 3. 插入排序(Insertion Sort)示例代码: ```java public 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 = j - 1; } arr[j + 1] = key; } } ``` 希望以上的示例代码能帮助你理解Java版本的几个排序算法。请注意,以上示例代码仅为参考,实际应用可能需要对输入数据进行合理的边界处理和优化。 ### 回答3: 以下是Java版本的几个常见排序算法的示例代码: 1. 冒泡排序: ```java public class BubbleSort { public static void bubbleSort(int[] array) { int n = array.length; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (array[j] > array[j+1]) { int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } public static void main(String[] args) { int[] array = {64, 34, 25, 12, 22, 11, 90}; bubbleSort(array); System.out.println("冒泡排序结果:"); for (int i : array) { System.out.print(i + " "); } } } ``` 2. 选择排序: ```java public class SelectionSort { public static void selectionSort(int[] array) { int n = array.length; for (int i = 0; i < n-1; i++) { int minIndex = i; for (int j = i+1; j < n; j++) { if (array[j] < array[minIndex]) { minIndex = j; } } int temp = array[minIndex]; array[minIndex] = array[i]; array[i] = temp; } } public static void main(String[] args) { int[] array = {64, 25, 12, 22, 11}; selectionSort(array); System.out.println("选择排序结果:"); for (int i : array) { System.out.print(i + " "); } } } ``` 3. 插入排序: ```java public class InsertionSort { public static void insertionSort(int[] array) { int n = array.length; for (int i = 1; i < n; ++i) { int key = array[i]; int j = i - 1; while (j >= 0 && array[j] > key) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = key; } } public static void main(String[] args) { int[] array = {12, 11, 13, 5, 6}; insertionSort(array); System.out.println("插入排序结果:"); for (int i : array) { System.out.print(i + " "); } } } ``` 以上示例代码包含了冒泡排序、选择排序和插入排序Java版本实现。你可以将要排序的数组传递给相应的排序函数,然后通过遍历数组打印出排序后的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值