标签:
=============================================
一种:选择排序
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