public class BubbleSort
{
/** <简单冒泡排序算法>
* <功能详细描述>
* @param args [参数说明]
*
* @return void [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public static void main(String[] args)
{
int[] beginArray = new int[]{16,17,18,4,9,2,69,45};
int[] endArray = bubbleSort(beginArray);
for(int i=0;i<endArray.length;i++)
{
System.out.print(endArray[i]+" ");
}
}
public static int[] bubbleSort(int[] beginArray)
{
int a;
int length=beginArray.length;
int count=0;
for(int i=0;i<length;i++)
{
for(int j=0;j<length-i-1;j++)
{
if(beginArray[j]>beginArray[j+1])
{
a = beginArray[j];
beginArray[j] = beginArray[j+1];
beginArray[j+1] = a;
}
count++;
}
System.out.println("第"+(i+1)+"次排序结果");
for(int k=0;k<beginArray.length;k++)
{
System.out.print(beginArray[k]+" ");
}
System.out.println();
}
System.out.println("总遍历次数:"+count);
return beginArray;
}
}
{
/** <简单冒泡排序算法>
* <功能详细描述>
* @param args [参数说明]
*
* @return void [返回类型说明]
* @exception throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public static void main(String[] args)
{
int[] beginArray = new int[]{16,17,18,4,9,2,69,45};
int[] endArray = bubbleSort(beginArray);
for(int i=0;i<endArray.length;i++)
{
System.out.print(endArray[i]+" ");
}
}
public static int[] bubbleSort(int[] beginArray)
{
int a;
int length=beginArray.length;
int count=0;
for(int i=0;i<length;i++)
{
for(int j=0;j<length-i-1;j++)
{
if(beginArray[j]>beginArray[j+1])
{
a = beginArray[j];
beginArray[j] = beginArray[j+1];
beginArray[j+1] = a;
}
count++;
}
System.out.println("第"+(i+1)+"次排序结果");
for(int k=0;k<beginArray.length;k++)
{
System.out.print(beginArray[k]+" ");
}
System.out.println();
}
System.out.println("总遍历次数:"+count);
return beginArray;
}
}