Giter VIP home page Giter VIP logo

bubblesortjava's Introduction

氣泡排序法

減少多餘的迴圈,最有效率的解法

前提

  • 假設陣列中有 n 個數字,大小參雜
  • int[] data = {27,10,32,99,2,13,80,55}; 
  • 排序完畢,數字由大到小排序
  • 輸出結果:2 10 13 27 32 55 80 99 

思考邏輯

  • 巢狀迴圈,包括內外兩層
  • 外層最少必須跑 n-1 次 (如果最小的數字在最後,必須 n-1 次才能到最前)
  • 外層每跑一次,尚未被分類的最大數字會被排至最後
  • 假設陣列 8 7 5 6 4 3 2 1
    第一輪排序 7 6 5 4 3 2 1 8   <- 8 被排到最後了
    第二輪排序 6 5 4 3 2 1 7 8   <- 7 被排到最後了 
    .
    . 以此類推
    .
  • 因此內層的分類次數會由 n-1 開始遞減
  • for 迴圈部分到此為止,接下來就是數值換位的部分了
  • 有計數器 k,從 0 開始遞增,兩兩比較數值用
  • k 必須在每一輪外層結束後歸零,下一輪從 0 開始

BubbleSortArgv.java

透過命令列參數傳入任意個數的數值,將排序後的結果顯示出來。

bubblesortjava's People

Contributors

0x66you avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.