- package com.example.demo.dataStructure.sort;
- // 简单选择排序
- public class SelectSort { public static void selectSort(int[] arg) {
- for (int i= 0;i< arg.length - 1;i++) {
- int k = i;
- for (int j=k+1;j<arg.length;j++) {
- if (arg[j] < arg[k]) {
- k = j;
- }
- }
- if (i != k) {
- int temp = arg[i];
- arg[i] = arg[k];
- arg[k] = temp;
- }
- }
- }
- public static void main(String[] args) {
- int[] arg = {9,8,7,6,5,4,3,1,2};
- selectSort(arg);
- for (int i = 0;i< arg.length;i++) {
- System.out.print(arg[i]);
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2765736.html