当我用随机数组(范围从0到99)比较平均情况下的冒泡排序和选择排序时,冒泡排序表现明显更好。我读过的大多数有关性能的参考资料都指出选择排序是两者中更好的一个。 这是我的选择实现: public static void selectionSort(int[] arr) { /* * Selection sort sorting algorithm. Cycle: The minimum element form ...
Selection sort is one of the simplest sorting algorithms. It is easy to implement but it is not very efficient. The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the ...
}publicstaticint[]Sort(int[] array){//记录数组长度intlength = array.length;//外层循环for(inti=0;i<length-1;i++){//将最小数下标记录为iintmin_index = i;//内层循序 遍历i后边的数组for(intj=i+1;j<length;j++){if(array[j]<array[min_index]){//如果当前遍历到的数小于当前最小索引的...
1packagesort;23publicclassSelectionSortDemo {4/**5* 选择排序6* 时间复杂度O(n^2)7*@paramarr 需要排序的数组8*/9publicstaticvoidselectionSort(int[] arr) {10for(inti = 0; i < arr.length - 1; i++) {11intminIndex =i;12intmin =arr[i];13for(intj = i + 1; j < arr.length; j...
选择排序(Selection Sort)代码实现: public class Demo { public static void main(String[] args) { int[] array= {6,1,7,8,9,3,5,4,2}; int[] newarray=selectionSort(array); for(int arr:newarray) { System.out.print(arr+" "); ...
/** * 选择排序 * @author chenpeng * */ public class SelectionSort { //我们的算法类不允许产生任何实例 private SelectionSort() {} public static void sort(int[] arr) { int n = arr.length; for(int i=0;i<n;i++) { //寻找区间里最小值的索引 int minIndex =i; for(int j=i+1;j...
Implement Java program for selection sort using arrays to sort array elements in ascending order and explains its pros and cons. Selection sort is an in-place comparison sort algorithm. Selection sort has O(n2) time complexity. Selection sort has perform
Selection Sort Algorithm selectionSort(array, size) for i from 0 to size - 1dosetiastheindexofthecurrentminimumforjfromi +1tosize-1doifarray[j] <array[currentminimum]setjasthenewcurrentminimumindexifcurrentminimumisnoti swaparray[i]witharray[currentminimum]endselectionSort ...
test.algorithm.sort.Selection; import java.util.Arrays; public class TestSelection { public static void main(String[] args) { Integer[] a = {4, 6, 8, 7, 10, 2, 1}; Selection.sort(a); System.out.println(Arrays.toString(a)); } } 测试结果: [1, 2, 4, 6, 7, 8, 10] 3.2....
Java Program to perform Selection Sort on Array In the following example, we have defined a methodselectionSort()that implements the selection sort algorithm. It finds the minimum element from the array and swaps it with the first element of the array. ...