选择排序java实现

时间:2023-08-03 09:46:32
package text.algorithm;

/**
* 选择排序
* O(n^2);空间复杂度O(1);
*/
public class SelectionSort { public static void selectionSort(int[] a) {
for (int i = 0; i < a.length; i++) {
int k = a[i];
for (int j = i+1; j < a.length; j++) {
if(k>a[j]){
int b = k;
k = a[j];
a[j]=b;
}
}
a[i]=k;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+",");
}
} public static void main(String[] args) {
int[] a = {1,23,43,56,34,12,56,7,87,-56,34,2};
selectionSort(a);
}
}