Java冒泡排序之我见

来源:互联网 发布:microchip单片机选型 编辑:程序博客网 时间:2024/05/06 12:23

关于冒泡排序
原理:
简单理解为假设排序有N趟,则冒泡排序是经过N-1趟排序完成,第X趟子排序从第一个数至N-X个数,若第X个数比后一个数大(则升序,小降序)交换位置。
每经过一轮排序,将最大的数沉到最底部
程序实现:
package com.liuy.study;

/**
 * Java 冒泡排序
 *
 * @author guaishou
 *
 */
public class JavaMaoPao {

 public void sort(int[] n) {

  for (int i = 1; i < n.length; i++) {
   for (int j = 0; j < n.length - i; j++) {
    if (n[j] < n[j + 1]) {
     int temp = n[j];
     n[j] = n[j + 1];
     n[j + 1] = temp;

    }
   }
   System.out.println("第"+(i)+"次排序结果");
   for(int k=0; k<n.length; k++) {
    System.out.println("*************"+n[k]+"\t");
   }
   System.out.println("");

  }
  System.out.println("最终排序结果");
  for(int k=0; k<n.length; k++) {
   System.out.println("*************"+n[k]+"\t");
  }

 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  JavaMaoPao jmp = new JavaMaoPao();
  int[] a = { 99, 22, 32, 12, 2, 8, 66, 88, 29, 236 };
  jmp.sort(a);

 }

}