• 算法学习(java实现)


    不管是在面试中,还是在日常中,算法的重要性不可言喻,接下来将持续更新算法:

    第1个:冒泡排序:

               冒泡排序,大一的C语言中接触的第一个算法。主要的思路是“把大的数往前推”。时间复杂度:O(n2)

               demo如下”

           

    /**
     * 经典的冒泡排序
     * */
    public class FirstSort {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    
    		int[] a = new int[] { 1, 4, 23, 7, 8, 9, 1, 20 };
    		int temp;
    		for (int i = 0; i < a.length; i++) {
    			System.out.print(a[i] + " ");
    		}
    
    		System.out.println();
    
    		for (int i = 0; i < a.length; i++)
    			for (int j = i + 1; j < a.length ; j++) {
    				if (a[i] > a[j]) {
    					temp = a[i];
    					a[i] = a[j];
    					a[j] = temp;
    				}
    			}
    
    		for (int i = 0; i < a.length; i++) {
    			System.out.print(a[i] + " ");
    		}
    	}
    
    }
    

      输出如下:

    1 4 23 7 8 9 1 20 

    1 1 4 7 8 9 20 23

  • 相关阅读:
    自闭的D7
    D2
    Codeforces Round #531 (Div. 3)
    hello 2019 D
    牛客练习赛36B
    cf954H
    gym102007 E
    Gym 101972
    Gym 101810
    试题 历届试题 青蛙跳杯子(bfs)
  • 原文地址:https://www.cnblogs.com/qgzhan/p/3020463.html
Copyright © 2020-2023  润新知