首页 > 编程语言 > 详细

两种简单的排序—适合我这样的新手

时间:2015-09-09 00:57:58      阅读:256      评论:0      收藏:0      [点我收藏+]

=============================================

              一种:选择排序

package edu.xl.lesson1;
/**
* 选择排序
* @author Administrator
*
*/
public class Selection_Sort {

public static void main(String[] args) {
//实例化一组数
  int a[]={2,42,245,53,13,46,1,5,75,34};
  for(int i = 0;i<a.length;i++){
//声明最小值
  int min=i;
  for( int j=i;j<a.length;j++){
  if(a[j]<a[min]){
  min = j;
  }
}
  if(min !=i){
  int b=a[min];
  a[min]=a[i];
  a[i]=b;
  }
}
  for (int c =0; c<a.length;c++){
  System.out.print(a[c]+",");
}

}

}

 

============================================

              二种:冒泡排序

package edu.hsu.yc78.day3.array;

import java.util.Arrays;

/**
* 数组排序
*
* @author Hsu。
* @date Sep 2, 2015
*/
public class ArraySortDemo {

/**
* 冒泡
*/
public static void test1() {
  int[] numbers = { 12, 41, 234, 1, 234, 134, 2, 34, 324, 23, 4 };
  for (int i = 0; i < numbers.length - 1; i++) {
  for (int j = 0; j < numbers.length - i - 1; j++) {
  if (numbers[j] > numbers[j + 1]) {
  int temp = 0;
  temp = numbers[j];
  numbers[j] = numbers[j + 1];
  numbers[j + 1] = temp;
    }
  }
}

  for (int current : numbers) {  
  System.out.println(current);
  }
}

============================================

          了解自带排序方法

/**
* JDK自带的排序
*/
public static void test2() {
  int[] numbers = { 12, 41, 234, 1, 234, 134, 2, 34, 324, 23, 4 };

// 按数字升序
  Arrays.sort(numbers);

  for (int i = numbers.length - 1; i >= 0; i--) {
  System.out.println(numbers[i]);
  }
}

 

public static void main(String[] args) {//调用test2
test2();  
}

/**

作为初学者,还得加油了  加油加油!!!

*/

两种简单的排序—适合我这样的新手

原文:http://www.cnblogs.com/ADAD/p/4793359.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!