黑马程序员技术交流社区

标题: 冒泡和选择两种排序。 [打印本页]

作者: gss    时间: 2015-9-18 23:10
标题: 冒泡和选择两种排序。
package sort;
public class BobuleSort {
        public static void bobule(int arr[]) {
                for (int x = 0; x < arr.length; x++) {
                        for (int y = 0; y < arr.length - x - 1; y++) {

                                if (arr[y] > arr[y + 1]) {
                                        int temp = arr[y];
                                        arr[y] = arr[y + 1];
                                        arr[y + 1] = temp;

                                }

                        }

                }

        }

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int arr[] = { 5, 8, 3, 9, 1, 2, 6 };
                bobule(arr);
                for (int x = 0; x < arr.length; x++) {
                        System.out.println(arr[x]);

                }

        }

}

package sort;

public class SelectSort {

        public static void select(int[] arr) {
                for (int x = 0; x < arr.length - 1; x++) {
                        for (int y = x + 1; y < arr.length; y++) {
                                if (arr[x] > arr[y]) {
                                        int temp = arr[x];
                                        arr[x] = arr[y];
                                        arr[y] = temp;
                                }

                        }

                }

        }

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int[] arr = { 2, 5, 3, 9, 5, 7 };
                select(arr);
                for (int x = 0; x < arr.length; x++) {

                        System.out.println(arr[x]);

                }

        }

}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2