zoukankan      html  css  js  c++  java
  • java两种排序方法

    public class Select_Sort {
        public static void main(String[] args)
        {
            int[] score = new int[5];
            Scanner scan = new Scanner(System.in);
            System.out.println("请填写5个要选择排序的数字:");
            for(int i = 0 ; i<score.length;i++)
            {
                System.out.print(i+1+".");
                score[i] = scan.nextInt();
                
            }
            
            for(int i = 0;i<score.length;i++)
            {
                int index=i;
                for(int k=i+1;k<score.length;k++)
                {
                    if(score[k]<score[index])
                        index=k;
                }
                int temp=score[i];
                score[i]=score[index];
                score[index]=temp;
                
                System.out.print("第"+(i+1)+"次选择排序的结果:");
                for(int arr:score)
                    System.out.print(arr+" ");
                System.out.println();
                
            }
            
        }
    }
    请填写5个要选择排序的数字:
    1.45
    2.1
    3.5
    4.65
    5.66
    第1次选择排序的结果:1 45 5 65 66 
    第2次选择排序的结果:1 5 45 65 66 
    第3次选择排序的结果:1 5 45 65 66 
    第4次选择排序的结果:1 5 45 65 66 
    第5次选择排序的结果:1 5 45 65 66 

    1,Bubble_Sort(冒泡排序)

     1 public class Bubble_Sort {
     2     public static void main(String[] args)
     3     {
     4         int[] score = new int[5];
     5         Scanner scan = new Scanner(System.in);
     6         System.out.println("请填写5个要冒泡排序的数字:");
     7         for(int i = 0 ; i<score.length;i++)
     8         {
     9             System.out.print(i+1+".");
    10             score[i] = scan.nextInt();
    11             
    12         }
    13         
    14         for(int i = 0;i<score.length-1;i++)
    15         {
    16             for(int j = 0;j<score.length-i-1;j++)
    17             {
    18                 if(score[j]<score[j+1])
    19                 {
    20                     //这里是一个小技巧,不借助第三者来交换数据
    21                     score[j]=score[j+1]+score[j];
    22                     score[j+1]=score[j]-score[j+1];
    23                     score[j]=score[j]-score[j+1];
    24                 }
    25             }
    26             System.out.print("第"+(i+1)+"次冒泡排序的结果:");
    27             for(int arr:score)
    28                 System.out.print(arr+" ");
    29             System.out.println();
    30         }
    31     }
    请填写5个要冒泡排序的数字:
    1.45
    2.12
    3.89
    4.32
    5.-7
    第1次冒泡排序的结果:45 89 32 12 -7 
    第2次冒泡排序的结果:89 45 32 12 -7 
    第3次冒泡排序的结果:89 45 32 12 -7 
    第4次冒泡排序的结果:89 45 32 12 -7 

    2,Inseretion_Sort(插入排序)

    TIPS:插入排序基本思想是把要排序的数组分成有序和无序两组,每次从无序部分分出一个元素插入到有序部分的合适部分。

     1 public class Inseretion_Sort {
     2     public static void main(String[] args){
     3         int[] score = new int[5];
     4         Scanner scan = new Scanner(System.in);
     5         System.out.println("请填写5个要插入排序的数字:");
     6         for(int i = 0 ; i<score.length;i++)
     7         {
     8             System.out.print(i+1+".");
     9             score[i] = scan.nextInt();
    10             
    11         }
    12         
    13         for(int i=1;i<score.length;i++)
    14         {
    15             for(int j=i;j>0;j--)
    16             {
    17                 if(score[j]>score[j-1])
    18                 {
    19                     //这里是一个小技巧,不借助第三者来交换数据
    20                     score[j]=score[j-1]+score[j];
    21                     score[j-1]=score[j]-score[j-1];
    22                     score[j]=score[j]-score[j-1];
    23                 }
    24             }
    25             System.out.print("第"+i+"次插入排序");
    26             for(int arr:score)
    27                 System.out.print(arr+" ");
    28             System.out.println();
    29         }
    30     }
    31 }
    请填写5个要插入排序的数字:
    1.545
    2.54
    3.0
    4.-46
    5.989
    第1次插入排序545 54 0 -46 989 
    第2次插入排序545 54 0 -46 989 
    第3次插入排序545 54 0 -46 989 
    第4次插入排序989 545 54 0 -46 
  • 相关阅读:
    ASP.NET 缓存 @ OutputCache
    转:Windows下JDK1.6.0+Tomcat6.0的安装配置
    bit、byte、位、字节、汉字、字符之间的区别
    JDK1.6 tomcat 6.0环境变量配置
    DataList和Repeater里的自定义button控件的使用
    Button控件的onclick、oncommand、commandname、CommandArgument的区别
    GridView里的Button控件用法
    ASP.NET2.0 文本编辑器FCKeditor用法
    应用系统架构设计补全篇
    java.util.vector中的vector的详细用法
  • 原文地址:https://www.cnblogs.com/speaklessdomore/p/3393965.html
Copyright © 2011-2022 走看看