选择排序

        public static void main(String[] args)
        {
            //System.out.println("Hello World!");
            int[] arr = { 8, 1, 2, 7, 4, 6, 12, 88, 95, 45, 32, 56 };
            SelectSort(arr);
            PrintArr(arr);
        }
 
        public static void SelectSort(int[] arr)
        {
            for (int x = 0; x < arr.length; x++)
            {
                for (int y = x + 1; y < arr.length; y++)
                {
                    if (arr[x] > arr[y])
                    {
                        int temp = arr[y];
                        arr[y] = arr[x];
                        arr[x] = temp;
                    }
                }
            }
        }
        public static void PrintArr(int[] arr)
	    {
		    System.out.print("[");
		    for(int i=0;i<arr.length;i++)
		    {
			    if(i!=arr.length-1)
			    {
				    System.out.print(arr[i]+", ");
			    }
			    else
			    {
				    System.out.print(arr[i]+"]");
			    }
		    }
	    }      
    }





posted @ 2014-08-28 12:40  勇天  阅读(104)  评论(0)    收藏  举报