输入数并排序的小程序题,在线等
现在输入n个数字,以,分开,然后可选择升序或降序排序,提交键就在另一页面显示按什么排序,
结果为也以,隔开,还可提供reset
大家帮忙写下.
thanks
[解决办法]
先把字符串分割成字符数组,然后再排序好了。输出的时候再加上+","。很easy的~
[解决办法]
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++) {
int lowerIndex = i;
// 找出最小的一个索引
for (int j = i + 1; j < x.length; j++) {
if (x[j] < x[lowerIndex]) {
lowerIndex = j;
}
}
// 交换
int temp = x[i];
x[i] = x[lowerIndex];
x[lowerIndex] = temp;
}
for (int i : x) {
System.out.print(i + " ");
}
}
// 选择排序Desc
public static void xuanZeDesc(int[] x) {
for (int i = 0; i < x.length; i++) {
int lowerIndex = i;
// 找出最大的一个索引
for (int j = i + 1; j < x.length; j++) {
if (x[j] > x[lowerIndex]) {
lowerIndex = j;
}
}
// 交换
int temp = x[i];
x[i] = x[lowerIndex];
x[lowerIndex] = temp;
}
for (int i : x) {
System.out.print(i + " ");
}
}
// 插入排序
public static void chaRu(int[] x) {
for (int i = 1; i < x.length; i++) {// i从一开始,因为第一个数已经是排好序的啦
for (int j = i; j > 0; j--) {
if (x[j] < x[j - 1]) {
int temp = x[j];
x[j] = x[j - 1];
x[j - 1] = temp;
}
}
}
for (int i : x) {
System.out.print(i + " ");
}
}
// 插入排序Desc
public static void chaRuDesc(int[] x) {
for (int i = 1; i < x.length; i++) {// i从一开始,因为第一个数已经是排好序的啦
for (int j = i; j > 0; j--) {
if (x[j] > x[j - 1]) {
int temp = x[j];
x[j] = x[j - 1];
x[j - 1] = temp;
}
}
}
for (int i : x) {
System.out.print(i + " ");
}
}
// 希尔排序
public static void shell(int[] x) {
// 分组
for (int increment = x.length / 2; increment > 0; increment /= 2) {
// 每个组内排序
for (int i = increment; i < x.length; i++) {
int temp = x[i];
int j = 0;
for (j = i; j >= increment; j -= increment) {
if (temp < x[j - increment]) {
x[j] = x[j - increment];
} else {
break;
}
}
x[j] = temp;
}
}
for (int i : x) {
System.out.print(i + " ");
}
}
// 希尔排序Desc
public static void shellDesc(int[] x) {
// 分组
for (int increment = x.length / 2; increment > 0; increment /= 2) {
// 每个组内排序
for (int i = increment; i < x.length; i++) {
int temp = x[i];
int j = 0;
for (j = i; j >= increment; j -= increment) {
if (temp > x[j - increment]) {
x[j] = x[j - increment];
} else {
break;
}
}
x[j] = temp;
}
}
for (int i : x) {
System.out.print(i + " ");
}
}
}