1 public class SelectSortTest { 2 public static void selectSort(int[] source) { 3 for (int i = 0; i < source.length; i++) { 4 for (int j = i + 1; j < source.length; j++) { 5 if (source[i] > source[j]) { 6 swap(source, i, j); 7 } 8 } 9 } 10 } 11 //private 完成交换功能的子函数 12 private static void swap(int[] source, int x, int y) { 13 int temp = source[x]; 14 source[x] = source[y]; 15 source[y] = temp; 16 } 17 //在main中测试 18 public static void main(String[] args) { 19 int[] a = {4, 2, 1, 6, 3, 6, 0, -5, 1, 1}; 20 21 selectSort(a); 22 23 for (int i = 0; i < a.length; i++) { 24 System.out.printf("%d ", a[i]); 25 } 26 } 27 }
#学习笔记,如有谬误,敬请指正。#
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步