Java复习笔记——基础排序

排序算法

1.选择排序

选择排序是一种简单直观的排序算法。

它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。

public class SelectionSort {
    public static void main(String[] args) {
        int[] arr = {64, 25, 12, 22, 11};
        selectionSort(arr);
        System.out.println("Sorted array");
        for (int i=0; i<arr.length; ++i)
            System.out.print(arr[i]+" ");
    }

    // Selection sort function
    public static void selectionSort(int arr[]) {
        int n = arr.length;

        // One by one move boundary of unsorted subarray
        for (int i = 0; i < n-1; i++) {
            // Find the minimum element in unsorted array
            int min_idx = i;
            for (int j = i+1; j < n; j++)
                if (arr[j] < arr[min_idx])
                    min_idx = j;

            // Swap the found minimum element with the first element of unsorted array
            int temp = arr[min_idx];
            arr[min_idx] = arr[i];
            arr[i] = temp;
        }
    }
}

在这个示例中,我们首先定义了一个需要排序的数组。

然后,我们调用selectionSort函数来对这个数组进行排序。在selectionSort函数中,我们首先对数组进行遍历,每次遍历都找出当前未排序部分的最小值,然后将这个最小值与未排序部分的第一个元素进行交换。

这样,每次遍历后,我们就可以确保未排序部分的第一个元素是当前所有未排序元素中的最小值,因此,经过n-1次遍历后(n是数组的长度),整个数组就被排序了。

运行结果:

2.冒泡排序

冒泡排序是一种简单的排序算法,它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。

遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。

public class maopao {
    public static void main(String[] args) {
        int[] arr = {64, 34, 25, 12, 22, 11, 90};
        bubbleSort(arr);
        System.out.println("Sorted array is");
        printArray(arr);
    }

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

    static void printArray(int[] arr) {
        int n = arr.length;
        for (int i=0; i<n; ++i)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

在这个示例中,我们首先定义了一个需要排序的数组。

然后,我们调用bubbleSort函数来对这个数组进行排序。在bubbleSort函数中,我们使用了两个嵌套的for循环。外层循环控制遍历次数,内层循环负责比较并交换元素。如果某个元素大于它后面的元素,我们就交换这两个元素的位置。这样,每一次外层循环结束后,最大的元素就会被放到数组的末尾。

因此,通过重复这个过程,我们就可以对整个数组进行排序。

运行结果:

3.插入排序

插入排序是一种简单直观的排序算法,它的基本思想是将一个记录插入到已经排好序的有序表中,从而得到一个新的、记录数增加1的有序表。

具体实现时,我们从第二个元素开始,将其与前面已经排好序的元素进行比较,找到合适的位置插入即可。

public class InsertionSort {
    /*Function to sort array using insertion sort*/
    void sort(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;
        }
    }

    /* A utility function to print array of size n*/
    static void printArray(int arr[]) {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }

    // Driver method
    public static void main(String args[]) {
        int arr[] = {12, 11, 13, 5, 6};

        InsertionSort ob = new InsertionSort();
        ob.sort(arr);

        printArray(arr);
    }
}

在这个示例当中,我们首先创建一个插入排序的对象,然后使用该对象的sort方法对数组进行排序。排序后的数组通过printArray方法打印出来。

在main方法中,我们创建了一个需要排序的数组,并调用sort方法进行排序。

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值