编写一个程序,利用数组把10个数从小到大排序。

是用java编写额

第1个回答  2010-07-05
public class fdsa {

public static void main(String[] args) {

int[] array = new int[] {3, 5, 6, 8, 2, 1, 9, 0, 7, 4};
int temp;
System.out.println("原数组为:");
printArray(array);
int index;
for (int i = 1; i < array.length; i ++)
{
index = 0;
for (int j = 1; j <= array.length - i ; j ++)
{
if (array[j] > array[index])
{
index = j;
}
temp = array[array.length - i];
array[array.length - i] = array[index];
array[index] = temp;
}
}
System.out.println("按照直接排序法升序排列后的数组为:");
printArray(array);

for (int i = 1 ; i < array.length;i++)
{
index = 0;
for(int j = 1; j <= array.length - i; j++)
{
if (array[j]<array[index])
{
index = j;
}
temp = array[array.length - i];
array[array.length - i ] = array[index];
array[index] = temp;
}
}
System.out.println("按照直接排序法降序排列后的数组为:");
printArray(array);

}

public static void printArray(int[] array)
{
for (int i:array)
{
System.out.print(i + " ");
}
System.out.println("\n");
}

}本回答被网友采纳
第2个回答  2010-07-05
public class Test {
public static void main(String[] args) {
Test t=new Test();
int [] a1={1,25,9,8,3,4,7,6,5,45};
t.SelectSort(a1);
for(int i=0;i<a1.length;i++){
System.out.println(a1[i]);
}
}
public void SelectSort(int [] needSortArray){
int tmp=0;
for(int i=0;i<needSortArray.length;i++){
for(int j=i+1;j<needSortArray.length;j++){
if(needSortArray[j]<needSortArray[i]){
tmp=needSortArray[i];
needSortArray[i]=needSortArray[j];
needSortArray[j]=tmp;
}
}
}
}

}