`
to_zoe_yang
  • 浏览: 139374 次
  • 性别: Icon_minigender_2
  • 来自: 01
社区版块
存档分类
最新评论

冒泡法排序

J# 
阅读更多
冒泡法就是每一次的遍历找,将最大的元素置于最顶层,然后对剩下重复上述过程!
在将最大的元素置于顶层有两种方法(我目前想到的)
1.记下下标,然后交换
2.遍历的过程就交换

下述方法用的第二种

public static void MaoPao(int array[]) {
		// index记录每次最大值的下标
		int index;

		for (int j = 1; j < array.length; j++) {
			for (int i = 0; i < array.length - j; i++) {
				if (array[i] > array[i + 1]) {
					int tmp = array[i];
					array[i] = array[i + 1];
					array[i + 1] = tmp;
				}
			}
		}
	}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics