java排序算法

------冒泡算法

public class BubbleSort {

    public static void main(String[] args) {
        int[] arr = {64, 34, 25, 12, 22, 11, 90};

        System.out.println("排序前的数组:" + Arrays.toString(arr));

        bubbleSort(arr);

        System.out.println("排序后的数组:" + Arrays.toString(arr));
    }

    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]) {
                    // 交换 arr[j] 和 arr[j+1]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}

在这个示例代码中,我们首先定义了一个包含一些整数的数组。然后我们调用 bubbleSort 方法对数组进行排序。在 bubbleSort 方法中,我们使用两个嵌套的循环来遍历数组,并在每次遍历过程中比较相邻的元素,如果发现顺序不对就交换它们的位置。通过多次这样的遍历和比较,最大的元素会慢慢“浮”到数组的末尾,最终完成排序。

你可以将以上代码复制到你的Java环境中运行,以查看冒泡排序算法对给定数组的排序结果。

------插入算法

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;
        }
    }

    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 6, 2};
        insertionSort(arr);
        System.out.print("排序后的数组:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

在这个示例中,我们定义了一个名为InsertionSort的类,其中包含了一个insertionSort方法来实现插入排序算法。在main方法中,我们创建了一个数组,并对其进行排序,并输出排序后的果。。

------选择算法

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;
        }
    }

    public static void main(String[] args) {
        int[] arr = {64, 25, 12, 22, 11};
        selectionSort(arr);
        System.out.println("Sorted array: ");
        for (int i : arr) {
            System.out.print(i + " ");
        }
    }
}
在上面的示例中,我们定义了一个 selectionSort 方法来实现选择排序算法,并在主方法中对一个整数数组进行排序,然后打印排序后的结果。通过这个示例,您可以了解选择排序的基本实现原理和 Java 语言中的实现方式。

------快排算法

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {5, 3, 7, 2, 8, 4, 6};

        quickSort(arr, 0, arr.length - 1);

        System.out.println("排序后的数组:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }

    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            int pivot = partition(arr, low, high);

            quickSort(arr, low, pivot - 1);
            quickSort(arr, pivot + 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;
    }
}

这段代码首先使用 Random 类生成了一个包含一万个随机数的数组 arr,然后使用 quickSort 方法对这个数组进行排序,并通过 System.currentTimeMillis 方法记录了排序所需的时间。

你可以将以上代码复制到你的Java环境中运行,查看快速排序算法对一万个随机数排序所需的时间。如果你需要测试其他语言实现的快速排序算法,也可以使用类似的方法。

注释:(快排算法更快但是更加复杂)

快速排序(Quicksort)是一种常用的排序算法,它的基本思想是通过将一个数组分割成两个子数组,然后递归地对子数组进行排序。下面是快速排序算法的步骤解析:

选择一个基准元素:从数组中选择一个基准元素,通常是数组中间的元素。

分割:重新排列数组,所有比基准元素小的元素放在基准元素的左边,所有比基准元素大的元素放在基准元素的右边。在这个过程中,基准元素将会找到其最终的位置,这个过程称为分割。

递归排序子数组:递归地对基准元素左边的子数组和右边的子数组进行快速排序。

合并结果:当所有的递归调用都完成后,数组就完成了排序。

总结来说,快速排序的步骤可以概括为:选择基准元素,分割数组,递归排序子数组,最终合并排序结果。

快速排序是一种高效的排序算法,它的平均时间复杂度为O(nlogn),最坏情况下的时间复杂度为O(n^2)。在实际应用中,快速排序通常比其他排序算法更快。

  • 29
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洋葱土豆和香菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值