JAVA排序算法之 选择排序

本文详细介绍了选择排序算法的工作原理,通过示例展示了其如何逐步对数组进行排序,并分析了选择排序的时间复杂度和空间复杂度。

1. 选择排序

选择排序的基本思想是遍历数组的过程中,以i代表当前需要排序的序号,则需要在剩余的[i..n-1]中找出其中的最小值,然后将找到的最小值与i指向的值进行交换。因为每一次确定元素的过程中都会有一个选择很大值的子流程,所以称之为选择排序。

比如[38, 17, 16, 16, 7, 31, 39, 32, 2, 11]

i = 0:  [2 , 17, 16, 16, 7, 31, 39, 32, 38 , 11] (0th [38]<->8th [2])

i = 1:  [2, 7 , 16, 16, 17 , 31, 39, 32, 38, 11] (1st [38]<->4th [17])

i = 2:  [2, 7, 11 , 16, 17, 31, 39, 32, 38, 16 ] (2nd [11]<->9th [16])

i = 3:  [2, 7, 11, 16, 17, 31, 39, 32, 38, 16] ( 无需交换 )

i = 4:  [2, 7, 11, 16, 16 , 31, 39, 32, 38, 17 ] (4th [17]<->9th [16])

i = 5:  [2, 7, 11, 16, 16, 17 , 39, 32, 38, 31 ] (5th [31]<->9th [17])

i = 6:  [2, 7, 11, 16, 16, 17, 31 , 32, 38, 39 ] (6th [39]<->9th [31])

i = 7:  [2, 7, 11, 16, 16, 17, 31, 32, 38, 39] ( 无需交换 )

i = 8:  [2, 7, 11, 16, 16, 17, 31, 32, 38, 39] ( 无需交换 )

i = 9:  [2, 7, 11, 16, 16, 17, 31, 32, 38, 39] ( 无需交换 )

选择排序随着排序的进行(i逐渐增大),比较的次数会越来越少,但是不论数组初始是否有序,选择排序都会从i至数组末尾进行一次选择比较,所以给定长度的数组,选择排序的比较次数是固定的:1+2+3+…+n=n*(n+1)/2,而交换的次数则跟初始数组的顺序有关,如果初始数组顺序为随机,则在最坏情况下数组元素将会交换N次,最好的情况是0次。选择排序的时间复杂度和空间复杂度分别为O(n2 ) O(1)

package algorithms;

 

publicabstractclass Sorter<E extends Comparable<E>>  {

    publicabstractvoid sort(E[] array,int from ,int len);

    publicfinalvoid sort(E[] array)

    {

        sort(array,0,array.length);

    }

    protectedfinalvoid swap(E[] array,int from ,int to)

    {

        E tmp=array[from];

        array[from]=array[to];

        array[to]=tmp;

    }

      publicvoid sort(String helloString, int from, int len) {

            // TODO Auto-generated method stub

           

      }

}

package algorithms;

/**

 * @author yovn

 *

 */

publicclass SelectSorter<E extends Comparable<E>> extends Sorter<E> {

 

      /* (non-Javadoc)

     * @see algorithms.Sorter#sort(E[], int, int)

     */

    @Override

    publicvoid sort(E[] array, int from, int len) {

        for(int i=0;i<len;i++)

        {

            int smallest=i;

            int j=i+from;

            for(;j<from+len;j++)

            {

                if(array[j].compareTo(array[smallest])<0)

                {

                    smallest=j;

                }

            }

            swap(array,i,smallest);

                  

        }

 

    }

   

    publicstaticvoid main(String[] args){

      String[] myStringArray = new String[3];

      String[] myStringArray1 = {"a","c","b"};

      String[] myStringArray2 = new String[]{"a","b","c"};

      SelectSorter<String> s1 = new SelectSorter<String>();

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

            System.out.println(myStringArray1[i]);

      }

      s1.sort(myStringArray1, 0, 3);

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

            System.out.println(myStringArray1[i]);

      }

    }

 

}

Output:

a

c

b

a

b

c

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值