SlideServe
Menu
Préférences
© SlideServe · v2026

List

hank avatar
17 diapositives · 352 Views · Aug 29, 2014

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

List - slide 1
01 / 17List
1 / 17

Télécharger la présentation

List
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Chapter 9 : List Arrays and Vectors Mechanism for representing lists

  2. 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

  3. 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); } }

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

  5. 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; }

  6. 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’

  7. 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; ... ...

  8. 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);

  9. ’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);

  10. 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);

  11. 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);

  12. 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);

  13. 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);

  14. 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; }

  15. 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

  16. 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

  17. 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