Download presentation
Presentation is loading. Please wait.
Published byGillian Nelson Modified over 9 years ago
1
Sorting: Optimising Bubblesort Damian Gordon
2
Sorting: Bubble Sort If we look at the bubble sort algorithm again:
3
PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO N-2 DO IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; ENDIF; ENDFOR; END. Sorting: Bubble Sort
4
The bubble sort pushes the largest values up to the top of the array.
5
Sorting: Bubble Sort The bubble sort pushes the largest values up to the top of the array.
6
Sorting: Bubble Sort The bubble sort pushes the largest values up to the top of the array.
7
Sorting: Bubble Sort The bubble sort pushes the largest values up to the top of the array.
8
Sorting: Bubble Sort The bubble sort pushes the largest values up to the top of the array.
9
Sorting: Bubble Sort The bubble sort pushes the largest values up to the top of the array.
10
Sorting: Bubble Sort The bubble sort pushes the largest values up to the top of the array.
11
Sorting: Bubble Sort The bubble sort pushes the largest values up to the top of the array.
12
Sorting: Bubble Sort The bubble sort pushes the largest values up to the top of the array.
13
Sorting: Bubble Sort The bubble sort pushes the largest values up to the top of the array.
14
Sorting: Bubble Sort So each time around the loop the amount of the array that is sorted is increased, and we don’t have to check for swaps in the locations that have already been sorted. So we reduce the checking of swaps by one each time we do a pass of the array.
15
PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; ReducingIndex <- N-2; FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO ReducingIndex DO IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; ENDIF; ENDFOR; ReducingIndex <- ReducingIndex – 1; ENDFOR; END. Sorting: Bubble Sort
16
PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; ReducingIndex <- N-2; FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO ReducingIndex DO IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; ENDIF; ENDFOR; ReducingIndex <- ReducingIndex – 1; ENDFOR; END. Sorting: Bubble Sort
17
Also, what if the data is already sorted? We should check if the program has done no swaps in one pass, and if t doesn’t that means the data is sorted. So even if the data started unsorted, as soon as the data gets sorted we want to exit the program.
18
Sorting: Bubble Sort PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; ReducingIndex <- N-2; DidSwap <- FALSE; FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO ReducingIndex DO IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; DidSwap <- TRUE; ENDIF; ENDFOR; ReducingIndex <- ReducingIndex – 1; IF (DidSwap = FALSE) THEN EXIT; ENDIF; ENDFOR; END.
19
PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; ReducingIndex <- N-2; DidSwap <- FALSE; FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO ReducingIndex DO IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; DidSwap <- TRUE; ENDIF; ENDFOR; ReducingIndex <- ReducingIndex – 1; IF (DidSwap = FALSE) THEN EXIT; ENDIF; ENDFOR; END. Sorting: Bubble Sort
20
The Swap function is very useful so we should have that as a method as follows:
21
MODULE SWAP[A,B]: Integer Temp_Value Temp_Value <- B; B <- A; A <- Temp_Value; RETURN A, B; END. Sorting: Bubble Sort
22
PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; ReducingIndex <- N-2; DidSwap <- FALSE; FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO ReducingIndex DO IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; DidSwap <- TRUE; ENDIF; ENDFOR; ReducingIndex <- ReducingIndex – 1; IF (DidSwap = FALSE) THEN EXIT; ENDIF; ENDFOR; END. Sorting: Bubble Sort
23
PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; ReducingIndex <- N-2; DidSwap <- FALSE; FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO ReducingIndex DO IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; DidSwap <- TRUE; ENDIF; ENDFOR; ReducingIndex <- ReducingIndex – 1; IF (DidSwap = FALSE) THEN EXIT; ENDIF; ENDFOR; END. Sorting: Bubble Sort
24
PROGRAM BubbleSort: Integer Age[8] <- {44,23,42,33,16,54,34,18}; ReducingIndex <- N-2; DidSwap <- FALSE; FOR Outer-Index IN 0 TO N-1 DO FOR Index IN 0 TO ReducingIndex DO IF (Age[Index+1] < Age[Index]) THEN SWAP(Age[Index], Age[Index+1]; DidSwap <- TRUE; ENDIF; ENDFOR; ReducingIndex <- ReducingIndex – 1; IF (DidSwap = FALSE) THEN EXIT; ENDIF; ENDFOR; END. Sorting: Bubble Sort
25
etc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.