170 likes | 329 Views
Chapter 9 :. List. Arrays and Vectors. Mechanism for representing lists. Key Concepts:. one-dimensional arrays array subscripting arrays as parameters array element as parameters character string Standard Template Library (STL) container class template class vector
E N D
Chapter 9 : List Arrays and Vectors Mechanism for representing lists
Key Concepts: • 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
QuickSort • 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<char> &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); } }
Picking The Pivot Element void Pivot(vector<char> &A, int left, int right) { if (A[left] > A[right]) { Swap(A[left], A[right]); } }
Decomposing Into Sublists int Partition(vector<char> &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; }
0 1 2 3 4 5 6 7 8 9 ’W’ ’I’ ’R’ ’T’ ’Y’ ’U’ ’E’ ’O’ QuickSort(B, 0, B.size()-1) right = 9 left = 0 Executing of QuickSort void QuickSort(vector<char> &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); } } B: ’Q’ ’P’
i = 0 j = 10 0 1 2 3 4 5 6 7 8 9 10 A: ’P’ ’W’ ’I’ ’R’ ’T’ ’Y’ ’U’ ’E’ ’O’ ’Q’ pivot Executing of QuickSort In the function Partition() int Partition(vector<char> &A, int left, int right) { charpivot = A[left]; inti = left; intj = right+1; ... ...
A: pivot j i 0 1 2 3 4 5 6 7 8 9 10 ’P’ ’W’ ’I’ ’R’ ’T’ ’Y’ ’U’ ’E’ ’O’ ’Q’ Executing of QuickSort do { do++i;while(A[i] < pivot); do--j;while(A[j] > pivot); } while (i < j);
’W’ ’O’ A: A: pivot pivot j i 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 ’P’ ’P’ ’W’ ’I’ ’I’ ’R’ ’R’ ’T’ ’T’ ’Y’ ’Y’ ’U’ ’U’ ’E’ ’E’ ’O’ ’Q’ ’Q’ i = 1 j = 8 Executing of QuickSort do { do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); if(i < j) { Swap(A[i], A[j]); } ’W’ ’O’ } while (i < j);
A: pivot j i 0 1 2 3 4 5 6 7 8 9 10 ’P’ ’O’ ’I’ ’R’ ’T’ ’Y’ ’U’ ’E’ ’W’ ’Q’ Executing of QuickSort do { do++i;while(A[i] < pivot); do--j;while(A[j] > pivot); } while (i < j);
A: A: pivot pivot j i 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 ’P’ ’P’ ’O’ ’O’ ’I’ ’I’ ’R’ ’T’ ’T’ ’Y’ ’Y’ ’U’ ’U’ ’I’ ’W’ ’W’ ’Q’ ’Q’ i = 3 j = 7 Executing of QuickSort do { do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); ’R’ ’E’ if(i < j) { Swap(A[i], A[j]); } ’R’ ’E’ } while (i < j);
A: pivot j i 0 1 2 3 4 5 6 7 8 9 10 ’P’ ’O’ ’I’ ’E’ ’T’ ’Y’ ’U’ ’R’ ’W’ ’Q’ Executing of QuickSort do { do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); } while (i < j);
A: A: pivot pivot j i 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 ’P’ ’P’ ’O’ ’O’ ’I’ ’I’ ’I’ ’T’ ’T’ ’Y’ ’Y’ ’U’ ’U’ ’R’ ’R’ ’W’ ’W’ ’Q’ ’Q’ j=3 i=4 Executing of QuickSort do { do ++i; while (A[i] < pivot); do --j; while (A[j] > pivot); ’E’ ’T’ if (i < j) { Swap(A[i], A[j]); } ’T’ ’E’ } while (i < j);
0 1 2 3 4 5 6 7 8 9 10 ’O’ ’I’ ’T’ ’Y’ ’U’ ’R’ ’W’ ’Q’ j=3 left i=4 Executing of QuickSort int Partition(vector<char> &A, int left, int right) { ... ... do{ ... ... } while (i < j); Swap(A[j], A[left]); pivot A: ’E’ ’P’ pivot return j; }
left=4 right=2 left=0 right=9 ’O’ ’I’ ’T’ ’Y’ ’U’ ’R’ ’W’ ’E’ ’Q’ k Executing of QuickSort void QuickSort(vector<char> &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); } } pivot QuickSort(A, 0, 2); QuickSort(A, 4, 9); A: ’P’ 0 1 2 3 4 5 6 7 8 9
9 … 9 8 … 9 7 … 9 8 … 7 5 … 9 7 … 6 4 … 9 5 … 5 4 … 3 2 … 2 1 … 2 0 … 2 1 … 0 0 … -1 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 O I P T Y U R W Q E O I P T Y U R W Q E I O P T Y U R W Q E I O 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 Y U R W T E I O P Q R T U W Y E I O P Q R T U W Y E I O P Q R T U W Y E I O P Q R T U W Y E I O P Q R T U W Y E I O P Q R T U W Y 0 … 9
Home works Exercises 9.43 Exercises: Read the section 9.11 of textbook (Page525~548) carefully and complete the game Maze Runner End of Chapter 9