发布网友 发布时间:2022-07-29 03:01
共2个回答
热心网友 时间:2023-11-18 05:52
java程序代码如下:
public class RandomSort {
public int [] numArray = new int[20];
public RandomSort(){
for (int i = 0; i < 20; i++){
this.numArray[i] = ((int)(Math.random() * 100 + 200));
}
}
//快速排序方法
public int[] SortFunction(int[] array){
int i = 0;
int j = 0; //
int k = 0;
int temp = 0;
for(i = 0; i < array.length - 1; i++){
j = array.length - 1;
k = array[i];
for(; j > i; j--){
if(array[j] < k ){
k = array[j];
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array;
}
public static void main(String[] args) {
RandomSort random1 = new RandomSort();
int[] array = random1.SortFunction(random1.numArray);
System.out.println("20个随机数:");
for(int i = 0; i < random1.numArray.length; i++){
System.out.print(random1.numArray[i] + ";");
}
System.out.println("");
System.out.println("逆序显示结果:");
for(int j = array.length - 1; j >= 0; j--){
System.out.print(array[j] + ";");
}
}
}
结果如下图:
热心网友 时间:2023-11-18 05:52
#include<stdio.h>
#include<time.h>
#define N 20
void main()
{ int i,j,k,a[N];
srand((unsigned int)time(NULL));
for ( i=0;i<N;i++ ) a[i]=rand()%101+200;
for ( i=0;i<N-1;i++ ) for ( j=i+1;j<N;j++ ) if ( a[i]<a[j] ) { k=a[i]; a[i]=a[j]; a[j]=k; }
for ( i=0;i<N;i++ ) printf("%d ",a[i]); printf("\n");
}