[关闭]
@XQF 2018-03-07T22:53:23.000000Z 字数 473 阅读 609

排序----选择排序

数据结构与算法


选择排序是每次选一个基准数和后面的数进行比较,选出这一轮中包括基准数在内的最值。每一轮一次。

  1. public class Solution {
  2. public int[] selectSort(int[] nums) {
  3. if(nums.length()==0||nums==null){
  4. return null;
  5. }
  6. for (int i = 0; i < nums.length-1; i++) {
  7. for (int j = i + 1; j < nums.length; j++) {
  8. if (nums[i] > nums[j]) {
  9. int temp = nums[i];
  10. nums[i] = nums[j];
  11. nums[j] = temp;
  12. }
  13. }
  14. }
  15. return nums;
  16. }
  17. public static void main(String[] args) {
  18. Solution solution = new Solution();
  19. int[] nums = {1, 2, 55, 6, 3, 2, 4, 5, 6, 8, 9, 0};
  20. System.out.println(Arrays.toString(solution.selectSort(nums)));
  21. }
  22. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注