日期:2014-05-20 浏览次数:20752 次
import java.io.*;
import java.util.*;
public class SortAll {
/**
* 冒泡排序,选择排序,插入排序,希尔(Shell)排序(升序降序)
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入:数字以‘,’隔开");
String str = sc.next();
String [] tempa = str.split(",");
int [] i = new int[tempa.length];
for(int ti=0;ti<tempa.length;ti++)
{
i[ti] = Integer.parseInt(tempa[ti]);
}
//int[] i = {13, 99, 67, 58, 466, 944, 3, 123, 539, 403, 596, 87 };
System.out.println("----冒泡排序的结果(从小到大):");
maoPao(i);
System.out.println();
System.out.println("----选择排序的结果(从小到大):");
xuanZe(i);
System.out.println();
System.out.println("----插入排序的结果(从小到大):");
chaRu(i);
System.out.println();
System.out.println("----希尔(Shell)排序的结果(从小到大):");
shell(i);
System.out.println("----冒泡排序的结果(从大到小):");
maoPaoDesc(i);
System.out.println();
System.out.println("----选择排序的结果(从大到小):");
xuanZeDesc(i);
System.out.println();
System.out.println("----插入排序的结果(从大到小):");
chaRuDesc(i);
System.out.println();
System.out.println("----希尔(Shell)排序的结果(从大到小):");
shellDesc(i);
}
// 冒泡排序
public static void maoPao(int[] x) {
for (int i = 0; i < x.length; i++) {
for (int j = i + 1; j < x.length; j++) {
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
for (int i : x) {
System.out.print(i + " ");
}
}
// 冒泡排序Desc
public static void maoPaoDesc(int[] x) {
for (int i = 0; i < x.length; i++) {
for (int j = i + 1; j < x.length; j++) {
if (x[i] < x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
for (int i : x) {
System.out.print(i + " ");
}
}
// 选择排序
public static void xuanZe(int[] x) {
for (int i = 0; i < x.length; i++) {