码迷,mamicode.com
首页 > 编程语言 > 详细

排序法

时间:2017-10-27 01:21:01      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:i++   port   class   logs   ati   pre   style   void   int   

冒泡法

public class Test {
    public static void main(String[] args) {
        int[] arr = { 9, 5, 6, 3, 8, 2, 1, 4, 7 };
        int temp;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "\t");
        }
    }
}
import java.util.Arrays;
public class Test {
    public static void main(String[] args) {
        int[] arr = { 9, 5, 6, 3, 8, 2, 1, 4, 7 };
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    arr[j] = arr[j] ^ arr[j + 1];
                    arr[j + 1] = arr[j] ^ arr[j + 1];
                    arr[j] = arr[j] ^ arr[j + 1];
                }
            }
        }
        System.err.println(Arrays.toString(arr));
    }
}

 

  

排序法

标签:i++   port   class   logs   ati   pre   style   void   int   

原文地址:http://www.cnblogs.com/superxff/p/7739737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!