teach-ict.com logo

THE education site for computer science and ICT

3. Bubble Sort pseudocode

The pseudocode below is for the ascending order algorithm. Much like the searching algorithms, you may wish to revisit this page once you have a better grasp on the programming techniques and constructs laid out in our Programming section. For now, just follow the commentaries on each line.

Hover over each line to see the comment about it.


And for completeness sake, a small adjustment to the pseudocode will sort the list in descending order, the only line that changes is the one in red shown below, where the 'greater than' becomes 'less than'

     data_set = [9,2,5,23,34,56]	
     last_exam_position = data_set.length - 2     

     swap = true  
     WHILE swap == true
       swap = false  
          FOR i = 0 to last_exam_position 
             IF data_set[i] < data_set[i +1] THEN
                   temp = data_set[i+1]
                   data_set[i+1] = data_set[i]
                   data_set[i] = temp   // the swap is complete
                swap = true
             END IF
          NEXT i

     PRINT "List is now in descending order." 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: What is bubble sorting?