1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
import java.util.ArrayList;
/**
*
*/
/**
* @author
*
*/
public class TriRapidCroissant {
/**
*
*/
public TriRapidCroissant() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
private void permuter(couple couple1, couple couple2) {
// TODO Auto-generated method stub
couple cp;
cp=couple1;
couple1=couple2;
couple2=cp;
}
public void TrierRapidCroissant(ArrayList <couple> List, int deb, int fin){
// trier dans un ordre croissant de distance: les plus bons sont ceux qui ont la moindre distance
int i=deb; int j=fin; float pivot=List.get((i+j)/2).getscnd();
do{
while( List.get(i).getscnd()<pivot)
{i++;
}
while( List.get(j).getscnd()>pivot)
{j--;
}
if(i<j)
{
permuter(List.get(i),List.get(j));
i++;
j--;
}
} while (i>j);
if (deb<j)
{
//System.out.println("bbbbbbb");
TrierRapidCroissant(List,deb,j);
}
if (i<fin){
TrierRapidCroissant(List,i,fin);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<couple> Liste =new ArrayList<couple>();
TriRapidCroissant TR= new TriRapidCroissant();
Liste.add(new couple("d11",(float) 0.8));
Liste.add(new couple("d12",(float) 0.3));
Liste.add(new couple("d13",(float) 0.2));
Liste.add(new couple("d14",(float) 0.5));
Liste.add(new couple("d21",(float) 0.6));
Liste.add(new couple("d22",(float) 0.37));
Liste.add(new couple("d23",(float) 0.8));
int deb=0; int fin= Liste.size()-1;
TR.TrierRapidCroissant(Liste, deb, fin);
for(int i=0; i< Liste.size();i++){
System.out.println(Liste.get(i).getfst());
System.out.println(Liste.get(i).getscnd());
}
}
} |
Partager