您的位置:首页 > 理论基础 > 数据结构算法

数据结构JAVA--冒泡排序

2015-01-02 17:35 260 查看
package com.function.demo_01;

/**

* @author OF

* 2014年12月31日

* 数据结构--冒泡排序

* 算法描述:

* 升序排列:从第一个元素开始,对数组中两两相邻的元素比较,将值较小的元素放在前面,

* 值较大的元素放在后面,一轮比较完毕,

* 一个最大的数沉底成为数组中的最后一个元素,一些较小的数如同气泡一样上浮一个位置。

* n个数,经过n-1轮比较后完成排序。

*/

public class ArraySort {

public static void main(String[] args) {

int arr = 10;

int temp = 0;

int a[] = { 3, 12, 4, 55, 62, 5, 44, 11, 53, 22 };

for (int i = 0; i < arr - 1; i++) {

for (int j = 0; j < arr - i - 1; j++) {

if (a[j] > a[j + 1]) {

temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

}

}

}

for (int i = 0; i < 10; i++) {

System.out.print(a[i] + ",");

}

System.out.println();

for (int i = 0; i < arr - 1; i++) {

for (int j = 0; j < arr - 1; j++) {

if (a[j] < a[j + 1]) {

temp = a[j + 1];

a[j + 1] = a[j];

a[j] = temp;

}

}

}

for (int i = 0; i < 10; i++) {

System.out.print(a[i] + ",");

}

System.out.println();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: