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

是用java编写额
2024-11-30 02:30:59
推荐回答(2个)
回答1:

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] {
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:

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 System.out.println(a1[i]);
}
}
public void SelectSort(int [] needSortArray){
int tmp=0;
for(int i=0;i for(int j=i+1;j if(needSortArray[j] tmp=needSortArray[i];
needSortArray[i]=needSortArray[j];
needSortArray[j]=tmp;
}
}
}
}

}