冒泡排序

 1 package com;
 2 
 3 /**
 4  * 
 5  * @author Administrator 冒泡排序
 6  *
 7  *         分析 比如 2,7,3,6,8,9 
                I 2<7 273689
                II 7>3 237689
                III 7>6 236789
                IIII 7<8
236789
               IIIII 8<9 236789
9 */ 10 public class BubleSort { 11 12 public static void main(String[] args) { 13 // 申明一个数组 14 int[] arr = { 2, 4, 1, 5, 3, 7, 6, 9 }; 15 int as[] =sourts(arr); 16 for (int i : as) { 17 System.out.print(i); 18 } 19 } 20 21 public static int[] sourts(int[] arr) { 22 // 首先遍历数组 23 for (int i = 0; i < arr.length - 1; i++) { 24 for (int j = i + 1; j < arr.length; j++) { 25 if (arr[i] > arr[j]) { 26 int temp; 27 temp = arr[i]; 28 arr[i] = arr[j]; 29 arr[j] = temp; 30 } 31 } 32 } 33 return arr; 34 } 35 }

 

posted on 2016-12-31 11:37  老邱2  阅读(106)  评论(0编辑  收藏  举报

导航