zoukankan      html  css  js  c++  java
  • 改进的冒泡排序算法一

    /**
     * Project Name:Algorithm
     * File Name:BubbleSortImprove1.java
     * Package Name:
     * Date:2017年9月14日上午11:07:21
     * Copyright (c) 2017, chenzhou1025@126.com All Rights Reserved.
     *
     */
    /**
     * Project Name:Algorithm
     * File Name:BubbleSortImprove1.java
     * Package Name:
     * Date:2017年9月14日上午11:07:21
     * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved.
     *
     */
    
    /**
     * ClassName:BubbleSortImprove1 
     * Function: 改进的冒泡排序算法, 测试数据集:6 3 5 7 0 4 12. 
     * Reason:     本例的数据集,在第4次就已经排好序了,所以没必要再进行第5、6次的排序了。
     *             设置一个bool变量,判断本次循环有没有元素之间交换位置,如果没有,则说明已经排好序了,
     *             退出循环即可
     *              
     * Date:     2017年9月14日 上午11:07:21 
     * @author   michael
     * @version  
     * @since    JDK 1.7
     * @see      
     */
    public class BubbleSortImprove1 {
        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 - 1; i++) {
                    boolean isChanged=false;
                    for (int j = 0; j < arr.length - 1; j++) {
                        int temp;
                        if (arr[j] > arr[j + 1]) {
                            temp = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = temp;
                            isChanged = true;
                        }
                        
                    }
                    if(!isChanged)
                        break;    
                    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");
            }
        }
    }

  • 相关阅读:
    C#博客随笔之四:使用C#模拟办公网登录HttpClient的使用
    C#博客随笔之三:Linq in C#
    C#博客随笔之二:wp开发之弹出对话框
    C#博客随笔之一:使用C#的第一个WP程序
    Fedora15命令速查手册
    乐观是一种智慧
    完全教程 Aircrackng破解WEP、WPAPSK加密利器
    FreeBSD常用命令大全
    Linux 网络管理员指南——前言
    API
  • 原文地址:https://www.cnblogs.com/Michael2397/p/7519704.html
Copyright © 2011-2022 走看看