Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11,...

16
Bubble Sort

Transcript of Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11,...

Page 1: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Bubble Sort

Page 2: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Bubble Sort Example9, 6, 2, 12, 11, 9, 3, 76, 9, 2, 12, 11, 9, 3, 76, 2, 9, 12, 11, 9, 3, 76, 2, 9, 12, 11, 9, 3, 76, 2, 9, 11, 12, 9, 3, 76, 2, 9, 11, 9, 12, 3, 76, 2, 9, 11, 9, 3, 12, 76, 2, 9, 11, 9, 3, 7, 12The 12 is greater than the 7 so they are exchanged.The 12 is greater than the 7 so they are exchanged.

The 12 is greater than the 3 so they are exchanged.The 12 is greater than the 3 so they are exchanged.

The twelve is greater than the 9 so they are exchangedThe twelve is greater than the 9 so they are exchanged

The 12 is larger than the 11 so they are exchanged.The 12 is larger than the 11 so they are exchanged.

In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list.

In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list.

Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged.

Now the next pair of numbers are compared. Again the 9 is the larger and so this pair is also exchanged.

Bubblesort compares the numbers in pairs from left to right exchanging when necessary. Here the first number is compared to the second and as it is larger they are exchanged.

Bubblesort compares the numbers in pairs from left to right exchanging when necessary. Here the first number is compared to the second and as it is larger they are exchanged.

The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.

The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.

Page 3: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Bubble Sort Example

6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 11, 9, 3, 7, 122, 6, 9, 9, 11, 3, 7, 122, 6, 9, 9, 3, 11, 7, 122, 6, 9, 9, 3, 7, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons.

Notice that this time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons.

First Pass

Second Pass

Page 4: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Bubble Sort Example

2, 6, 9, 9, 3, 7, 11, 122, 6, 9, 3, 9, 7, 11, 122, 6, 9, 3, 7, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.

This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.

Page 5: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Bubble Sort Example

2, 6, 9, 3, 7, 9, 11, 122, 6, 3, 9, 7, 9, 11, 122, 6, 3, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

Each pass requires fewer comparisons. This time only 4 are needed.Each pass requires fewer comparisons. This time only 4 are needed.

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass

Page 6: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Bubble Sort Example

2, 6, 3, 7, 9, 9, 11, 122, 3, 6, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges.

The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges.

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass

2, 6, 3, 7, 9, 9, 11, 12Fifth Pass

Page 7: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Bubble Sort Example

2, 3, 6, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass

2, 6, 3, 7, 9, 9, 11, 12Fifth Pass

Sixth Pass2, 3, 6, 7, 9, 9, 11, 12

This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.

This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.

Page 8: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Bubble Sort ExampleQuiz Time

1. Which number is definitely in its correct position at the end of the first pass?

Answer: The last number must be the largest.

Answer: Each pass requires one fewer comparison than the last.

Answer: When a pass with no exchanges occurs.

2. How does the number of comparisons required change as the pass number increases?

3. How does the algorithm know when the list is sorted?

4. What is the maximum number of comparisons required for a list of 10 numbers?

Answer: 9 comparisons, then 8, 7, 6, 5, 4, 3, 2, 1 so total 45

Page 9: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Exercise: Bubble Sort• Write a function bsort1(int A[], int n)and apply the bubble sort algorithm to sort elements in array A in ascending order.

• You may test your function with the following main program.

int main(){    int a[] = { 1, 3, 5, 7, 2, 4, 6 };    int size = sizeof(a) / sizeof(a[0]);    bsort1(a, size);    print_array(a, size);       return 0;}

Page 10: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

bsort1

• Upload your function to Moodle.• The earlier you upload a correct code, the

higher score you will obtain.

Page 11: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

bsort2• Write a function bsort2(int A[], int n)and apply the bubble sort algorithm to sort elements in array A in descending order.

• You may test your function with the following main program.

int main(){    int a[] = { 1, 3, 5, 7, 2, 4, 6 };    int size = sizeof(a) / sizeof(a[0]);    bsort1(a, size);    print_array(a, size); bsort2(a, size);    print_array(a, size);        return 0;}

Page 12: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

bsort3• Write a function bsort3(int A[], int n)which will sort the array A so that even numbers will be in front of odd numbers.

• For example,– { 1, 3, 5, 7, 2, 4, 6 } → {2, 4, 6, 1, 3, 5, 7 }– { 7, 3, 5, 1, 6, 4, 2} → {6, 4, 2, 7, 3, 5, 1 }

Page 13: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

bsort4• Write a function bsort4(int A[], int n)which will sort the array A so that odd numbers will be in front of even numbers.

• For example,– {2, 4, 6, 1, 3, 5, 7 } → { 1, 3, 5, 7, 2, 4, 6 }– {6, 4, 2, 1, 3, 5, 7 } →{ 1, 3, 5, 7, 6, 4, 2}

Page 14: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

bsort5• Write a function bsort5(int A[], int n)which will sort the array A so that even numbers will be in front of odd numbers.– Moreover, the sequences of odd numbers and even

numbers are also independently sorted in ascending order.

• For example,– { 1, 3, 5, 7, 6, 4, 2} → {2, 4, 6, 1, 3, 5, 7 }

Page 15: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

bsort6• Write a function bsort6(int A[], int n)which will sort the array A so that odd numbers will be in front of even numbers.– Moreover, the sequences of odd numbers and even

numbers are also independently sorted in ascending order.

• For example,– {6, 4, 2, 1, 3, 5, 7 } →{ 1, 3, 5, 7, 2, 4, 6}

Page 16: Bubble Sort. Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 6, 2, 9, 12, 11, 9, 3, 7 6, 2, 9, 11, 12, 9, 3, 7 6, 2, 9, 11, 9, 12,

Pointer to Functions

• A simpler way:– You may define 6 “comparison” functions to do this:

int bsort(int a[], int n, int (*pfun)(int a, int b) ){ int i, j, temp; for (i=n-1; i>=1; i--) for (j=0; j<i; j++) if (pfun(a[j] , a[j+1]) > 0 ) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } return 0;}