zoukankan      html  css  js  c++  java
  • 常用Java排序算法

    常用Java排序算法

    冒泡排序 、选择排序、快速排序

     1 package com.javaee.corejava;
     2 
     3 public class DataSort {
     4 
     5     public DataSort() {
     6         // TODO Auto-generated constructor stub
     7     }
     8 
     9     public static void main(String[] args) {
    10         int[] p = { 34, 21, 54, 18, 23, 76, 38, 98, 45, 33, 27, 51, 11, 20, 79,  
    11                 30, 89, 41 };  
    12   
    13         long start = System.currentTimeMillis();  
    14   
    15         DataSort.bubbleSort(p);// 冒泡排序  
    16         //DataSort.selectSort1(p);// 选择排序 1 
    17         //DataSort.selectSort2(p);// 选择排序2  
    18         //DataSort.quickSort(p, 0, p.length - 1);// 快速排序  
    19   
    20         System.out.println("所用时间:" + (System.currentTimeMillis() - start));  
    21         for (int i = 0; i < p.length; i++) {  
    22             System.out.print(p[i] + " ");  
    23         }  
    24 
    25     }
    26     //冒泡排序
    27     public static void bubbleSort(int[] data){
    28         for(int i=0;i<data.length-1;i++){
    29             for(int j=0;j<data.length-i-1;j++){
    30                 if(data[j]>data[j+1]){
    31                     int temp=data[j];
    32                     data[j]=data[j+1];
    33                     data[j+1]=temp;
    34                 }
    35             }
    36         }
    37     }
    38     //选择排序1
    39     public static void selectSort1(int[] data){
    40         if(data==null||data.length==0){
    41             return;
    42         }
    43         for(int i=0;i<data.length-1;i++){
    44             int min=i;
    45             for(int j=i+1;j<data.length;j++){
    46                 if(data[j]<data[min]){
    47                     min=j;
    48                 }
    49             }
    50             if(i!=min){
    51                 int temp=data[i];
    52                 data[i]=data[min];
    53                 data[min]=temp;
    54             }
    55         }
    56     }
    57     //选择排序2
    58     public static void selectSort2(int[] data){
    59         for(int i=0;i<data.length-1;i++){
    60             for(int j=i+1;j<data.length;j++){
    61                 if(data[i]>data[j]){
    62                     int temp=data[i];
    63                     data[i]=data[j];
    64                     data[j]=temp;
    65                 }
    66             }
    67         }
    68     }
    69     //快速排序
    70     public static void quickSort(int[] data,int start,int end){
    71         int key=data[start];
    72         int i=start;
    73         int j=end;
    74         while(i<j){
    75             while(data[j]>key&&i<j){
    76                 j--;
    77             }
    78             data[i]=data[j];
    79             while(data[i]<key&&i<j){
    80                 i++;
    81             }
    82             data[j]=data[i];
    83         }
    84         data[i]=key;
    85         if(i-1>start){
    86             quickSort(data,start,i-1);
    87         }
    88         if(i+1<end){
    89             quickSort(data,i+1,end);
    90         }
    91     }
    92 
    93 }
     
  • 相关阅读:
    IOC基础
    spring ioc原理
    spring ioc原理
    2014年度辛星css教程夏季版第一节
    2014年度辛星html教程夏季版第八节
    2014年度辛星html教程夏季版第七节
    2014年度辛星html教程夏季版第六节
    2014年度辛星html教程夏季版第五节
    2014年度辛星html教程夏季版第四节
    2014年度辛星html教程夏季版第三节
  • 原文地址:https://www.cnblogs.com/miaoyf/p/4060953.html
Copyright © 2011-2022 走看看