博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常用排序算法二
阅读量:5245 次
发布时间:2019-06-14

本文共 3317 字,大约阅读时间需要 11 分钟。

SELECTION SORT:选择排序算法,每次从未完成排序的部分选出最小的插入未完成排序元素的最前面

代码实现比较好写:

import java.util.*;

public class SelectSort

{
     public static void main(String[] args)
     {
         System.out.println("Hello World!");

        int [] a = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};

        selectionSort(a);

     }

    public static void selectionSort(int [] a)

     {
         int length = a.length;

        //int min = -1;

        int minPos = -1;//记录当前最小的值在数组中的位置

        for(int i = 0; i < length-1; i++)//需要从左开始循环length-1次

         {
             minPos = i;//循环前,将最前面没有排序的值作为最小值记录下来其位置
             for( int j = i + 1; j < length; j++)
             {
                 if(a[minPos] > a[j])//从左到右开始比较还未完成排序的数字,保存最小数字的位置
                 {
                     minPos = j;
                 }
             }
             System.out.print(a[i]+":"+a[minPos]);
             int tem = a[i];//交换最小数字和刚开始排序开始的位置

            a[i] = a[minPos];

             a[minPos] = tem;
             System.out.println(Arrays.toString(a));
         }
     }
}

运行结果:

E:\java\java_test\sortprogram>java SelectSort

Hello World!
3:2[2, 44, 38, 5, 47, 15, 36, 26, 27, 3, 46, 4, 19, 50, 48]
44:3[2, 3, 38, 5, 47, 15, 36, 26, 27, 44, 46, 4, 19, 50, 48]
38:4[2, 3, 4, 5, 47, 15, 36, 26, 27, 44, 46, 38, 19, 50, 48]
5:5[2, 3, 4, 5, 47, 15, 36, 26, 27, 44, 46, 38, 19, 50, 48]
47:15[2, 3, 4, 5, 15, 47, 36, 26, 27, 44, 46, 38, 19, 50, 48]
47:19[2, 3, 4, 5, 15, 19, 36, 26, 27, 44, 46, 38, 47, 50, 48]
36:26[2, 3, 4, 5, 15, 19, 26, 36, 27, 44, 46, 38, 47, 50, 48]
36:27[2, 3, 4, 5, 15, 19, 26, 27, 36, 44, 46, 38, 47, 50, 48]
36:36[2, 3, 4, 5, 15, 19, 26, 27, 36, 44, 46, 38, 47, 50, 48]
44:38[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 46, 44, 47, 50, 48]
46:44[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
46:46[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
47:47[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
50:48[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]

E:\java\java_test\sortprogram>

INSERECTION SORT:插入排序-----代码加了注释,感觉很详细了。

import java.util.*;

public class  InsertSort

{
     public static void main(String[] args)
     {
         System.out.println("Hello World!");
         int [] a = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
         insertionSort(a);
     }

    public static void insertionSort(int a[])

     {
         int length = a.length;

        for(int i = 1; i < length; i++)//从第二个元素开始,第一元素默认以排好

         {
             int insert = a[i];//准备要插入的元素,把要插入元素的位置空出来,为了能移动元素
             for(int j = i-1; j >= 0; j--)//从要插入元素位置往前查,以便确认插入元素要插入的位置
             {
                 if(a[j] > insert)//如果当前元素大于要准备插入的元素,则将该元素往后移动一位
                 {
                     int tem = a[j];
                     a[j] = a[j + 1];
                     a[j+1] = tem;
                 }
                 else//如果当前元素小要准备插入的元素,则将要准备插入的元素插入该元素的后面,同时退出这次循环,开始排下一个元素
                 {
                     int tem1 = a[j+1];
                     a[j+1] = insert;
                     break;
                 }
             }

            System.out.println(Arrays.toString(a));

         }
     }
}

运行结果:

Hello World!

[3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 38, 44, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 38, 44, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 38, 44, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 15, 38, 44, 47, 36, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 15, 36, 38, 44, 47, 26, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 15, 26, 36, 38, 44, 47, 27, 2, 46, 4, 19, 50, 48]
[3, 5, 15, 26, 27, 36, 38, 44, 47, 2, 46, 4, 19, 50, 48]
[2, 3, 5, 15, 26, 27, 36, 38, 44, 47, 46, 4, 19, 50, 48]
[2, 3, 5, 15, 26, 27, 36, 38, 44, 46, 47, 4, 19, 50, 48]
[2, 3, 4, 5, 15, 26, 27, 36, 38, 44, 46, 47, 19, 50, 48]
[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 50, 48]
[2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]

E:\java\java_test\sortprogram>

转载于:https://www.cnblogs.com/xiaochenztx/p/8635536.html

你可能感兴趣的文章
python之decode、encode及codecs模块
查看>>
使用 Apache Pig 处理数据6
查看>>
Hadoop集群内lzo的安装与配置
查看>>
CASS 7.1 和 AutoCAD 2006的安装使用
查看>>
supervisor之启动rabbitmq报错原因
查看>>
Struts2工作原理
查看>>
二 、Quartz 2D 图形上下文栈
查看>>
[Leetcode Week8]Edit Distance
查看>>
针对sl的ICSharpCode.SharpZipLib,只保留zip,gzip的流压缩、解压缩功能
查看>>
ASP.NET 3.5构建Web 2.0门户站点
查看>>
PP tables for production order
查看>>
oam系统安装,windows操作系统注册列表影响系统安装
查看>>
[scrum]2011/9/25-----第五天
查看>>
《人月神话》有感,好书,推荐
查看>>
IE浏览器打开chorme浏览器,如何打开其他浏览器
查看>>
GNU 内联汇编
查看>>
【转】代码中特殊的注释技术——TODO、FIXME和XXX的用处
查看>>
php提交表单校验例子
查看>>
man查看帮助命令
查看>>
【SVM】libsvm-python
查看>>