关于完整的排序例子
我要做一个比较完整的数字排序的小例子,
要求可以在键盘上任意输入数子,然后接收,排序之后输出。
排序的主体不用ARRAYS,用冒泡。
下边是我会做的部分。。 import java.io.*;
public class P
{
public static void main(String args[]) throws IOException
{
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
String n1;
int[] c = new int[];
try{
System.out.print("in put the Number: ");
n1= buf.readLine(); }
catch(NumberFormatException e)
{ System.out.println(" please input restart! ");
// return aaa;
}
int n11;
int a[]=n1;
int i,j,t;
for(i=0;i<a.length;i++)
{
for (j = 0; j < (a.length -1) - i; j++)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j+1] = t;
}
}
}
for (i=0;i<a.length;i++)
System.out.print(a[i]+",");
}
}
做了一塌糊涂。哪个哥哥姐姐可以告诉我怎么完成啊????????我新学JAVA
我要做一个比较完整的数字排序的小例子,
要求可以在键盘上任意输入数子,然后接收,排序之后输出。
排序的主体不用ARRAYS,用冒泡。
下边是我会做的部分。。 import java.io.*;
public class P
{
public static void main(String args[]) throws IOException
{
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
String n1;
int[] c = new int[];
try{
System.out.print("in put the Number: ");
n1= buf.readLine(); }
catch(NumberFormatException e)
{ System.out.println(" please input restart! ");
// return aaa;
}
int n11;
int a[]=n1;
int i,j,t;
for(i=0;i<a.length;i++)
{
for (j = 0; j < (a.length -1) - i; j++)
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j+1] = t;
}
}
}
for (i=0;i<a.length;i++)
System.out.print(a[i]+",");
}
}
做了一塌糊涂。哪个哥哥姐姐可以告诉我怎么完成啊????????我新学JAVA
public class MainTest {
public static void main(String[] args) throws UnsupportedEncodingException,
MalformedURLException, IOException {
MainTest mt = new MainTest();
System.out.println("请输入数字,按照a,b,c,d....格式输入,之后按回车");
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
String input=buf.readLine();
StringTokenizer tokenizer = new StringTokenizer(input, ",");
Vector numbers = new Vector();
while (tokenizer.hasMoreTokens()) {
numbers.addElement(tokenizer.nextToken());
}
int[] no=new int[numbers.size()];
for (int i = 0; i < numbers.size(); i++) {
no[i]=Integer.parseInt((String)numbers.elementAt(i));
}
int temp=0;
for(int i=0;i<no.length;i++){
for(int j=0;j<no.length-1-i;j++){
if(no[j]>no[j+1]){
temp=no[j];
no[j]=no[j+1];
no[j+1]=temp;
}
}
}
for (int i = 0; i < no.length; i++) {
System.out.println(no[i]);
}
}
}
下面是控制台的输出:请输入数字,按照a,b,c,d....格式输入,之后按回车
25,14,87,695,324,120,21,4
4
14
21
25
87
120
324
695
第一遍使最轻的记录上升到数组的最顶端,
第二遍使剩下的最小的上升到第二位置,
第二遍扫描时不必再比较最顶端的记录
*/static void Main(string[] args)
{
int[] a= new int[100];
Console.WriteLine("输入int数组里的元素数目");
string s=Console.ReadLine();
int x=Int32.Parse(s); Console.WriteLine("输入元素");
for(int j=0;j<x;j++)
{
string s1=Console.ReadLine();
a[j]=Int32.Parse(s1);
} int limit= x-1;
for(int pass=0;pass<x-1;pass++)
{
for(int j=0;j<limit-pass;j++)
{
if(a[j]>a[j+1])
{
int k=a[j]; //数组元素交换
a[j]=a[j+1]; //数组元素交换
a[j+1]=k; //数组元素交换
}
}
} Console.WriteLine("Sorted elements of an array are(冒泡排序)"); for (int j=0;j<x;j++)
{
Console.WriteLine(a[j]);
}
Console.ReadLine ();
}