public class BubbleSort
{
public void bubbleSort(int[] data, boolean isDesc)
{
for(int i = 0; i < data.length - 2; i++)
{
boolean flag = false;
for(int j = 0; j < data.length - 1 -i; j++)
{
if((isDesc == false) && (data[j] > data[j+1]))
{
swap(data, j, j+1);
flag = true;
}
if(((isDesc == true) && (data[j] < data[j+1])))
{
swap(data, j, j+1);
flag = true;
}
}
if(flag == false)
{
return;
}
}
}
private void swap(int[] data, int m, int n)
{
int temp = data[m];
data[m] = data[n];
data[n] = temp;
}
}
Bubble Sort
最新推荐文章于 2023-12-09 21:11:44 发布