Tri rapide en JAVA

Fermé
protanq - 15 déc. 2007 à 18:10
Laritta Messages postés 12 Date d'inscription jeudi 17 janvier 2008 Statut Membre Dernière intervention 31 janvier 2008 - 27 janv. 2008 à 21:59
Bonjour,

Je doir créer un algorithme de tri rapide en java. Pour l'instant, je crois que je touche au but. Cependant il y a quelques erreurs qui font que sa ne marche pas.
Je vous joins mon code:

public class expose{

public static void main TriRapide(int tableau[])
{
int longueur=tableau.length;
triRapide(tableau,0,longueur-1);
}

private static int partition(int tableau[],int deb,int fin)
{
int compt=deb;
int pivot=tableau[deb];

for(int i=deb+1;i<=fin;i++)
{
if (tableau[i]<pivot)
{
compt++;
echanger(tableau,compt,i);
}
}
echanger(tableau,deb,compt);
return(compt);
}

private static void triRapide(int tableau[],int deb,int fin)
{
if(deb<fin)
{
int positionPivot=partition(tableau,deb,fin);
triRapide(tableau,deb,positionPivot-1);
triRapide(tableau,positionPivot+1,fin);
}
}


private static int echanger (int tableau[], int temp, int compt, int deb){
int temp=tableau[deb];
tableau[deb]=tableau[compt];
tableau[compt]=temp;

}
}
}

Je vous remercie d'avance.

cordialement
A voir également:

1 réponse

Laritta Messages postés 12 Date d'inscription jeudi 17 janvier 2008 Statut Membre Dernière intervention 31 janvier 2008 4
27 janv. 2008 à 21:59
Salut voici mon code en java j'espere qu'il taide.........mais j'ai une petite question vous pover m'envoyer un programme en code java concernat l'encryption decryption merci en tout cas



public staic void TriRapide (int [] tab){
int N=tab.length;
Trirapide(tab,0,N-1);}

private staic int partition(int [] tab,int l,int r){
int i=l;//indexe of left to right;
int k=r-1;// indexe of right to left
int temp;
if(r-l>=1)//check there is two eltement to start
{int pivot =tab[r];
while(k>i){// left and right indice not meet
while((tab[i]<pivot)&&(k>i)) i++;
while ((tab[k]>pivot)&&(k>i)) k--;
if(k> i){ //put pivot into its final place
while((tab[i]>pivot)&&(tab[k]<pivot)
{ temp=tab[k];
tab[k]=tab[i];
tab[i]=temp;}//swap(tab,i,k)
else{
if(tab[i]>pivot) { temp=pivot;
pivot=tab[i];
tab[i]=temp;}
}//fin else
else //if there is one element in the partition
{return i;}
}//fin partition

private sativ void Trirapide(int [] tab,int l,int r)
{ if(l<r)
{ int positionpivot=partition(tab,l,r);
TriRapide(tab,l,positionpivot-1);
TriRapide(tab,positionpivot+1,r); }
}

//fonction de creation d'un tableau

public static void afficher(int [] tab){
for(int i=0;i<tab.length;i++)
System.out.println(tab[i]+"");
System.out.println();}

//Fonction principale
public static void main (String [] args) {
int N;
N=Integer.parseInt(JOptionPane.showInputDialog("Donner la taille"));
int [] tab=new int[N];
for(int i=0;i<tab.length;i++){
tab[i]=Integer.parseInt(JOptionPane.showInputDialog("Entrer i SVP"));}
System.out.println("Tableau initiale :");
afficher(tab);
TriRapide(tab);
System.out.println("Tableau trier :");
afficher(tab);
}
}
3