Trace Table Exercise: Efficient Bubble Sort

Trace the state of the array after each full pass of the FOR loop. Array: [5, 2, 8, 1, 3]

DECLARE Data : ARRAY[1:5] OF INTEGER
DECLARE Temp, Index : INTEGER
DECLARE Swapped : BOOLEAN
Data ← [5, 2, 8, 1, 3]
Swapped ← TRUE

WHILE Swapped = TRUE DO
    Swapped ← FALSE
    FOR Index ← 1 TO 4 DO
        IF Data[Index] > Data[Index + 1] THEN
            Temp ← Data[Index]
            Data[Index] ← Data[Index + 1]
            Data[Index + 1] ← Temp
            Swapped ← TRUE
        ENDIF
    NEXT Index
ENDWHILE
	
PassD[1]D[2]D[3]D[4]D[5]Swapped Flag
1
2
3
4