Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set....

6
Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : www.2july- maths.co.uk 1. Start at the beginning of the data set. 2. Compare the first two elements, and if the first is greater than the second, swaps them. 3. Continue doing this for each pair of adjacent elements to the end of the data set. 4. Start again with the first two elements, repeating until no swaps have occurred on the last pass.

Transcript of Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set....

Page 1: Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali :  1.Start at the beginning of the data set. 2.Compare the first two elements,

Decision Maths 1

Sorting AlgorithmsBubble Sort

A V Ali : www.2july-maths.co.uk

1. Start at the beginning of the data set.

2. Compare the first two elements, and if the first is greater than the second, swaps them.

3. Continue doing this for each pair of adjacent elements to the end of the data set.

4. Start again with the first two elements, repeating until no swaps have occurred on the last pass.

Page 2: Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali :  1.Start at the beginning of the data set. 2.Compare the first two elements,

5 7 312

95

Bubble SortBubble Sort

Pass 1 5 7 312

95

5 7 312

95

5 5 312

97

5 5 312

97

5 5 12

3 97

Page 3: Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali :  1.Start at the beginning of the data set. 2.Compare the first two elements,

5 5 97 12

3

Pass 2 5 5 93 12

7

5 5 93 12

7

5 5 93 12

7

5 5 97 12

3

Page 4: Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali :  1.Start at the beginning of the data set. 2.Compare the first two elements,

5 5 93 12

7

Pass 3 5 5 97 12

3

5 5 97 12

3

5 3 97 12

5

Page 5: Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali :  1.Start at the beginning of the data set. 2.Compare the first two elements,

5 3 95 12

7

Pass 4 5 3 97 12

5

3 5 97 12

5

Page 6: Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali :  1.Start at the beginning of the data set. 2.Compare the first two elements,

3 5 95 12

7

Pass 5 3 5 97 12

5

3 5 97 12

5 Sorted !!!