Java冒泡排序算法
Posted Tom_gx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java冒泡排序算法相关的知识,希望对你有一定的参考价值。
package com.jckb; /** * 冒泡排序 * @author gx */ public class BubbleSort { public static void main(String[] args) { int[] arr = { 6, 3, 8, 2, 9, 1 }; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } System.out.println("排序后的数组为:"); for (int item : arr) { System.out.print(item + "\\t"); } } }
//冒泡排序过程图
以上是关于Java冒泡排序算法的主要内容,如果未能解决你的问题,请参考以下文章