标签:
——from me
public class BubbleSort{
public static int[] sortArray(int[] nums){
for(int i=0;i<nums.length;i++){
for(int j=i+i;j<nums.length;j++){
if(nums[i]>nums[j]){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
}
return nums;
}
public static void main(String[] args)
{
int[] nums=sortArray(new int[]{1,6,3,9,8,2,4,7,5});
for(int i=0;i<nums.length;i++)
System.out.print(nums[i]);
}
}
——from books
public class BubbleSort {
public void sortArray() {
int arr[] = {2, 1, 5, 8, 21, 12};
System.out.print("before sort:");
for (int i = 0; i < arr.length; i++) {
System.out.print(" " + arr[i]);
for (int j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("\nsorted:");
for (int i = 0; i < arr.length; i++)
System.out.print(" " + arr[i]);
}
public static void main(String[] args) {
BubbleSort p = new BubbleSort();
p.sortArray();
}
}
标签:
原文地址:http://www.cnblogs.com/sunshinewxz/p/4525065.html