黑马程序员技术交流社区

标题: 选择冒泡排序源代码 [打印本页]

作者: Neverlandxu    时间: 2015-10-12 20:34
标题: 选择冒泡排序源代码
import java.util.*;

class ArrayDemo4
{
        //遍历数组的功能。
        public static void printArray(int[] arr)
        {
                System.out.print("[");
                for(int x=0; x<arr.length; x++)
                {
                        if(x!=arr.length-1)
                                System.out.print(arr[x]+", ");
                        else
                                System.out.println(arr[x]+"]");
                }
        }
        public static void main(String[] args)
        {
                int[] arr = {34,19,11,109,3,56};



        }

        /*
        冒泡排序。
        */
        public static void bubbleSort(int[] arr)
        {
                for(int x=0; x<arr.length-1; x++)
                {
                        for(int y=0; y<arr.length-1-x; y++)
                        {
                                if(arr[y]>arr[y+1])       
                                {
                                        int temp  = arr[y];
                                        arr[y] = arr[y+1];
                                        arr[y+1] = temp;
                                }
                        }       
                }
        }


        /*
        选择排序。
        */
        public static void selectSort(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;
                                }
                        }
                }
        }









}






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