输入n个整数,并且进行降序排序

来源:互联网 发布:在淘宝网买东西可靠吗 编辑:程序博客网 时间:2024/06/10 06:38

package com.pro2;

public class SortNumbers {

 /**
  * 利用命令行参数输入n个整数,并且进行降序排序
  *
  * @param args
  */
 // 冒泡排序逆序
 public void bubbleSort1(int table[]) {
  int i, j;
  int len = table.length;
  for (i = len - 1; i > 0; i--) {
   for (j = 0; j < i; j++) {
    if (table[j] < table[j + 1]) {
     int t = table[j];
     table[j] = table[j + 1];
     table[j + 1] = t;
    }// end if

   }// end for
  }//
  for (i = 0; i < len; i++) {
   System.out.print(table[i] + " ");
  }
 }

 // 冒泡排序顺序
 public void bubbleSort2(int table[]) {
  int i, j;
  int len = table.length;
  for (i = len - 1; i > 0; i--) {
   for (j = 0; j < i; j++) {
    if (table[j] > table[j + 1]) {
     int t = table[j];
     table[j] = table[j + 1];
     table[j + 1] = t;
    }// end if

   }// end for
  }//
  for (i = 0; i < len; i++) {
   System.out.print(table[i] + " ");
  }
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int num[] = { 2, 3, 43, 12, 4, 9, 0, 12, 7, 6, 8 };
  SortNumbers sortnum = new SortNumbers();
  System.out.print("逆序输出:");
  sortnum.bubbleSort1(num);
  System.out.print("\n顺序输出:");
  sortnum.bubbleSort2(num);
 }
}