您的位置:首页 > 编程语言 > Java开发

Java实现之冒泡排序

2009-11-11 21:07 351 查看
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->

【文章标题】
Java
实现之冒泡排序

【文章作者】曾健生

【作者邮箱】
zengjiansheng1@126.com

【作者
QQ

190678908

【作者
MSN

zengjiansheng1@hotmail.com

【作者博客】
blog.csdn.net/newjueqi





package com.newjueqi.servlet;
//冒泡排序
public class BubbleSort {
	public void bubble(int array[]) {
		
		boolean flag;// 标记,检查是否存在交换,TRUE为存在,false为不存在
		for (int i = 0; i < array.length; i++) {
			
			flag = false;
			for (int j = 0; j < array.length - i-1; j++) {
				if (array[j] < array[j + 1]) {
					
					flag = true;					
					
					//交换array[j]与array[j + 1]的值
					array[j] = array[j]^array[j + 1];
					array[j + 1] =array[j]^array[j + 1];
					array[j] = array[j]^array[j + 1];
					
				}
			}
			if (flag == false) {
				break;
			}
		}
	}
	public static void main(String args[])
	{
		
		BubbleSort bubbleSort=new BubbleSort();
		int array[]={8,9,1,2,3,4,5,6,7};
		bubbleSort.bubble( array );
		for(int i=0;i<array.length;i++)
		{
			System.out.println(array[i]);
		}
		
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: