zoukankan      html  css  js  c++  java
  • 选择排序

    /**
     * Project Name:Algorithm
     * File Name:Selection.java
     * Package Name:
     * Date:2017年9月15日下午7:33:52
     * Copyright (c) 2017, chenzhou1025@126.com All Rights Reserved.
     *
     */
    
    /**
     * ClassName:Selection 
     * Function: 选择排序,数据集:6 3 5 7 0 4 1 2 
     * Reason:     (1)举个例子,序列5 8 5 2 9, 我们知道第一遍选择第1个元素5会和2交换,
     *                     那么原序列中2个5的相对前后顺序就被破坏了,所以选择排序不是一个稳定的排序算法
     *             (2)简单选择排序的基本思想:给定数组:int[] arr={里面n个数据};
     *                 第1趟排序,在待排序数据arr[1]~arr[n]中选出最小的数据,将它与arrr[1]交换;
     *                 第2趟,在待排序数据arr[2]~arr[n]中选出最小的数据,将它与r[2]交换;
     *                 以此类推,第i趟在待排序数据arr[i]~arr[n]中选出最小的数据,将它与r[i]交换,直到全部排序完成
     * Date:     2017年9月15日 下午7:33:52 
     * @author   michael
     * @version  
     * @since    JDK 1.7
     * @see      
     */
    public class Selection {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String input = "";
            while (sc.hasNext()) {
                input = sc.nextLine();
                System.out.println("输入值:" + input);
                long startTime = System.currentTimeMillis();
                String[] str = input.split(" ");
                int[] arr = new int[str.length];
                // 字符串数组转化成int数组
                for (int i = 0; i < str.length; i++) {
                    arr[i] = Integer.parseInt(str[i]);
                }
                for (int i = 0; i < arr.length; i++) {
                    for (int j = i+1; j < arr.length; j++) {
                        int temp = 0;
                        if(arr[j]<arr[i]){
                            temp = arr[i];
                            arr[i]= arr[j];
                            arr[j] = temp;
                        }
                        
                    }
                    System.out.println();
                    System.out.print("第" + (i + 1) + "次循环结果:");
                    for (int j = 0; j < arr.length; j++) {
                        System.out.print(arr[j]);
                    }
                }
    
                long endTime = System.currentTimeMillis();
                System.out.println();
                System.out.println();
                System.out.println("最终排序结果:");
                for (int j = 0; j < arr.length; j++) {
                    System.out.print(arr[j]);
                }
                System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
            }
        }
    }

  • 相关阅读:
    git的使用
    本体建模
    word2vec改进之Negative Sampling
    word2vec改进之Hierarchical Softmax
    word2vec原理
    Window下mysql的安装
    PageRank算法
    集成学习-------简单介绍
    自我介绍
    Apollo学习笔记(二):循迹实现过程
  • 原文地址:https://www.cnblogs.com/Michael2397/p/7528236.html
Copyright © 2011-2022 走看看