Bubble Sort.

No Language here - just algorithms!
Post Reply
User avatar
Anuj Dhawan
Founder
Posts: 2799
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Bubble Sort.

Post by Anuj Dhawan »

Bubble Sort:The bubble sort is generally considered to be the simplest sorting algorithm. The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them). Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass. A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits.
Why is it called a bubble sort?The bubble sort gets its name because elements tend to move up into the correct order like bubbles rising to the surface.
Pseudocode

Code: Select all

loop
    NewValue := false
    decrement itemCount
    loop with index from 1 to itemCount
        if (item at index) > (item at (index + 1))
            swap (item at index) with (item at (index + 1))
            NewValue := true
until NewValue = false
There can be many implementation of this in COBOL, one such is as follows:

Code: Select all

IDENTIFICATION DIVISION.
        PROGRAM-ID. PRG1.
       DATA DIVISION.
           01 TABLE OCCURS 5 TIMES PIC S9(2).
           01 TEMP PIC 999 VALUE 000.
           01 VAR1 PIC 9 VALUE 0.
           01 VAR2 PIC 9 VALUE 1.
       PROCEDURE DIVISION.
       0000-HOUSEKEEPING.
           DISPLAY "ENTER ANY FIVE NUMBERS:"
*
           PERFORM UNTIL VAR1 = 5
           ADD 1 TO VAR1  
           ACCEPT TABLE(VAR1)
           END-PERFORM.
*
           MOVE 1 TO VAR1.
*
           PERFORM UNTIL VAR1 > 5
                        MOVE VAR1 TO VAR2
                        PERFORM UNTIL VAR2 > 5 
                                     IF (TABLE(VAR1) > TABLE(VAR2))
                                        MOVE TABLE(VAR1) TO TEMP
                                        MOVE TABLE(VAR2) TO TABLE(VAR1)
                                        MOVE TEMP TO TABLE(VAR2)
                                     END-IF
                        ADD 1 TO VAR2 GIVING VAR2
                       END-PERFORM
*
           ADD 1 TO VAR1 GIVING VAR1
           END-PERFORM.
*
           MOVE 0 TO VAR1.
*
           PERFORM UNTIL VAR1 = 5
           ADD 1 TO VAR1  
           DISPLAY  VAR1 ":=" TABLE(VAR1)
           END-PERFORM.
*        
GOBACK.

Output:

Code: Select all

    ENTER ANY FIVE NUMBERS:
    98
    25
    54
    55
    22

    AFTER SORTING:
    22
    25
    54
    55
    98
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “Programming Algorithms.”