2025-01-15 选择排序

This commit is contained in:
liangjinglin 2025-01-16 00:34:55 +08:00
parent e50079795d
commit d275f990ac
2 changed files with 54 additions and 2 deletions

View File

@ -49,9 +49,9 @@ public class BubbleSort {
}
public static void main(String[] args) {
int[] arr = new int[10000];
int[] arr = new int[50000];
System.out.println("排序前的序列:");
for(int i=0; i<10000; i++){
for(int i=0; i<50000; i++){
arr[i] = (int)(Math.random()*1000000);
System.out.print(arr[i] + " ");
}

View File

@ -0,0 +1,52 @@
package SortAlgori;
public class SelectSort {
private int[] arr;
public SelectSort(int[] arr){
this.arr = arr;
}
public void sort(){
for (int i = 0; i<arr.length; i++){
int temp = arr[i];
int index = i;
for(int j=i+1; j<arr.length; j++){
if(temp>arr[j]){
temp = arr[j];
index = j;
}
}
if(index != i){
arr[index] = arr[i];
arr[i] = temp;
}
}
}
public void show(){
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] arr = new int[50000];
System.out.println("排序前的序列:");
for(int i=0; i<50000; i++){
arr[i] = (int)(Math.random()*1000000);
System.out.print(arr[i] + " ");
}
System.out.println();
SelectSort selectSort = new SelectSort(arr);
long startTime = System.currentTimeMillis();
selectSort.sort();
long endTime = System.currentTimeMillis();
System.out.println("排序后的序列:");
selectSort.show();
System.out.println("排序所花费的时间:" + (endTime - startTime) + "ms");
}
}