站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题排序的问题

排序的问题

摘要: 排序的问题


在控制台任意输入一组数字进行排序
小弟的JAVA基础不好,请各位指点~


我也是新手,前几天写的快速排序加以修改,楼主看满足你的要求不?
import java.io.*;
public class QuickSort {
public static void sort(int[] a){
sort(a,0,a.length-1);
}
public static void sort(int[] a ,int i ,int j){
if(i < j){
int low = i ,high = j ,temp = a[i];
while(low != high){
while(low < high && a[high]>temp)high--;
if(low < high)a[low++]=a[high];
while(low < high && a[low]if(low < high)a[high--]=a[low];
}
a[low]=temp;
sort(a,i,low-1);
sort(a,low+1,j);
}
}
public static void print(int[] a){
for(int v:a){
System.out.print(v + " ");
}
}
public static void inputNum() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("输入你要排序的数,中间用空格分开");
String s = br.readLine().trim();
String [] stringNums = s.split("//s+");
int[] num = typeOf(stringNums);
sort(num);
print(num);
}catch(NumberFormatException e1){
throw new RuntimeException("输入的类型不正确");
}
catch(IOException e2){
e2.printStackTrace();
}
}
public static int[] typeOf(String[] s){
int[] number = new int[s.length];
int i = 0;
for(String str:s){
number[i++] = Integer.parseInt(str);
}
return number;
}
public static void main(String[] args) {
inputNum();
}
}


谢谢 我运行了下代码 可以
真是太感谢了
我还要研究一下~

import java.io.*;
public class pai
{
public static void main(String args[]) throws Exception
{

BufferedReader put=new BufferedReader(new InputStreamReader(System.in));
String input = put.readLine();



int length=input.length();
char sub[]=new char[length];
input.getChars(0,length,sub,0);
for(int i=0;i{
if(sub[i]>sub[i+1])
{
sub[i+1]=sub[i];
}
}

System.out.print(sub);

}
}


↑返回目录
前一篇: 如何编写代码实现从access数据库中将几个表的数据导入到sql serve/Oracle中去?
后一篇: 我想把一个文件转码成utf8的,但是文件中出现了乱码,希望高手指点(内附代码)