选择排序,就是选择最小的,然后置换,循环再找到最小的,再置换...
1 package com.swust.插入排序; 2 3 import java.util.Random; 4 5 public class Example1 { 6 public static void main(String[] args) { 7 int[] arr=new int[10]; 8 for(int i=0;i<arr.length;i++){ 9 arr[i]=new Random().nextInt(100); 10 } 11 test(arr); 12 for (int i = 0; i < arr.length; i++) { 13 System.out.print(arr[i]+","); 14 } 15 } 16 public static void test(int[] a){ 17 for (int i=0; i<a.length; i++){ 18 int small = i; 19 //找出最小的 20 for (int j=i+1; j<a.length; j++){ 21 if (a[small]>a[j]){ 22 small = j; 23 } 24 } 25 int temp =0; 26 //置换位置 27 if (i != small){ 28 temp = a[small]; 29 a[small] = a[i]; 30 a[i] = temp; 31 } 32 } 33 } 34 }
时间: 2024-10-10 23:26:55