Presentation is loading. Please wait.

Presentation is loading. Please wait.

List Chapter 9 : Arrays and Vectors Mechanism for representing lists.

Similar presentations


Presentation on theme: "List Chapter 9 : Arrays and Vectors Mechanism for representing lists."— Presentation transcript:

1 List Chapter 9 : Arrays and Vectors Mechanism for representing lists

2 one-dimensional arrays array subscripting arrays as parameters array element as parameters character string Standard Template Library (STL) container class template class vector vector subscripting vector resizing string subscripting iterators iterator dereferencing vector of vector sorting function InsertionSort() function QuickSort() searching function BinarySearch() algorithm library function find() table matrices member initialization list multidimensional arrays Key Concepts:

3 QuickSort Divide the list into sublists such that every element in the left sublist <= to every element in the right sublist Repeat the QuickSort process on the sublists void QuickSort(vector &A, int left, int right) { if (left < right) { Pivot(A, left, right); int k = Partition(A, left, right); QuickSort(A, left, k-1); QuickSort(A, k+1, right); }

4 Picking The Pivot Element void Pivot(vector &A, int left, int right) { if (A[left] > A[right]) { Swap(A[left], A[right]); }

5 Decomposing Into Sublists int Partition(vector &A, int left, int right) { char pivot = A[left]; int i = left; int j = right+1; do { do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); if (i < j) { Swap(A[i], A[j]); } } while (i < j); Swap(A[j], A[left]); return j; }

6 Executing of QuickSort void QuickSort(vector &A, int left, int right) { if (left < right) { Pivot(A, left, right); int k = Partition(A, left, right); QuickSort(A, left, k-1); QuickSort(A, k+1, right); } ’W’’I’’R’’T’’Y’’U’’E’’O’ B: ’P’ left = 0right = 9 QuickSort(B, 0, B.size()-1) 0123456789 ’Q’

7 Executing of QuickSort char pivot = A[left]; int i = left; int j = right+1; In the function Partition() ’P’’W’’I’’R’’T’’Y’’U’’E’’O’’Q’ A: i = 0 pivot j = 10 012345678910 int Partition(vector &A, int left, int right) {......

8 ’P’’W’’I’’R’’T’’Y’’U’’E’’O’’Q’ A: pivot 012345678910 Executing of QuickSort i do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); j do { } while (i < j);

9 ’P’’I’’R’’T’’Y’’U’’E’’Q’ A: pivot 012345678910 Executing of QuickSort do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); j = 8i = 1 if (i < j) { Swap(A[i], A[j]); } ’O’’W’ do { } while (i < j); ’P’’W’’I’’R’’T’’Y’’U’’E’’O’’Q’ A: pivot 012345678910 i j ’O’’W’

10 ’P’’O’’I’’R’’T’’Y’’U’’E’’W’’Q’ A: pivot 012345678910 Executing of QuickSort i do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); j do { } while (i < j);

11 ’P’’O’’I’’T’’Y’’U’’W’’Q’ A: pivot 012345678910 ’P’’O’’I’’R’’T’’Y’’U’’I’’W’’Q’ A: pivot 012345678910 Executing of QuickSort i do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); j = 7i = 3 if (i < j) { Swap(A[i], A[j]); } ’E’ j ’R’ do { } while (i < j); ’E’’R’

12 ’P’’O’’I’’E’’T’’Y’’U’’R’’W’’Q’ A: pivot 012345678910 Executing of QuickSort i do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); j do { } while (i < j);

13 ’P’’O’’I’’T’’Y’’U’’R’’W’’Q’ A: pivot 012345678910 ’P’’O’’I’ ’T’’Y’’U’’R’’W’’Q’ A: pivot 012345678910 Executing of QuickSort i do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); i=4 j=3 if (i < j) { Swap(A[i], A[j]); } j ’E’ do { } while (i < j); ’T’’E’ ’T’

14 ’O’’I’’T’’Y’’U’’R’’W’’Q’ A: pivot 012345678910 Executing of QuickSort i=4 j=3 } ’P’ ’E’ int Partition(vector &A, int left, int right) {...... do{...... } while (i < j); Swap(A[j], A[left]); left return j; pivot

15 Executing of QuickSort void QuickSort(vector &A, int left, int right) { if (left < right) { Pivot(A, left, right); int k = Partition(A, left, right); QuickSort(A, left, k-1); QuickSort(A, k+1, right); } ’P’ A: ’T’’Y’’U’’R’’W’ ’Q’ left=0right=9 QuickSort(A, 0, 2); 0123456789 ’O’’I’ ’E’ QuickSort(A, 4, 9); right=2left=4 pivot k

16 Sorting Q W I R T Y U E O P Q W I R T Y U E O P E O I P T Y U R W Q E I O P T Y U R W Q E I O P Q Y U R W T E I O P Q R T U W Y 0 … 9 5 … 9 4 … 3 4 … 9 0 … 2 1 … 2 0 … -1 2 … 2 1 … 0 7 … 9 5 … 5 8 … 9 7 … 6 8 … 7 9 … 9

17 End of Chapter 9 Exercises 9.43 Exercises: Read the section 9.11 of textbook (Page525~548) carefully and complete the game Maze Runner Home works


Download ppt "List Chapter 9 : Arrays and Vectors Mechanism for representing lists."

Similar presentations


Ads by Google