bubble sort...?

x5452

New Member
Hi, there

2 questions:

1. what is a bubble sort?

2. how do/code/write a bubble sort?

Thankz
X5452
 

lord_icon

Member
Bubble sort is the most efficient sorting method, and generally how Progress sorts anyway, indexes aside. The basic bubble sort is this;
Read serially each record, ie place each record from the db into the buffer.
Apply the required comparison criteria.
ie does row nbr = required var OR
does row name = required var OR required checks
This comparison process is dependant upon the required criteria, eg field1 = whatever, throughout the row / record.
If the row / record is a match to the requirements then that row floats to the top like a bubble.
This process is iterative to the next db row and record. The process continues throughout the db until all rows with required criteria have floated to the top of the db like a bubble.
How this realWorld syntax helps to explain the process
 

lord_icon

Member
Generally how Progress works > where did you get that from

Example, indexes aside, this is how Progress is working:

FOR EACH table NO-LOCK:
GET row from db
does the row criteria = desired criteria
if true but row into buffer
else dismiss
END.

a buffer has been bubble sorted with all matchin rows.
 

Cecil

19+ years progress programming and still learning.
Here are my two examples of a bubble sort. The first is a simple sort. The second sorts by item by every 2nd value.

The code is using ENTRY which is slow using arrays would be better.

First Code:
Code:
FUNCTION BubbleSort RETURNS CHARACTER
  (INPUT pcArray AS CHARACTER): 
  
  DEFINE VARIABLE cTempArray AS CHARACTER NO-UNDO.
  DEFINE VARIABLE i          AS INTEGER   NO-UNDO.
  DEFINE VARIABLE j          AS INTEGER   NO-UNDO.
  DEFINE VARIABLE iEntries   AS INTEGER   NO-UNDO.

  iEntries = NUM-ENTRIES(pcArray).

  DO i = iEntries TO 1 BY -1:
    DO j = 1 TO i - 1:
      IF ENTRY(j, pcArray) > ENTRY(j + 1, pcArray) THEN
      DO:
        cTempArray            = ENTRY(j, pcArray).
        ENTRY(j, pcArray)     = ENTRY(j + 1, pcArray).
        ENTRY(j + 1, pcArray) = cTempArray.
      END.
    END.
  END.

  RETURN pcArray.

END FUNCTION.
Second Code:
Code:
FUNCTION pairedBubbleSort RETURNS CHARACTER
  (INPUT pcArray AS CHARACTER): 
  
  DEFINE VARIABLE cTempArray AS CHARACTER NO-UNDO EXTENT 2.
  DEFINE VARIABLE i         AS INTEGER    NO-UNDO.
  DEFINE VARIABLE j         AS INTEGER    NO-UNDO.
  DEFINE VARIABLE iEntries  AS INTEGER    NO-UNDO.

  pcArray = TRIM(pcArray).
  pcArray = REPLACE(pcArray,CHR(32),CHR(0)).

  iEntries = NUM-ENTRIES(pcArray).

  IF iEntries MOD 2 <> 0 THEN
    RETURN pcArray.
                                                                  
  DO i = iEntries TO 1 BY -2:

    DO j = 1 TO i - 2 BY 2:

      IF ENTRY(j + 1, pcArray) > ENTRY(j + 3, pcArray)  THEN
      DO:
        cTempArray[1]         = ENTRY(j,     pcArray).
        cTempArray[2]         = ENTRY(j + 1, pcArray).

        ENTRY(j ,    pcArray) = ENTRY(j + 2, pcArray).
        ENTRY(j + 1, pcArray) = ENTRY(j + 3, pcArray).

        ENTRY(j + 2, pcArray) = cTempArray[1].
        ENTRY(j + 3, pcArray) = cTempArray[2].
        
      END.
    END.
  END.

  RETURN pcArray.

END FUNCTION.
 
Top