算法之冒泡排序
public static void main(String[] args){
//測試數據
int arr[] = {5, 4, 1, 3, 6};
//冒泡排序
bubbleSort(arr, 5);
//打印排序結果
int i;
for(i = 0; i < 5; i++)
System.out.println(arr[i]);
}
static void bubbleSort(int arr[], int count)
{
int i = count, j;
int temp;
while(i > 0)
{
for(j = 0; j < i - 1; j++)
{
if(arr[j] > arr[j + 1])
{ temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
i--;
}
}
//測試數據
int arr[] = {5, 4, 1, 3, 6};
//冒泡排序
bubbleSort(arr, 5);
//打印排序結果
int i;
for(i = 0; i < 5; i++)
System.out.println(arr[i]);
}
static void bubbleSort(int arr[], int count)
{
int i = count, j;
int temp;
while(i > 0)
{
for(j = 0; j < i - 1; j++)
{
if(arr[j] > arr[j + 1])
{ temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
i--;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.