美文网首页
java选择排序递归实现

java选择排序递归实现

作者: HH001 | 来源:发表于2019-03-22 10:38 被阅读0次

public class SelectSort {

/**

*arr[]={ 5,3,6,2,7,1,8,4,0,9}

*      (0),3,6,2,7,1,8,4,(5),9    第一次

*      0,(1),6,2,7,(3),8,4,5,9    第二次

*/

public static void selectSort(int[] arr,int base,int next) {

int i=base,j=next,temp,minIndex=base;

if(j<=arr.length-1) {

temp=arr[base];

while(j<=arr.length-1) {

if(arr[j]<temp) {

temp=arr[j];

minIndex=j;

}

j++;

}

arr[minIndex]=arr[base];

arr[base]=temp;

i++;

selectSort(arr,i,++i);

}

}

public static void main(String[] args) {

int[] arr= {5,3,6,2,7,1,8,4,0,9};

selectSort(arr,0,1);

for(int a:arr) {

System.out.print(a);

}

}

}

相关文章

网友评论

      本文标题:java选择排序递归实现

      本文链接:https://www.haomeiwen.com/subject/ywclvqtx.html