begin process at 2012 05 27 21:06:19
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths & Algorithmes

 > TRI QUICKSORT COMPILÉ AVEC C++BUILDER5

TRI QUICKSORT COMPILÉ AVEC C++BUILDER5


 Description

Petit code montrant le principe de récursivité à l'aide de l'algorithm du QuickSort.  Conçu avec des variables dimensionnées (fonctionne aussi très bien avec des pointeurs)

Source

  • #include <iostream.h>
  • #include <conio.h>
  • void Echange(int tab[], int i, int j);
  • int Partition(int tab[], int gauche, int droite);
  • void Tri_Rapide(int tab[], int gauche, int droite);
  • void Afficher(int tab[], int N);
  • //Fonction principal -->
  • void main(void)
  • {
  • int tab[45];
  • int i, Num = 1, ligne = 4;
  • gotoxy(20, 1);
  • cout << "Entrez 45 Nombres différents:";
  • for (i=0; i<45; i++, Num++)
  • {
  • if (Num > 30)
  • {
  • if (ligne >= 19)
  • ligne = 4;
  • gotoxy( 51, ligne++ );
  • cout << "Nombre " << Num << ": ";
  • }
  • else if (Num > 15)
  • {
  • if (ligne >= 19)
  • ligne = 4;
  • gotoxy( 26, ligne++ );
  • cout << "Nombre " << Num << ": ";
  • }
  • else
  • {
  • gotoxy( 1, ligne++ );
  • cout << "Nombre " << Num << ": ";
  • }
  • cin >> tab[i];
  • }
  • gotoxy(20, 20);
  • cout << "\nLes nombres en ordre d'entré sont:\n\n";
  • Afficher(tab, 45);
  • gotoxy(20, 25);
  • cout << "\nLes nombres une fois le tri effectué sont:\n\n";
  • Tri_Rapide(tab, 0, i-1);
  • Afficher(tab, 45);
  • getch(); // Pause
  • }
  • //Fonction récursive du QuickSort
  • void Tri_Rapide(int tab[], int gauche, int droite)
  • {
  • int pivot;
  • if ( droite > gauche )
  • {
  • pivot = Partition( tab, gauche, droite);
  • Tri_Rapide( tab, gauche, pivot);
  • Tri_Rapide( tab, pivot+1, droite);
  • }
  • }
  • //Fonction répartissant les données à l'aide d'un pivot (le premier élément) -->
  • int Partition( int tab[], int gauche, int droite)
  • {
  • int g, d, p;
  • p = tab[gauche];
  • g = gauche-1;
  • d = droite+1;
  • while(true)
  • {
  • do
  • d--;
  • while ( tab[d] > p );
  • do
  • g++;
  • while ( tab[g] < p );
  • if ( g < d)
  • Echange( tab, g, d );
  • else
  • return d;
  • }
  • }
  • //Fonction Échangeant les éléments de droites et de gauches -->
  • void Echange(int tab[], int i, int j)
  • {
  • int temp;
  • temp = tab[i];
  • tab[i] = tab[j];
  • tab[j] = temp;
  • }
  • //Simple fonction d'affichage -->
  • void Afficher(int tab[], int N)
  • {
  • for(int i=0;i<N;i++)
  • cout << tab[i] << " ";
  • }
#include <iostream.h>
#include <conio.h>

void Echange(int tab[], int i, int j);
int Partition(int tab[], int gauche, int droite);
void Tri_Rapide(int tab[], int gauche, int droite);
void Afficher(int tab[], int N);

//Fonction principal  -->

void main(void)
{
  int tab[45];
  int i, Num = 1, ligne = 4;

  gotoxy(20, 1);
  cout << "Entrez 45 Nombres différents:";

  for (i=0; i<45; i++, Num++)
    {
        if (Num > 30)
        {
          if (ligne >= 19)
            ligne = 4;
          gotoxy( 51, ligne++ );
          cout << "Nombre " << Num << ": ";
        }
        else if (Num > 15)
        {
          if (ligne >= 19)
            ligne = 4;
          gotoxy( 26, ligne++ );
          cout << "Nombre " << Num << ": ";
        }
        else
        {
          gotoxy( 1, ligne++ );
          cout << "Nombre " << Num << ": ";
        }
      cin >> tab[i];
    }

  gotoxy(20, 20);
  cout << "\nLes nombres en ordre d'entré sont:\n\n";
  Afficher(tab, 45);
  gotoxy(20, 25);
  cout << "\nLes nombres une fois le tri effectué sont:\n\n";
  Tri_Rapide(tab, 0, i-1);
  Afficher(tab, 45);

  getch();  // Pause
}

//Fonction récursive du QuickSort

void Tri_Rapide(int tab[], int gauche, int droite)
{
  int pivot;

  if ( droite > gauche )
  {
    pivot = Partition( tab, gauche, droite);
    Tri_Rapide( tab, gauche, pivot);
    Tri_Rapide( tab, pivot+1, droite);
  }
}

//Fonction répartissant les données à l'aide d'un pivot (le premier élément) -->

int Partition( int tab[], int gauche, int droite)
{
  int g, d, p;

  p = tab[gauche];
  g = gauche-1;
  d = droite+1;

  while(true)
  {
    do
       d--;
    while ( tab[d] > p );

    do
       g++;
    while ( tab[g] < p );

    if ( g < d)
       Echange( tab, g, d );
    else
       return d;
  }
}

//Fonction Échangeant les éléments de droites et de gauches -->

void Echange(int tab[], int i, int j)
{
   int temp;

   temp = tab[i];
   tab[i] = tab[j];
   tab[j] = temp;
}

//Simple fonction d'affichage -->

void Afficher(int tab[], int N)
{
  for(int i=0;i<N;i++)
     cout << tab[i] << " ";
}  

 Conclusion

Selon moi c'est l'algorithm du QuickSort le plus simple à comprendre!


 Sources du même auteur

FONCTION PAUSE(INT TEMPS) [C++BUILDER5]
PUISSANCE 4 COMPILÉ AVEC C++BUILDER 5
Source avec Zip LISTE CHAÎNÉE (FIFO) COMPILÉ AVEC C++BUILDER 5

 Sources de la même categorie

Source avec Zip UN EXAMPLE D'APPLICATION EN CUDA DE L'ALGORITHME DE SCAN POU... par oguzaras
Source avec Zip Source avec une capture CHIFFREMENT DE VIGENERE par lajouad
Source avec Zip Source avec une capture ANALYSE SYNTAXIQUE par lajouad
Source avec Zip Source avec une capture STRUCTURE D'UNE MATRICE PAR LES LISTE LINÉAIRE (NON CONTUGUS... par benzarabel
Source avec Zip Source avec une capture DESSINER UNE ARBRE BINAIRE( MODE CONSOLE): par benzarabel

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,328 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales