冒泡排序实例

package com;

/**
* 冒泡排序
*/
public class Taxis {

/**
* @param args
*/
public static void main(String[] args) {
int c = 0; //定义int变量
int arr[] = {12,56,4,62,8}; //定义用于排序的数组

System.out.println("没有排序前数组中的元素为:");

for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}

System.out.println("\n从小到大排序为:");

for (int i = 0; i < arr.length; i++) {
for(int j = i + 1; j < arr.length; j++) {
if(arr[i] > arr[j]) { //对相邻两个数进行比较
c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
System.out.print(arr[i] + " ");
}

System.out.println("\n从大到小排序为:");

for (int i = 0; i < arr.length; i++) {
for(int j = i + 1; j < arr.length; j++) {
if(arr[i] < arr[j]) { //对相邻两个数进行比较
c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
System.out.print(arr[i] + " ");
}
}

}

posted @ 2013-05-10 17:09  幻星宇  阅读(279)  评论(0编辑  收藏  举报