您的位置:首页 > 其它

lintcode/leetcode由易至难第3题:选择排序

2017-05-08 14:18 453 查看
public class Solution {
/**
* @param A an integer array
* @return void
*/
//重点是记录指针!!!!派一个指针不断向后找
public void sortIntegers(int[] A) {
// Write your code here
if (A == null || A.length == 0 || A.length == 1) return;
for(int i = 0; i < A.length - 1; i++){   //此处判断条件不能为i < A.length,否则第11行溢出
int target = i + 1;
for(int j = target + 1; j < A.length; j++){
if(A[target] >= A[j]){
target = j;
}
}
if(A[i] >= A[target]){
int tmp = A[i];
A[i] = A[target];
A[target] = tmp;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: