2025-01-15 选择排序
This commit is contained in:
parent
e50079795d
commit
d275f990ac
@ -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] + " ");
|
||||
}
|
||||
|
52
src/dataANDcalc/java/SortAlgori/SelectSort.java
Normal file
52
src/dataANDcalc/java/SortAlgori/SelectSort.java
Normal 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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user