Bubble sort: Difference between revisions

From Algowiki
Jump to navigation Jump to search
(Created page with "== Pseudocode == <code> BUBBLESORT(''A'') 1 '''for''' ''i'' = 1 '''to''' "A.length" - 1 2 '''for'' ''j'' = ''A.length'' '''downto''' ''i'' + 1 3 '''if''...")
 
Line 5: Line 5:
  1 '''for''' ''i'' = 1 '''to''' "A.length" - 1  
  1 '''for''' ''i'' = 1 '''to''' "A.length" - 1  
  2      '''for'' ''j'' = ''A.length'' '''downto''' ''i'' + 1
  2      '''for'' ''j'' = ''A.length'' '''downto''' ''i'' + 1
  3            '''if''' ''A[j]'' < ''A[j - 1]''
  3            '''if''' ''A''[''j''] < ''A''[''j'' - 1]
  4                  exchange ''A[j] with ''A[j - 1]''
  4                  exchange ''A''[''j''] with ''A''[''j'' - 1]
</code>
</code>

Revision as of 15:32, 25 September 2014

Pseudocode

BUBBLESORT(A)
1 for i = 1 to "A.length" - 1 
2      for j = A.length downto' i + 1
3            if A[j] < A[j - 1]
4                  exchange A[j] with A[j - 1]