美文网首页
java实现多种排序算法

java实现多种排序算法

作者: Moonsmile | 来源:发表于2017-02-21 20:48 被阅读0次

    冒泡排序

    /**
     * Created by junyi.pc on 2017/1/25.
     */
    import  java.util.Calendar;
    import java.util.Scanner;
    import java.util.Vector;
    
    public class Main {
        public static void main(String[] args){
            int a[]={1,4,5,2,8,4,0,3};
            int len=a.length;
            for(int i=0;i<len-1;i++){
               for(int j=len-1;j>i;j--){
                   if(a[j]<a[j-1]){
                       int temp=a[j-1];
                       a[j-1]=a[j];
                       a[j]=temp;
                   }
               }
            }
            for(int i=0;i<len;i++)
            System.out.print(a[i]+" ");
    
        }
    }
    或者这样效率低的更好记
    /**
     * Created by junyi.pc on 2017/1/25.
     */
    import  java.util.Calendar;
    import java.util.Scanner;
    import java.util.Vector;
    
    public class Main {
        public static void main(String[] args){
            int a[]={1,4,5,2,8,4,0,3};
            int len=a.length;
            for(int i=0;i<len-1;i++){
               for(int j=0;j<len-1;j++){
                   if(a[j]>a[j+1]){
                       int temp=a[j];
                       a[j]=a[j+1];
                       a[j+1]=temp;
                   }
               }
            }
            for(int i=0;i<len;i++)
            System.out.print(a[i]+" ");
    
        }
    }
    
    
    运行结果
    C:\java\jdk1.8.0_25\bin\java -Didea.launcher.port=7540 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\java\jdk1.8.0_25\jre\lib\charsets.jar;C:\java\jdk1.8.0_25\jre\lib\deploy.jar;C:\java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\java\jdk1.8.0_25\jre\lib\javaws.jar;C:\java\jdk1.8.0_25\jre\lib\jce.jar;C:\java\jdk1.8.0_25\jre\lib\jfr.jar;C:\java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\java\jdk1.8.0_25\jre\lib\jsse.jar;C:\java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\java\jdk1.8.0_25\jre\lib\plugin.jar;C:\java\jdk1.8.0_25\jre\lib\resources.jar;C:\java\jdk1.8.0_25\jre\lib\rt.jar;C:\Users\junyi.pc\Desktop\client\out\production\client;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
    0 1 2 3 4 4 5 8 
    Process finished with exit code 0
    

    选择排序

    /**
     * Created by junyi.pc on 2017/1/25.
     */
    
    public class Main {
        public static void main(String[] args){
            int a[]={1,4,5,2,8,4,0,3,10,11};
            int len=a.length;
            for(int i=0;i<len;i++){
                int index=i;
                for(int j=i+1;j<len;j++) {
                    if (a[index] > a[j]) {
                    index=j;//标记一下j的位置,
                    }
                }
                if(index!=i){
                    int temp=a[i];
                    a[i]=a[index];
                    a[index]=temp;
                }
            }
            for(int i=0;i<len;i++)
                System.out.print(a[i]+" ");
        }
    }
    
    

    运行结果

    C:\java\jdk1.8.0_25\bin\java -Didea.launcher.port=7548 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\java\jdk1.8.0_25\jre\lib\charsets.jar;C:\java\jdk1.8.0_25\jre\lib\deploy.jar;C:\java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\java\jdk1.8.0_25\jre\lib\javaws.jar;C:\java\jdk1.8.0_25\jre\lib\jce.jar;C:\java\jdk1.8.0_25\jre\lib\jfr.jar;C:\java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\java\jdk1.8.0_25\jre\lib\jsse.jar;C:\java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\java\jdk1.8.0_25\jre\lib\plugin.jar;C:\java\jdk1.8.0_25\jre\lib\resources.jar;C:\java\jdk1.8.0_25\jre\lib\rt.jar;C:\Users\junyi.pc\Desktop\client\out\production\client;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
    0 1 2 3 4 4 5 8 10 11 
    Process finished with exit code 0
    
    

    插入排序

    /**
     * Created by junyi.pc on 2017/1/25.
     */
    
    public class Main {
        public static void main(String[] args){
            int a[]={1,4,5,2,8,4,0,3,10,11};
            int len=a.length;
            for(int i=1;i<len;i++){
                int j=i-1;
                int k=a[i];
                while(j>-1&&k<a[j]) {//j>-1&&k<a[j]这个顺序千万不能反,否者会报越界错误
                    a[j + 1] = a[j];
                    j--;
                }
                 a[j+1]=k;
            }
            for(int i=0;i<len;i++)
                System.out.print(a[i]+" ");
        }
    }
    
    

    运行结果

    C:\java\jdk1.8.0_25\bin\java -Didea.launcher.port=7552 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\java\jdk1.8.0_25\jre\lib\charsets.jar;C:\java\jdk1.8.0_25\jre\lib\deploy.jar;C:\java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\java\jdk1.8.0_25\jre\lib\javaws.jar;C:\java\jdk1.8.0_25\jre\lib\jce.jar;C:\java\jdk1.8.0_25\jre\lib\jfr.jar;C:\java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\java\jdk1.8.0_25\jre\lib\jsse.jar;C:\java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\java\jdk1.8.0_25\jre\lib\plugin.jar;C:\java\jdk1.8.0_25\jre\lib\resources.jar;C:\java\jdk1.8.0_25\jre\lib\rt.jar;C:\Users\junyi.pc\Desktop\client\out\production\client;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
    0 1 2 3 4 4 5 8 10 11 
    Process finished with exit code 0
    
    

    快速排序

    
    public class QuikSort {
        
        public void quikSort(int[] a,int number){
            if(a==null||number<0){
                System.out.println("input error");
            }
            else{
                quikSort1(a,0,number-1);
            }
            
        }
        public void quikSort1(int [] a,int start,int end){
                    
              if(start>=end){
                  return;
              }
              int i=start;
              int j=end;
              boolean flag=true;
              int value=a[i];
              
              while(i!=j){
                  
                  if(flag){
                      
                      if(value>a[j]){
                          swap(a,i,j);
                          flag=false;
                      }
                      else{
                          j--;
                      }
                      
                  }
                  else{
                      
                      if(value<a[i]){
                          swap(a,i,j);
                          flag=true;
                      }
                      else{
                        i++;
                      }
                  }
              }
              
              print(a);
              quikSort1(a, start, j-1);
              quikSort1(a, start+1, end);
              
              
        }
        
        public void swap(int [] a,int i,int j){
              int temp=a[i];
              a[i]=a[j];
              a[j]=temp;
              
          }
        public void print(int [] a){
            
            for(int i =0;i<a.length;i++){
                System.out.println(a[i]+" ");
            }
            System.out.println(" ");
        }
          
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            QuikSort quikSort=new QuikSort();
            int []a={3,4,7,2,9,1};
            
            quikSort.quikSort(a, 6);
        }
    
    }
    
    
    

    运行结果

    1 
    2 
    3 
    7 
    9 
    4 
     
    1 
    2 
    3 
    7 
    9 
    4 
     
    1 
    2 
    3 
    7 
    9 
    4 
     
    1 
    2 
    3 
    7 
    9 
    4 
     
    1 
    2 
    3 
    4 
    7 
    9 
     
    1 
    2 
    3 
    4 
    7 
    9 
     
    
    

    相关文章

      网友评论

          本文标题:java实现多种排序算法

          本文链接:https://www.haomeiwen.com/subject/lzouwttx.html