Basic algorithm - Data sorting - Bubble

Just watching Luo Gu class network playback, the starting point to rip.

The following summary notes finish (a little tired, content simple, forgive me).

Bubble Sort

Every operation, from left to right scan the array; if a [i]> a [i + 1], to the exchange, repeated n times, orderly array.

Code (meaning understand like, largely omitted): void bubbleSort ()

{

          for(int i=1;i<=n;i++)

              for(int j=1;j<n;j++)

                  if(a[j]>a[j+1]) swap(a[j],a[j+1]);

}

Time complexity: O (n * n);

Complexity Space: O (1).

Advantages: simple, low space complexity and stability.

Disadvantages: low efficiency, high time complexity.

 

Guess you like

Origin www.cnblogs.com/weijianzhen/p/12241753.html