首页 > 编程语言 > 详细

冒泡排序、选择排序、插入排序

时间:2017-02-04 18:09:59      阅读:192      评论:0      收藏:0      [点我收藏+]
 1 package com.cn.sortt;
 2 
 3 public class BubbleSort {
 4      //冒泡排序1
 5     public static void bubble1(int[] array){
 6       for (int i = 0 ;i < array.length;i++){
 7          for(int j = 0;j < array.length-1-i;j++ ){
 8            if(array[j] < array[j+1]){
 9                 int temp = array[j];
10                 array[j] = array[j+1];
11                 array[j+1] = temp;
12             }
13           }
14        }
15       for (int i = 0 ;i < array.length;i++){
16              System.out.print(array[i] + " ");
17        }
18     }
19     
20     public static void main(String[] args) {
21         // TODO Auto-generated method stub
22       int[] array = {1,4,3,22,46,7};
23       //bubble1(array);
24       bubble2(array);
25     
26      
27     }
28     
29     // 冒泡排序2
30     public static void bubble2(int[] array){
31           for (int i = 0 ;i < array.length;i++){
32              for(int j = array.length-1;j > i;j-- ){
33                if(array[j] > array[j-1]){
34                     int temp = array[j];
35                     array[j] = array[j-1];
36                     array[j-1] = temp;
37                 }
38               }
39            }
40           for (int i = 0 ;i < array.length;i++){
41                  System.out.print(array[i] + " ");
42            }
43         }
44     
45 }
 1 package com.cn.sortt;
 2 
 3 public class SelectionSort {
 4      //选择排序
 5     public static void select(int[] array){
 6       for (int i = 0 ;i < array.length-1;i++){
 7          for(int j = i+1;j < array.length;j++ ){
 8            if(array[i] < array[j]){
 9                 int temp = array[j];
10                 array[j] = array[i];
11                 array[i] = temp;
12             }
13           }
14        }
15       for (int i = 0 ;i < array.length;i++){
16              System.out.print(array[i] + " ");
17        }
18     }
19     
20     public static void main(String[] args) {
21         // TODO Auto-generated method stub
22      int[] array = {1,4,3,22,46,7};
23      select(array);   
24     }
25 }

 

冒泡排序、选择排序、插入排序

原文:http://www.cnblogs.com/ailsalin/p/6366036.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!