Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Damian Gordon.

Similar presentations


Presentation on theme: "Sorting Damian Gordon."— Presentation transcript:

1 Sorting Damian Gordon

2 Sorting Let’s remember our integer array from before:

3 Sorting 44 23 1 42 2 33 3 16 4 54 5 6 34 7 18 Age

4 Sorting 44 23 1 42 2 33 3 16 4 54 5 34 6 18 7 Age How do we sort the data, in other words, get it into this order:

5 Sorting 44 23 1 42 2 33 3 16 4 54 5 6 34 7 18 Age How do we sort the data, in other words, get it into this order: 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

6 Sorting As humans, we can sort the array just by inspection (just be looking at it), but if the array was 100,000 elements long it would be more of a challenge for us.

7 Sorting: Bubble Sort The simplest algorithm for sort an array is called BUBBLE SORT.

8 Sorting: Bubble Sort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows:

9 Sorting: Bubble Sort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for an array of size N: Look at the first and second element Are they in order? If so, do nothing If not, swap them around Look at the second and third element Do the same Keep doing this until you get to the end of the array Go back to the start again keep doing this whole process for N times.

10 Sorting: Bubble Sort Lets look at the swapping bit
if I wanted to swap two values, the following won’t work: Age[0] <- Age[1]; Age[1] <- Age[0]; Why not?

11 Sorting: Bubble Sort Lets assume Age[0]=44, and Age[1]=23, if we do the following: Age[0] <- Age[1]; Age[1] <- Age[0]; What happens is:

12 Sorting: Bubble Sort Lets assume Age[0]=44, and Age[1]=23, if we do the following: Age[0] <- Age[1]; Age[1] <- Age[0]; What happens is: 23

13 Sorting: Bubble Sort Lets assume Age[0]=44, and Age[1]=23, if we do the following: Age[0] <- Age[1]; Age[1] <- Age[0]; What happens is: 23 23

14 Sorting: Bubble Sort Lets assume Age[0]=44, and Age[1]=23, if we do the following: Age[0] <- Age[1]; Age[1] <- Age[0]; What happens is: 23 23 23

15 Sorting: Bubble Sort Lets assume Age[0]=44, and Age[1]=23, if we do the following: Age[0] <- Age[1]; Age[1] <- Age[0]; What happens is: 23 23 23 23

16 NOT a Successful swap Sorting: Bubble Sort
Lets assume Age[0]=44, and Age[1]=23, if we do the following: Age[0] <- Age[1]; Age[1] <- Age[0]; What happens is: NOT a Successful swap 23 23 23 23

17 NOT a Successful swap Age[0]=23 Age[1]=23
Sorting: Bubble Sort Lets assume Age[0]=44, and Age[1]=23, if we do the following: Age[0] <- Age[1]; Age[1] <- Age[0]; What happens is: NOT a Successful swap Age[0]=23 Age[1]=23 23 23 23 23

18 Sorting: Bubble Sort We need an extra variable to make this work:

19 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value.

20 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value. Temp_Value <- Age[1];

21 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0];

22 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value;

23 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value; 23

24 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value; 23 23

25 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value; 23 23 44

26 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value; 23 23 44 44

27 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value; 23 23 44 44 23

28 Sorting: Bubble Sort We need an extra variable to make this work:
Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value; 23 23 44 44 23 23

29 Successful swap Sorting: Bubble Sort
We need an extra variable to make this work: Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value; 23 23 Successful swap 44 44 23 23

30 Age[0]=23 Age[1]=44 Successful swap
Sorting: Bubble Sort We need an extra variable to make this work: Lets call it Temp_Value. Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value; 23 23 Age[0]=23 Age[1]=44 Successful swap 44 44 23 23

31 Sorting: Bubble Sort Let’s wrap an IF statement around this:
IF (Age[1] < Age[0]) THEN Temp_Value <- Age[1]; Age[1] <- Age[0]; Age[0] <- Temp_Value; ENDIF;

32 Sorting: Bubble Sort And in general: IF (Age[N+1] < Age[N])
THEN Temp_Value <- Age[N+1]; Age[N+1] <- Age[N]; Age[N] <- Temp_Value; ENDIF;

33 Sorting: Bubble Sort Let’s replace “N” with “Index”
IF (Age[Index+1] < Age[Index]) THEN Temp_Value <- Age[Index+1]; Age[Index+1] <- Age[Index]; Age[Index] <- Temp_Value; ENDIF;

34 Sorting: Bubble Sort To get from one end of the array to another:
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;

35 Sorting: Bubble Sort Does this mean we have the array sorted?

36 Sorting: Bubble Sort Does this mean we have the array sorted? No

37 Sorting: Bubble Sort 44 23 1 42 2 33 3 16 4 54 5 6 34 7 18 Age

38 Sorting: Bubble Sort 23 44 1 42 2 33 3 16 4 54 5 6 34 7 18 Age

39 Sorting: Bubble Sort 23 44 1 42 2 33 3 16 4 54 5 6 34 7 18 Age

40 Sorting: Bubble Sort 23 42 1 44 2 33 3 16 4 54 5 6 34 7 18 Age

41 Sorting: Bubble Sort 23 42 1 44 2 33 3 16 4 54 5 6 34 7 18 Age

42 Sorting: Bubble Sort 23 42 1 33 2 44 3 16 4 54 5 6 34 7 18 Age

43 Sorting: Bubble Sort 23 42 1 33 2 44 3 16 4 54 5 6 34 7 18 Age

44 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 54 5 6 34 7 18 Age

45 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 54 5 6 34 7 18 Age

46 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 54 5 6 34 7 18 Age

47 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 54 5 6 34 7 18 Age

48 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 34 5 6 54 7 18 Age

49 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 34 5 6 54 7 18 Age

50 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 34 5 6 18 7 54 Age

51 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 34 5 6 18 7 54 Age

52 Sorting: Bubble Sort Age 23 42 1 33 2 16 3 44 4 34 5 18 6 54 7
23 42 1 33 2 16 3 44 4 34 5 18 6 54 7 Age So what happened?

53 Sorting: Bubble Sort Age 23 42 1 33 2 16 3 44 4 34 5 18 6 54 7
23 42 1 33 2 16 3 44 4 34 5 18 6 54 7 Age So what happened? We have moved the largest value (54) into the correct position.

54 Sorting: Bubble Sort Let’s do it again:

55 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 34 5 6 18 7 54 Age

56 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 34 5 6 18 7 54 Age

57 Sorting: Bubble Sort 23 42 1 33 2 16 3 44 4 34 5 6 18 7 54 Age

58 Sorting: Bubble Sort 23 33 1 42 2 16 3 44 4 34 5 6 18 7 54 Age

59 Sorting: Bubble Sort 23 33 1 42 2 16 3 44 4 34 5 6 18 7 54 Age

60 Sorting: Bubble Sort 23 33 1 16 2 42 3 44 4 34 5 6 18 7 54 Age

61 Sorting: Bubble Sort 23 33 1 16 2 42 3 44 4 34 5 6 18 7 54 Age

62 Sorting: Bubble Sort 23 33 1 16 2 42 3 44 4 34 5 6 18 7 54 Age

63 Sorting: Bubble Sort 23 33 1 16 2 42 3 44 4 34 5 6 18 7 54 Age

64 Sorting: Bubble Sort 23 33 1 16 2 42 3 34 4 44 5 6 18 7 54 Age

65 Sorting: Bubble Sort 23 33 1 16 2 42 3 34 4 44 5 6 18 7 54 Age

66 Sorting: Bubble Sort 23 33 1 16 2 42 3 34 4 18 5 6 44 7 54 Age

67 Sorting: Bubble Sort 23 33 1 16 2 42 3 34 4 18 5 6 44 7 54 Age

68 Sorting: Bubble Sort Age 23 1 33 2 16 3 42 4 34 5 18 6 44 7 54
23 1 33 2 16 3 42 4 34 5 18 6 44 7 54 Age So what happened?

69 Sorting: Bubble Sort Age 23 1 33 2 16 3 42 4 34 5 18 6 44 7 54
23 1 33 2 16 3 42 4 34 5 18 6 44 7 54 Age So what happened? We have moved the second largest value (44) into the correct position.

70 Sorting: Bubble Sort Let’s do it again:

71 Sorting: Bubble Sort 23 33 1 16 2 42 3 34 4 18 5 6 44 7 54 Age

72 Sorting: Bubble Sort 23 33 1 16 2 42 3 34 4 18 5 6 44 7 54 Age

73 Sorting: Bubble Sort 23 33 1 16 2 42 3 34 4 18 5 6 44 7 54 Age

74 Sorting: Bubble Sort 23 16 1 33 2 42 3 34 4 18 5 6 44 7 54 Age

75 Sorting: Bubble Sort 23 16 1 33 2 42 3 34 4 18 5 6 44 7 54 Age

76 Sorting: Bubble Sort 23 16 1 33 2 42 3 34 4 18 5 6 44 7 54 Age

77 Sorting: Bubble Sort 23 16 1 33 2 42 3 34 4 18 5 6 44 7 54 Age

78 Sorting: Bubble Sort 23 16 1 33 2 42 3 34 4 18 5 6 44 7 54 Age

79 Sorting: Bubble Sort 23 16 1 33 2 34 3 42 4 18 5 6 44 7 54 Age

80 Sorting: Bubble Sort 23 16 1 33 2 34 3 42 4 18 5 6 44 7 54 Age

81 Sorting: Bubble Sort 23 16 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

82 Sorting: Bubble Sort 23 16 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

83 Sorting: Bubble Sort 23 16 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

84 Sorting: Bubble Sort 23 16 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

85 Sorting: Bubble Sort Age 23 16 1 33 2 34 3 18 4 42 5 44 6 54 7
23 16 1 33 2 34 3 18 4 42 5 44 6 54 7 Age So what happened? We have moved the third largest value (42) into the correct position.

86 Sorting: Bubble Sort Let’s do it again:

87 Sorting: Bubble Sort 23 16 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

88 Sorting: Bubble Sort 16 23 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

89 Sorting: Bubble Sort 16 23 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

90 Sorting: Bubble Sort 16 23 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

91 Sorting: Bubble Sort 16 23 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

92 Sorting: Bubble Sort 16 23 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

93 Sorting: Bubble Sort 16 23 1 33 2 34 3 18 4 42 5 6 44 7 54 Age

94 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

95 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

96 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

97 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

98 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

99 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

100 Sorting: Bubble Sort Age 16 23 1 33 2 18 3 34 4 42 5 44 6 54 7
16 23 1 33 2 18 3 34 4 42 5 44 6 54 7 Age So what happened?

101 Sorting: Bubble Sort Age 16 23 1 33 2 18 3 34 4 42 5 44 6 54 7
16 23 1 33 2 18 3 34 4 42 5 44 6 54 7 Age So what happened? We have moved the fourth largest value (34) into the correct position.

102 Sorting: Bubble Sort Let’s do it again:

103 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

104 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

105 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

106 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

107 Sorting: Bubble Sort 16 23 1 33 2 18 3 34 4 42 5 6 44 7 54 Age

108 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

109 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

110 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

111 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

112 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

113 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

114 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

115 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

116 Sorting: Bubble Sort Age 16 23 1 18 2 33 3 34 4 42 5 44 6 54 7
16 23 1 18 2 33 3 34 4 42 5 44 6 54 7 Age So what happened? We have moved the fifth largest value (33) into the correct position.

117 Sorting: Bubble Sort Let’s do it again:

118 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

119 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

120 Sorting: Bubble Sort 16 23 1 18 2 33 3 34 4 42 5 6 44 7 54 Age

121 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

122 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

123 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

124 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

125 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

126 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

127 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

128 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

129 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

130 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

131 Sorting: Bubble Sort Age 16 18 1 23 2 33 3 34 4 42 5 44 6 54 7
16 18 1 23 2 33 3 34 4 42 5 44 6 54 7 Age So what happened?

132 Sorting: Bubble Sort Age 16 18 1 23 2 33 3 34 4 42 5 44 6 54 7
16 18 1 23 2 33 3 34 4 42 5 44 6 54 7 Age So what happened? We have moved the sixth largest value (23) into the correct position.

133 Sorting: Bubble Sort Let’s do it again:

134 Sorting: Bubble Sort Let’s do it again: Let’s not bother!

135 Sorting: Bubble Sort Let’s do it again: Let’s not bother! It’s sorted!

136 Sorting: Bubble Sort 16 18 1 23 2 33 3 34 4 42 5 6 44 7 54 Age

137 Sorting: Bubble Sort So we need two loops:

138 Sorting: Bubble Sort So we need two loops: 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;

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

140 Sorting: Bubble Sort

141 etc.


Download ppt "Sorting Damian Gordon."

Similar presentations


Ads by Google