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'

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

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

     SEND "List is now in descending order." TO DISPLAY 
    

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?