sorting: optimising bubblesort damian gordon. sorting: bubble sort if we look at the bubble sort...

25
Sorting: Optimising Bubblesort Damian Gordon

Upload: gillian-nelson

Post on 04-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting:Optimising Bubblesort

Damian Gordon

Page 2: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• If we look at the bubble sort algorithm again:

Page 3: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

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;

ENDFOR;END.

Sorting: Bubble Sort

Page 4: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 5: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 6: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 7: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 8: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 9: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 10: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 11: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 12: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 13: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The bubble sort pushes the largest values up to the top of the array.

Page 14: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

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.

Page 15: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

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

Page 16: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

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

Page 17: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• 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.

Page 18: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble SortPROGRAM 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.

Page 19: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

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

Page 20: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

Sorting: Bubble Sort

• The Swap function is very useful so we should have that as a method as follows:

Page 21: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

MODULE SWAP[A,B]: Integer Temp_Value Temp_Value <- B;

B <- A; A <- Temp_Value;RETURN A, B;

END.

Sorting: Bubble Sort

Page 22: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

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

Page 23: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

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

Page 24: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

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

Page 25: Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:

etc.