Let's say that an array is max-min sorted if the first element of the array is the maximum element, the second is the minimum, the third is the second maximum and so on. Modify Selection sort such that it can be used for max-min sorting. Input:the first line contains a number nn...
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]){//如果当前遍历到的数小于当前最小索引的对应...
public void selectionSort() { for (int i = 0; i < size; i++) { int minIndex = i; //set the minIndex to current element for (int j = i + 1; j < size; j++) { //compare the value of current element with remaining elements in // the array. If a value smaller than...
选择排序(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+" "); } } public static int[] selectionSort(int[] array){ ...
/** * 选择排序 * @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;...
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. import java.util.Arrays; public class SelectionSort { //采用选择排序将数组排序 public static void main(String[] args) { //创建数组 Integer[] arr={2,5,8,1,4,7,3,6,9}; int temp;
因为789456123,这算是一个数字,args的长度也就是一 可以改为:String[] s = args.split();int[] a = new int[s.length];for(int m = 0; m < a.length; m++){ a[m] = new Integer(s[m]);}
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the beginning. The array will have two parts in this process. A subarray which is sorted and other subarrays which is yet to be sorted....
Java C C++ # Selection sort in PythondefselectionSort(array, size):forstepinrange(size): min_idx = stepforiinrange(step +1, size):# to sort in descending order, change > to < in this line# select the minimum element in each loopifarray[i] < array[min_idx]: min_idx = i# put...
=== "Java" java // arr代码下标从 1 开始索引 static void selection_sort(int[] arr, int n) { for (int i = 1; i < n; i++) { int ith = i; for (int j = i + 1; j <= n; j++) { if (arr[j] < arr[ith]) { ith = j; } } // swap int temp = arr[...