SlideServe
Menu
Préférences
© SlideServe · v2026

Array Algorithms Essentials: Counting, Searching & Sorting

leona avatar
10 diapositives · 231 Views · Sep 15, 2014

Learn fundamental algorithms on arrays: counting the elements that satisfy a condition, searching for an element, and sorting the array. Get hands-on training with step-by-step instructions. Enhance your programming skills with practical exercises.

Array Algorithms Essentials: Counting, Searching & Sorting - slide 1
01 / 10Array Algorithms Essentials: Counting, Searching & Sorting
1 / 17

Télécharger la présentation

Array Algorithms Essentials: Counting, Searching & Sorting
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. Programming Training Main Points: - More Fundamental algorithms on Arrays.

  2. The counting problem The counting problem: If a=(a[i], i=0,1,…,n-1) is an array with n elements find the number of elements that satisfy a condition C. Inputs: n, a=(a[i], i=0,1,…,n-1). Output: nr. How to do it: Step 1. (initialization) nr=0; Step 2. (repetition) repeat for i=0,1,2,…,n-1 test if a[i] satisfies C and then increase nr;

  3. COUNT HOW NAMY NUMBERS ARE POSITIVE int count_positives_array(int n, int a[]) { int i, nr; // initialization step nr = 0; // reapeatition step for(i=0;i<n;i++) if(a[i]>0) // or test if a[i] holds a condition { nr++ } return nr; }

  4. The searching problem The searching problem: If a=(a[i], i=0,1,…,n-1) is an array with n elements find whether the element X is in the array and find its position. Inputs: n, a=(a[i], i=0,1,…,n-1). Output: pos. How to do it: Step 1. (initialization) pos=-1; Step 2. (repetition) repeat for i=0,1,2,…,n-1 test if a[i]==X then record the position pos=i;

  5. int linear_search(int n, int a[], int X) { int i, pos; // initialisation step pos=-1; // repetition step for(i=0;i<n;i++) { if(a[i]==X) { pos=i; break; } } return pos; }

  6. The Sorting problem The Sorting problem: If a=(a[i], i=0,1,…,n-1) is an array with n elements then reorganise the elements so that a[0] < a[1]<…<a[n-1]. Swap two variables a and b using a tmp: tmp=a; a=b; b=tmp; Compare and Exchange two elements a and b. Denote this operation as a↔b if(a>b) { tmp=a; a=b; b=tmp; }

  7. The Sorting problem Inputs: n, a=(a[i], i=0,1,…,n-1). Output: a=(a[i], i=0,1,…,n-1). How to do it: repeat the following chains of compare and exchange: a[0] ↔a[1] ↔a[2] ↔ … ↔a[n-2] ↔a[n-1] a[0] ↔a[1] ↔a[2] ↔ … ↔a[n-3] ↔a[n-2] …. a[0] ↔a[1] ↔a[2] a[0] ↔a[1]

  8. The Sorting problem How to do it: repeat for i=n-1, n-2, …, 1 // a[0] ↔a[1] ↔a[2] ↔ … ↔a[i] repeat for j=0, 1, …,i-1 a[j] ↔a[j+1]

  9. void sort_array(int n, int a[]) { int i, j; for(i=n-1;i>0;i--) { for(j=0;j<i-1;j++) { if(a[j]>a[j+1]) { tmp=a[i];a[i]=a[i+1];a[i+1]=tmp; } } } return ; }

  10. To do List • Solve the HW problems.