SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Downloaden Sie, um offline zu lesen
Algorithmes de tri
Tri : complexité
           minimale
• n! résultats possibles
• si tests binaires, on obtient un arbre de
  hauteur minimale
  log2 n! ~ log2 (n^n / e^n) ~ O(n log n)
• La complexité d’un tri sera donc au mieux
  en O(n log n)
The first pass has no effect in this example, because there is no element in the
       array smaller than the A at the left. On the second pass, the other A is the

                      Tri par sélection
   smallest remaining element, so it is exchanged with the S in the second position.
    Then, the E near the middle is exchanged with the O in the third position on the
   third pass; then, the other E is exchanged with the R in the fourth position on the

                      (selection sort)
                                fourth pass; and so forth.




           Récurrence sur le résultat (tableau de 1 à i-1 trié)
Program 6.12 is an implementation of selection sort that adheres to our conventions. The
inner loop is just a comparison to test a current element against the smallest element
found so far (plus the code necessary to increment the index of the current element and
to check that it does not exceed the array bounds); it could hardly be simpler. The work
of moving the items around falls outside the inner loop: each exchange puts an element
Tri par sélection
         (selection sort)
Pour chaque i de l à r-1, échanger a[i] avec l’élément
minimum de a[i], ..., a[r]. Quand l’index i va de gauche à
droite, les éléments à sa gauche sont à leur place finale dans le
tableau (et ne seront plus modifiés), donc le tableau est
complètement trié lorsque i arrive à l’extrémité droite.
static void selection(ITEM[] a, int l, int r)
  {
    for (int i = l; i < r; i++)
      { int min = i;
        for (int j = i+1; j <= r; j++)
          if (less(a[j], a[min])) min = j;
        exch(a, i, min);
      }
  }
During the first pass of insertion sort, the S in the second position is larger than
   the A, so it does not have to be moved. On the second pass, when the O in the
                         Tri par insertion
    third position is encountered, it is exchanged with the S to put A O S in sorted
   order, and so forth. Un-shaded elements that are not circled are those that were

                         (insertion sort)
                            moved one position to the right.




he implementation of insertion sort données (on insère l’élément i) but inefficien
                Récurrence sur les in Program 6.1 is straightforward,
 e shall now consider three ways to improve it in order to illustrate a recurrent them
 roughout many of our implementations: We want code to be succinct, clear, an
 ficient, but these goals sometimes conflict, so we must often strike a balance. We do s
Tri par insertion
           (insertion sort)
(i) met le plus petit élément du tableau à la première place pour que cet
élément serve de “sentinelle” ; (ii) une seule assignation, plutôt qu’un
échange, dans la boucle principale ; et (iii) la boucle interne se termine
quand l’élément inséré est à la bonne position. Pour chaque i, trie les
éléments a[l], ..., a[i] en déplaçant d’une position vers la droite les
éléments de la liste triée a[l], ..., a[i-1] qui sont plus grands que a[i],
place ensuite a[i] à la bonne position.

static void insertion(ITEM[] a, int l, int r)
  { int i;
    for (i = r; i > l; i--)
      if (less(a[i], a[i-1])) exch (a, i-1, i);
    for (i = l+2; i <= r; i++)
      { int j = i; ITEM v = a[i];
        while (less(v, a[j-1]))
           { a[j] = a[j-1]; j--; }
        a[j] = v;
      }
  }
ge A and S to get A S; then, we merge O and R to get O R; then, we merg
 with A S to get A O R S. Later, we merge I T with G N to get G I N T, then
erge this result with A O R S to get A G I N O R S T, and so on. The metho
               Tri fusion (Merge sort)
          recursively builds up small sorted files into larger ones.




wn mergesort isRécurrence dichotomique sur les donnéesstyle, where a m
                  analogous to a top-down management
n organization to take on a big task by dividing it into pieces to be
ndently by underlings. If each manager operates by simply dividing the give
 then putting together the solutions that the subordinates develop and pass
Tri fusion (Merge sort)

static void mergesort(ITEM[]   a,   int
l, int r)
  { if (r <= l) return;
    int m = (r+l)/2;
    mergesort(a, l, m);
    mergesort(a, m+1, r);
    merge(a, l, m, r);
  }
Tri par segmentation
                         (Quicksort, Hoare 1960)




  use the following general strategy to implement partitioning. First, we arbitra
ose a[r] to be the partitioning element—the one that will go into its final positi
 t, we scan from the left end of the array until we find an element greater than
titioning element, and we scan from the right end of the array until we find
Tri par segmentation
          (Quicksort, Hoare 1960)
•   Choisir un pivot v

•   Réarranger le tableau pour que tous les éléments
    plus petits que v soient à gauche, tous les plus
    grands à droite

•   Trier les deux sous-tableaux à droite et à gauche
    static void quicksort(ITEM[] a, int l, int r)
      {
        if (r <= l) return;
        int i = partition(a, l, r);
        quicksort(a, l, i-1);
        quicksort(a, i+1, r);
      }
Tri par segmentation : partition
                  (Quicksort, Hoare 1960)




ure 7.3. Dynamic characteristics of quicksort partition
Tri par segmentation
                  (Quicksort, Hoare 1960)
La variable v contient la valeur du pivot a[r], et i et j sont respectivement les indices
de parcours gauche et droit. La boucle de partition incrémente i et décrémente j, en
maintenant la propriété invariante : aucun élément à gauche de i n’est plus grand que v
et aucun élément à droite de j n’est plus petit que v. Quand les indices se rejoignent, on
termine le partitionnement en échangeant a[i] et a[r], ce qui met v dans a[i], avec
aucun élément plus grand que v à droite et aucun élément plus petit à gauche.
La boucle de partitionnement est codée comme une boucle infinie, avec un break quand
les indices se croisent. Le test j==l protège du cas où l’élément de partitionnement est le
plus petit du fichier.

static int partition(ITEM a[], int l, int r)
  { int i = l-1, j = r; ITEM v = a[r];
    for (;;)
      {
        while (less(a[++i], v)) ;
        while (less(v, a[--j])) if (j == l) break;
        if (i >= j) break;
        exch(a, i, j);
      }
    exch(a, i, r);
    return i;
  }
Tri : complexité
•   Tri par sélection : au plus n^2/2 comparaisons et n
    échanges

•   Tri par insertion : au plus n^2/4 comparaisons et
    n^2/4 affectations

•   Tri fusion : O(n log n) (démonstration à suivre)

•   Quicksort : n^2/2 comparaisons au pire,
    2n ln n comparaisons en moyenne,
    donc O(n log n). Partitionnement en O(n).
    Situation optimale : pivot au milieu, donc choix du
    pivot crucial
In addition to netting the improvements discussed in Section 8.2, a good Java VM
 implementation might avoid unnecessary array accesses to reduce the inner loop of
 mergesort to a comparison (with conditional branch), two index increments ( k and either
 i or j ), and a test with conditional branch for loop completion. The total number of
 instructions in such an inner loop is slightly higher than that for quicksort, but the
 instructions are executed only N lg N times, where quicksort's are executed 39 percent



Tri : temps d’exécution
 more often (or 29 percent with the median-of-three modification). Careful
 implementation and detailed analysis are required for more precise comparison of the
 algorithms in particular environments; nonetheless, we do know that mergesort has an
 inner loop that is slightly longer than that of quicksort.

              Table 8.1. Empirical study of mergesort algorithms
 These relative timings for various sorts on random files of integers indicate that
 standard quicksort is about twice as fast as standard mergesort; that adding a cutoff for
 small files lowers the running times of both bottom-up and top-down mergesort by
 about 15 percent; that top-down mergesort is about 10 percent faster than bottom-up
 mergesort for these file sizes; and that even eliminating the cost of the file copy (the
 method used in the Java system sort) leaves mergesort substantially slower than
 quicksort for randomly ordered files.
                        top-down                         bottom-up
 N              Q       T          T*         O          B           B*        S
 12500          23      27         16         19         30          20        19
 25000          20      43         34         27         42          36        28
 50000          45      91         79         52         92          77        56
 100000         95      199        164        111        204         175       117
 200000         201     421        352        244        455         396       256
 400000         449     904        764        520        992         873       529
 800000         927     1910       1629       1104       2106        1871      1119
 Key:

 Q Quicksort, standard (Program 7.1)

 T Top-down mergesort, standard (Program 8.1)

 T* Top-down mergesort with cutoff for small files

 O Top-down mergesort with cutoff and no array copy

 B Bottom-up mergesort, standard (Program 8.5)

 B* Bottom-up mergesort with cutoff for small files

 S java.util.Arrays.sort

Weitere ähnliche Inhalte

Was ist angesagt?

Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sortingryokollll
 
signal and system Dirac delta functions (1)
signal and system Dirac delta functions (1)signal and system Dirac delta functions (1)
signal and system Dirac delta functions (1)iqbal ahmad
 
Advance control theory
Advance control theoryAdvance control theory
Advance control theorySHIMI S L
 
Lti and z transform
Lti and z transformLti and z transform
Lti and z transformpranvendra29
 
DSP, Differences between Fourier series ,Fourier Transform and Z transform
DSP, Differences between  Fourier series ,Fourier Transform and Z transform DSP, Differences between  Fourier series ,Fourier Transform and Z transform
DSP, Differences between Fourier series ,Fourier Transform and Z transform Naresh Biloniya
 
Applications of Z transform
Applications of Z transformApplications of Z transform
Applications of Z transformAakankshaR
 
Application of Convolution Theorem
Application of Convolution TheoremApplication of Convolution Theorem
Application of Convolution Theoremijtsrd
 
DSP_FOEHU - MATLAB 03 - The z-Transform
DSP_FOEHU - MATLAB 03 - The z-TransformDSP_FOEHU - MATLAB 03 - The z-Transform
DSP_FOEHU - MATLAB 03 - The z-TransformAmr E. Mohamed
 
The Laplace Transform of Modeling of a Spring-Mass-Damper System
The Laplace Transform of Modeling of a Spring-Mass-Damper System The Laplace Transform of Modeling of a Spring-Mass-Damper System
The Laplace Transform of Modeling of a Spring-Mass-Damper System Mahmoud Farg
 
Fourier transform
Fourier transformFourier transform
Fourier transformShahed Babu
 
Digital control systems (dcs) lecture 18-19-20
Digital control systems (dcs) lecture 18-19-20Digital control systems (dcs) lecture 18-19-20
Digital control systems (dcs) lecture 18-19-20Ali Rind
 

Was ist angesagt? (20)

Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
signal and system Dirac delta functions (1)
signal and system Dirac delta functions (1)signal and system Dirac delta functions (1)
signal and system Dirac delta functions (1)
 
Z transform
Z transformZ transform
Z transform
 
Advance control theory
Advance control theoryAdvance control theory
Advance control theory
 
Lti and z transform
Lti and z transformLti and z transform
Lti and z transform
 
Chapter 2 fourier transform
Chapter 2 fourier transformChapter 2 fourier transform
Chapter 2 fourier transform
 
DSP, Differences between Fourier series ,Fourier Transform and Z transform
DSP, Differences between  Fourier series ,Fourier Transform and Z transform DSP, Differences between  Fourier series ,Fourier Transform and Z transform
DSP, Differences between Fourier series ,Fourier Transform and Z transform
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Applications of Z transform
Applications of Z transformApplications of Z transform
Applications of Z transform
 
Laplace transformations
Laplace transformationsLaplace transformations
Laplace transformations
 
Application of Convolution Theorem
Application of Convolution TheoremApplication of Convolution Theorem
Application of Convolution Theorem
 
Loops_in_Rv1.2b
Loops_in_Rv1.2bLoops_in_Rv1.2b
Loops_in_Rv1.2b
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
 
DSP_FOEHU - MATLAB 03 - The z-Transform
DSP_FOEHU - MATLAB 03 - The z-TransformDSP_FOEHU - MATLAB 03 - The z-Transform
DSP_FOEHU - MATLAB 03 - The z-Transform
 
Laplace transform
Laplace transformLaplace transform
Laplace transform
 
The Laplace Transform of Modeling of a Spring-Mass-Damper System
The Laplace Transform of Modeling of a Spring-Mass-Damper System The Laplace Transform of Modeling of a Spring-Mass-Damper System
The Laplace Transform of Modeling of a Spring-Mass-Damper System
 
Z Transform
Z TransformZ Transform
Z Transform
 
Analog properties and Z-transform
Analog properties and Z-transformAnalog properties and Z-transform
Analog properties and Z-transform
 
Fourier transform
Fourier transformFourier transform
Fourier transform
 
Digital control systems (dcs) lecture 18-19-20
Digital control systems (dcs) lecture 18-19-20Digital control systems (dcs) lecture 18-19-20
Digital control systems (dcs) lecture 18-19-20
 

Andere mochten auch

Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?
Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?
Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa? Kelan tutkimus / Research at Kela
 
Assembly powerpoint
Assembly powerpointAssembly powerpoint
Assembly powerpointemmahehir
 
Kelly digital story
Kelly digital storyKelly digital story
Kelly digital storykkelly2
 
All about me
All about meAll about me
All about megreen13
 
Ami kiat menulis artikel step by step
Ami kiat menulis artikel step by stepAmi kiat menulis artikel step by step
Ami kiat menulis artikel step by stepSyifa Herbalisto
 
8710 Adoption - CA
8710 Adoption - CA8710 Adoption - CA
8710 Adoption - CAATD13
 
Erityiskorvausoikeuksien vaikutus lääkekorvauksiin ja hallinnolliseen työhön
Erityiskorvausoikeuksien vaikutus lääkekorvauksiin ja hallinnolliseen työhönErityiskorvausoikeuksien vaikutus lääkekorvauksiin ja hallinnolliseen työhön
Erityiskorvausoikeuksien vaikutus lääkekorvauksiin ja hallinnolliseen työhönKelan tutkimus / Research at Kela
 
Integroitumista vai eriytymistä? Maahanmuuttajaperheiden nuorten kotoutuminen
Integroitumista vai eriytymistä? Maahanmuuttajaperheiden nuorten kotoutuminenIntegroitumista vai eriytymistä? Maahanmuuttajaperheiden nuorten kotoutuminen
Integroitumista vai eriytymistä? Maahanmuuttajaperheiden nuorten kotoutuminenKelan tutkimus / Research at Kela
 
Subjektiivisen hyvinvoinnin mittaaminen ja kohdennetut kyselyt
Subjektiivisen hyvinvoinnin mittaaminen ja kohdennetut kyselytSubjektiivisen hyvinvoinnin mittaaminen ja kohdennetut kyselyt
Subjektiivisen hyvinvoinnin mittaaminen ja kohdennetut kyselytKelan tutkimus / Research at Kela
 
Rozzi distance
Rozzi distanceRozzi distance
Rozzi distancejrozzi
 
PR 2.0 (Optimizing for People)
PR 2.0 (Optimizing for People)PR 2.0 (Optimizing for People)
PR 2.0 (Optimizing for People)JonnyWegs
 

Andere mochten auch (20)

Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?
Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?
Suvi Rasimus: Tuottaako EU-tuomioistuin oikeusturvaa sosiaalivakuutuksessa?
 
Assembly powerpoint
Assembly powerpointAssembly powerpoint
Assembly powerpoint
 
Hyvinvointi, kestävä talous ja degrowth
Hyvinvointi, kestävä talous ja degrowthHyvinvointi, kestävä talous ja degrowth
Hyvinvointi, kestävä talous ja degrowth
 
Kelly digital story
Kelly digital storyKelly digital story
Kelly digital story
 
Investeringar i barns humankapital
Investeringar i barns humankapitalInvesteringar i barns humankapital
Investeringar i barns humankapital
 
Hiekkala, Sinikka: Neuropsykologinen etäkuntoutus
Hiekkala, Sinikka: Neuropsykologinen etäkuntoutusHiekkala, Sinikka: Neuropsykologinen etäkuntoutus
Hiekkala, Sinikka: Neuropsykologinen etäkuntoutus
 
Toimeentulotukea Kelasta
Toimeentulotukea KelastaToimeentulotukea Kelasta
Toimeentulotukea Kelasta
 
All about me
All about meAll about me
All about me
 
Ami kiat menulis artikel step by step
Ami kiat menulis artikel step by stepAmi kiat menulis artikel step by step
Ami kiat menulis artikel step by step
 
8710 Adoption - CA
8710 Adoption - CA8710 Adoption - CA
8710 Adoption - CA
 
Yrjö Mattila: Oikeusturva sosiaalivakuutusasioissa
Yrjö Mattila: Oikeusturva sosiaalivakuutusasioissaYrjö Mattila: Oikeusturva sosiaalivakuutusasioissa
Yrjö Mattila: Oikeusturva sosiaalivakuutusasioissa
 
Erityiskorvausoikeuksien vaikutus lääkekorvauksiin ja hallinnolliseen työhön
Erityiskorvausoikeuksien vaikutus lääkekorvauksiin ja hallinnolliseen työhönErityiskorvausoikeuksien vaikutus lääkekorvauksiin ja hallinnolliseen työhön
Erityiskorvausoikeuksien vaikutus lääkekorvauksiin ja hallinnolliseen työhön
 
Integroitumista vai eriytymistä? Maahanmuuttajaperheiden nuorten kotoutuminen
Integroitumista vai eriytymistä? Maahanmuuttajaperheiden nuorten kotoutuminenIntegroitumista vai eriytymistä? Maahanmuuttajaperheiden nuorten kotoutuminen
Integroitumista vai eriytymistä? Maahanmuuttajaperheiden nuorten kotoutuminen
 
Tutorial dasar vegas
Tutorial dasar vegasTutorial dasar vegas
Tutorial dasar vegas
 
Subjektiivisen hyvinvoinnin mittaaminen ja kohdennetut kyselyt
Subjektiivisen hyvinvoinnin mittaaminen ja kohdennetut kyselytSubjektiivisen hyvinvoinnin mittaaminen ja kohdennetut kyselyt
Subjektiivisen hyvinvoinnin mittaaminen ja kohdennetut kyselyt
 
Henkisen hyvinvoinnin haasteet
Henkisen hyvinvoinnin haasteetHenkisen hyvinvoinnin haasteet
Henkisen hyvinvoinnin haasteet
 
Rozzi distance
Rozzi distanceRozzi distance
Rozzi distance
 
Elina Ahola: Rekisteritutkimus omaishoitajien tulotasosta
Elina Ahola: Rekisteritutkimusomaishoitajien tulotasostaElina Ahola: Rekisteritutkimusomaishoitajien tulotasosta
Elina Ahola: Rekisteritutkimus omaishoitajien tulotasosta
 
PR 2.0 (Optimizing for People)
PR 2.0 (Optimizing for People)PR 2.0 (Optimizing for People)
PR 2.0 (Optimizing for People)
 
Nuoret eläkkeensaajat
Nuoret eläkkeensaajatNuoret eläkkeensaajat
Nuoret eläkkeensaajat
 

Ähnlich wie 10 algos de tri

CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Mahesh Tibrewal
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufWrushabhShirsat3
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.pptNielmagahod
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 

Ähnlich wie 10 algos de tri (20)

CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!
 
Sorting2
Sorting2Sorting2
Sorting2
 
quicksort (1).ppt
quicksort (1).pptquicksort (1).ppt
quicksort (1).ppt
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Quick sort.pptx
Quick sort.pptxQuick sort.pptx
Quick sort.pptx
 
Selection sort
Selection sortSelection sort
Selection sort
 
s4_quick_sort.ppt
s4_quick_sort.ppts4_quick_sort.ppt
s4_quick_sort.ppt
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
chapter7.ppt
chapter7.pptchapter7.ppt
chapter7.ppt
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 

Kürzlich hochgeladen

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Kürzlich hochgeladen (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

10 algos de tri

  • 2. Tri : complexité minimale • n! résultats possibles • si tests binaires, on obtient un arbre de hauteur minimale log2 n! ~ log2 (n^n / e^n) ~ O(n log n) • La complexité d’un tri sera donc au mieux en O(n log n)
  • 3. The first pass has no effect in this example, because there is no element in the array smaller than the A at the left. On the second pass, the other A is the Tri par sélection smallest remaining element, so it is exchanged with the S in the second position. Then, the E near the middle is exchanged with the O in the third position on the third pass; then, the other E is exchanged with the R in the fourth position on the (selection sort) fourth pass; and so forth. Récurrence sur le résultat (tableau de 1 à i-1 trié) Program 6.12 is an implementation of selection sort that adheres to our conventions. The inner loop is just a comparison to test a current element against the smallest element found so far (plus the code necessary to increment the index of the current element and to check that it does not exceed the array bounds); it could hardly be simpler. The work of moving the items around falls outside the inner loop: each exchange puts an element
  • 4. Tri par sélection (selection sort) Pour chaque i de l à r-1, échanger a[i] avec l’élément minimum de a[i], ..., a[r]. Quand l’index i va de gauche à droite, les éléments à sa gauche sont à leur place finale dans le tableau (et ne seront plus modifiés), donc le tableau est complètement trié lorsque i arrive à l’extrémité droite. static void selection(ITEM[] a, int l, int r) { for (int i = l; i < r; i++) { int min = i; for (int j = i+1; j <= r; j++) if (less(a[j], a[min])) min = j; exch(a, i, min); } }
  • 5. During the first pass of insertion sort, the S in the second position is larger than the A, so it does not have to be moved. On the second pass, when the O in the Tri par insertion third position is encountered, it is exchanged with the S to put A O S in sorted order, and so forth. Un-shaded elements that are not circled are those that were (insertion sort) moved one position to the right. he implementation of insertion sort données (on insère l’élément i) but inefficien Récurrence sur les in Program 6.1 is straightforward, e shall now consider three ways to improve it in order to illustrate a recurrent them roughout many of our implementations: We want code to be succinct, clear, an ficient, but these goals sometimes conflict, so we must often strike a balance. We do s
  • 6. Tri par insertion (insertion sort) (i) met le plus petit élément du tableau à la première place pour que cet élément serve de “sentinelle” ; (ii) une seule assignation, plutôt qu’un échange, dans la boucle principale ; et (iii) la boucle interne se termine quand l’élément inséré est à la bonne position. Pour chaque i, trie les éléments a[l], ..., a[i] en déplaçant d’une position vers la droite les éléments de la liste triée a[l], ..., a[i-1] qui sont plus grands que a[i], place ensuite a[i] à la bonne position. static void insertion(ITEM[] a, int l, int r) { int i; for (i = r; i > l; i--) if (less(a[i], a[i-1])) exch (a, i-1, i); for (i = l+2; i <= r; i++) { int j = i; ITEM v = a[i]; while (less(v, a[j-1])) { a[j] = a[j-1]; j--; } a[j] = v; } }
  • 7. ge A and S to get A S; then, we merge O and R to get O R; then, we merg with A S to get A O R S. Later, we merge I T with G N to get G I N T, then erge this result with A O R S to get A G I N O R S T, and so on. The metho Tri fusion (Merge sort) recursively builds up small sorted files into larger ones. wn mergesort isRécurrence dichotomique sur les donnéesstyle, where a m analogous to a top-down management n organization to take on a big task by dividing it into pieces to be ndently by underlings. If each manager operates by simply dividing the give then putting together the solutions that the subordinates develop and pass
  • 8. Tri fusion (Merge sort) static void mergesort(ITEM[] a, int l, int r) { if (r <= l) return; int m = (r+l)/2; mergesort(a, l, m); mergesort(a, m+1, r); merge(a, l, m, r); }
  • 9. Tri par segmentation (Quicksort, Hoare 1960) use the following general strategy to implement partitioning. First, we arbitra ose a[r] to be the partitioning element—the one that will go into its final positi t, we scan from the left end of the array until we find an element greater than titioning element, and we scan from the right end of the array until we find
  • 10. Tri par segmentation (Quicksort, Hoare 1960) • Choisir un pivot v • Réarranger le tableau pour que tous les éléments plus petits que v soient à gauche, tous les plus grands à droite • Trier les deux sous-tableaux à droite et à gauche static void quicksort(ITEM[] a, int l, int r) { if (r <= l) return; int i = partition(a, l, r); quicksort(a, l, i-1); quicksort(a, i+1, r); }
  • 11. Tri par segmentation : partition (Quicksort, Hoare 1960) ure 7.3. Dynamic characteristics of quicksort partition
  • 12. Tri par segmentation (Quicksort, Hoare 1960) La variable v contient la valeur du pivot a[r], et i et j sont respectivement les indices de parcours gauche et droit. La boucle de partition incrémente i et décrémente j, en maintenant la propriété invariante : aucun élément à gauche de i n’est plus grand que v et aucun élément à droite de j n’est plus petit que v. Quand les indices se rejoignent, on termine le partitionnement en échangeant a[i] et a[r], ce qui met v dans a[i], avec aucun élément plus grand que v à droite et aucun élément plus petit à gauche. La boucle de partitionnement est codée comme une boucle infinie, avec un break quand les indices se croisent. Le test j==l protège du cas où l’élément de partitionnement est le plus petit du fichier. static int partition(ITEM a[], int l, int r) { int i = l-1, j = r; ITEM v = a[r]; for (;;) { while (less(a[++i], v)) ; while (less(v, a[--j])) if (j == l) break; if (i >= j) break; exch(a, i, j); } exch(a, i, r); return i; }
  • 13. Tri : complexité • Tri par sélection : au plus n^2/2 comparaisons et n échanges • Tri par insertion : au plus n^2/4 comparaisons et n^2/4 affectations • Tri fusion : O(n log n) (démonstration à suivre) • Quicksort : n^2/2 comparaisons au pire, 2n ln n comparaisons en moyenne, donc O(n log n). Partitionnement en O(n). Situation optimale : pivot au milieu, donc choix du pivot crucial
  • 14. In addition to netting the improvements discussed in Section 8.2, a good Java VM implementation might avoid unnecessary array accesses to reduce the inner loop of mergesort to a comparison (with conditional branch), two index increments ( k and either i or j ), and a test with conditional branch for loop completion. The total number of instructions in such an inner loop is slightly higher than that for quicksort, but the instructions are executed only N lg N times, where quicksort's are executed 39 percent Tri : temps d’exécution more often (or 29 percent with the median-of-three modification). Careful implementation and detailed analysis are required for more precise comparison of the algorithms in particular environments; nonetheless, we do know that mergesort has an inner loop that is slightly longer than that of quicksort. Table 8.1. Empirical study of mergesort algorithms These relative timings for various sorts on random files of integers indicate that standard quicksort is about twice as fast as standard mergesort; that adding a cutoff for small files lowers the running times of both bottom-up and top-down mergesort by about 15 percent; that top-down mergesort is about 10 percent faster than bottom-up mergesort for these file sizes; and that even eliminating the cost of the file copy (the method used in the Java system sort) leaves mergesort substantially slower than quicksort for randomly ordered files. top-down bottom-up N Q T T* O B B* S 12500 23 27 16 19 30 20 19 25000 20 43 34 27 42 36 28 50000 45 91 79 52 92 77 56 100000 95 199 164 111 204 175 117 200000 201 421 352 244 455 396 256 400000 449 904 764 520 992 873 529 800000 927 1910 1629 1104 2106 1871 1119 Key: Q Quicksort, standard (Program 7.1) T Top-down mergesort, standard (Program 8.1) T* Top-down mergesort with cutoff for small files O Top-down mergesort with cutoff and no array copy B Bottom-up mergesort, standard (Program 8.5) B* Bottom-up mergesort with cutoff for small files S java.util.Arrays.sort