Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understanding BubbleSort CS-502 (EMC) Fall 20091 Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern.

Similar presentations


Presentation on theme: "Understanding BubbleSort CS-502 (EMC) Fall 20091 Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern."— Presentation transcript:

1 Understanding BubbleSort CS-502 (EMC) Fall 20091 Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern Operating Systems, 3 rd ed., by Andrew Tanenbaum and from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, & Gagne)

2 Understanding BubbleSort CS-502 (EMC) Fall 20092 BubbleSort Code void BubbleSort (int A[], const int arraySize) { int i, j; for(i = 0; i abs(A[j+1])) swap(A+j, A+j+1); }//void BubbleSort(…) void swap (int *a, int *b) { int temp = *a; *a = *b; *b = temp; }//void swap(…) Note use of pointer arguments!

3 Understanding BubbleSort CS-502 (EMC) Fall 20093 Swap is easy void swap (int *a, int *b) { int temp = *a; *a = *b; *b = temp; }//void swap(…) Put the value pointed to by a in the location pointed to by b Put the value pointed to by b in the location pointed to by a Notice how swap reaches back to the caller’s data and changes it

4 Understanding BubbleSort CS-502 (EMC) Fall 20094 Bubble Sort Algorithm void BubbleSort (int A[], const int arraySize) { int i, j; for(i = 0; i abs(A[j+1])) swap(A+j, A+j+1); }//void BubbleSort(…) Fundamental principle –Let the “lighter” elements bubble to the top and the “heavier” elements sink to the bottom

5 Understanding BubbleSort CS-502 (EMC) Fall 20095 BubbleSort Code void BubbleSort (int A[], const int arraySize) { int i, j; for(i = 0; i abs(A[j+1])) swap(A+j, A+j+1); }//void BubbleSort(…) Loop invariant (loop i ) –The largest i elements are located at the bottom of the array in order.

6 Understanding BubbleSort CS-502 (EMC) Fall 20096 BubbleSort Code (continued) void BubbleSort (int A[], const int arraySize) { int i, j; for(i = 0; i abs(A[j+1])) swap(A+j, A+j+1); }//void BubbleSort(…) Loop invariant (loop j ) –Absolute value of A[j+1] ≥ any of the elements A[0] … A[j].

7 Understanding BubbleSort CS-502 (EMC) Fall 20097 BubbleSort Code void BubbleSort (int A[], const int arraySize) { int i, j; for(i = 0; i abs(A[j+1])) swap(A+j, A+j+1); }//void BubbleSort(…) Loop invariant (loop i ) The largest i elements are located at the bottom of the array in order. Loop invariant (loop j ) Absolute value of A[j+1] ≥ any of the elements A[0] … A[j]. Does invariant of loop j preserve the invariant of loop i ? Why?

8 Understanding BubbleSort CS-502 (EMC) Fall 20098 Answer Loop invariant i is true for i == 0 By inspection Suppose loop invariant i is true for some value i > 0 Is it true for i + 1? From this, can we conclude that it is true for the entire array?

9 Understanding BubbleSort CS-502 (EMC) Fall 20099 Questions?


Download ppt "Understanding BubbleSort CS-502 (EMC) Fall 20091 Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern."

Similar presentations


Ads by Google