选择排序Java实现

排序原理:

        1.第一次遍历的过程,都假定第一个选择的元素是最小的值,和其他索引的值进行比较,如果第一个元素索引处的值大于X处索引值的时候,则认定X处的值为最小值,则交换两处的索引号码(现在最小值的索引就是X),第一次遍历结束就会找到最小值的索引。

        2.交换第一次个索引与最小值所在索引处的值

        3.循环即可,直到排完序

代码实现

import java.util.Arrays;

public class Selection {
        /*
           对数组a中的元素进行排序
        */
        public static void sort(Comparable[] a){
            for(int i = 0;i<=a.length-2;i++){
                //定义一个变量,记录最小元素所在的索引,默认为参与选择排序的第一个元素所在位置
                int mainIndex = i;
                for (int j = i+1;j<a.length;j++){
                    //需要比较最小索引maiIndex处的值与j索引处的值;
                    if(greater(a[mainIndex],a[j])){
                        mainIndex = j;
                    }
                }
                //交换最小元素所在索引minIndex处的值与索引处的值
                exch(a,i,mainIndex);
            }
        }


        /*
            比较v元素是否大于w元素
         */
        private static  boolean greater(Comparable v,Comparable w){
            return v.compareTo(w)>0;
        }


        /*
        数组元素i和j交换位置
         */
        private static void exch(Comparable[] a,int i,int j){
            Comparable temp;
            temp = a[i];
            a[i]=a[j];
            a[j]=temp;
        }

        public static void main(String[] args) {
            //原始数据
            Integer[] a = {4,6,8,7,9,2,10,1};
            Selection.sort(a);  //把数组a传给 sort方法   因为是static修饰 可以直接类名+方法名称调用
            System.out.println(Arrays.toString(a));//{1,2,4,5,7,8,9,10}
        }
}

 需要注意的是:

在greater方法中compareTo是比较两个数大小的方法;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 选择排序Java实现代码如下:public static void selectionSort(int[] array) { for (int i = 0; i < array.length - 1; i++) { int index = i; for (int j = i + 1; j < array.length; j++) { if (array[j] < array[index]) { index = j; } } int smallerNumber = array[index]; array[index] = array[i]; array[i] = smallerNumber; } } ### 回答2: 选择排序(Selection Sort)是一种简单直观的排序算法,它的基本思想是每次从待排序的数据元素中选择最小(或最大)的一个元素作为首元素,放到已排序序列的末尾,直到所有元素排序完毕。 以下是选择排序Java实现代码: ```java 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("排序后的数组:"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } } ``` 这段代码首先定义了一个`selectionSort`方法,接收一个整型数组作为参数。方法中的两层循环用于找到未排序部分的最小元素下标,并将该元素与未排序部分的第一个元素进行交换。通过不断重复这个过程,最终可以将整个数组按照升序排列。 在`main`方法中,我们定义了一个示例数组`arr`,并调用`selectionSort`方法进行排序,最后输出排序后的结果。 以上就是选择排序Java实现代码。 ### 回答3: 选择排序(Selection Sort)是一种简单的排序算法,它的基本思想是每次从待排序的数组中选取最小(或最大)的元素,放到已排序的数组的末尾。下面是使用Java实现选择排序代码示例: ```java 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("排序后的数组:"); for (int value : arr) { System.out.print(value + " "); } } } ``` 以上代码中的`selectionSort`方法使用选择排序对传入的数组进行排序。在每次遍历未排序部分时,内层循环会找出未排序部分的最小元素,并将其与当前未排序部分的第一个元素交换位置。最后,输出排序后的数组。 以上是选择排序Java实现代码。该算法的时间复杂度为O(n^2),其中n是待排序数组的长度。选择排序是一种简单但效率较低的排序算法,在处理小型数据集时可以使用。对于大型数据集,推荐使用更高效的排序算法,如快速排序或归并排序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值