选择排序算法java实现
2018-07-20 来源:open-open
选择排序算法java实现
import java.util.Arrays; import java.util.List; import java.util.Scanner; public class SelectionSort { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String line = sc.nextLine(); String[] split = line.split(" "); List<String> list = Arrays.asList(split); for (int i = 0; i < list.size(); i++) { String s = list.get(i); if ("".equals(s)) { list.remove(i); } } int[] ints = new int[list.size()]; for (int i = 0; i < ints.length; i++) { ints[i] = Integer.parseInt(list.get(i)); } int temp = 0; for (int i = 0; i < ints.length; i++) { temp = i; for (int j = i + 1; j < ints.length; j++) { if (ints[j] < ints[temp]) { temp = j; } } if (ints[i] > ints[temp]) { ints[i] = ints[temp] + ints[i]; ints[temp] = ints[i] - ints[temp]; ints[i] = ints[i] - ints[temp]; } } for (int i = 0; i < ints.length - 1; i++) { int anInt = ints[i]; System.out.print(String.format("%d ", anInt)); } System.out.println(ints[ints.length - 1]); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇:php 打包网站在线压缩为zip
最新资讯
热门推荐