[トップに置く]データ構造とアルゴリズムのバブルソート効率分析とjava実現を一歩一歩学習する
バブルソート効率分析とjava実現:
public class BubbleSort {
/**
* : ,
* : n(n-1)/2 O(1)
*
*
* , , , ,
* ,
* @param array
*/
public int[] bubbleSort(int[] array)
{
int i,j,temp,len=array.length;
boolean flag=true;
for(i=0;i<len&&flag;i++)
{
flag = false; // true, ,
for(j=len-1;j>i;j--)
{
if(array[j-1]>array[j]) //
{
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
flag = true;
}
}
}
return array;
}
/***
*
*/
public void print(int[] array )
{
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]+",");
}
}
public static void main(String[] args) {
BubbleSort bs = new BubbleSort();
int[] array = {1,5,9,3,4,18,7,6};
bs.print(bs.bubbleSort(array));
}
}