//冒泡排序法
package com.test;
public class Sort {
public static void main(String[] args)
{
int[] array = new int[]{2,4,2,5,3,67,4};
outPut(array);
bubbingAscending(array);
bubbingDscending(array);
}
/**
* 输出数组
* @param array
*/
public static void outPut(int[] array)
{
for (int i = 0; i < array.length; ++i)
{
System.out.print(array[i]+ " ");
}
System.out.println();
}
/**
* 冒泡升序排序
* @param array
*/
public static void bubbingAscending(int[] array)
{
for (int i = 1; i < array.length; ++i)
{
for (int j = 0; j < array.length - i; ++j)
{
if (array[j] > array[j + 1])
{
int temp;
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
System.out.println("冒泡升序排序后的结果为");
outPut(array);
}
public static void bubbingDscending(int[] array)
{
for (int i = 1; i < array.length; ++i)
{
for (int j = 0; j < array.length - i; ++j)
{
if (array[j] < array[j + 1])
{
int temp;
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
System.out.println("冒泡降序排序后的结果为");
outPut(array);
}
}
冒泡排序法
最新推荐文章于 2022-11-05 09:46:56 发布