Advertisement
AudibertHerve

Chercher

Jan 25th, 2020
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. package chercher;
  2.  
  3. public class Chercher {
  4.    
  5.    
  6.  public Chercher(int[] tab) {
  7.         // TODO Auto-generated constructor stub
  8.         this.tab = tab;
  9.     }
  10.  
  11.     int tab [] ;
  12.  
  13.     public int chercherElt ( int element, int[] tab){
  14.         return checkSiPremiereOccurence(chercherEltRecursif(element, tab));
  15.     }
  16.  
  17.     public int checkSiPremiereOccurence (int positionTrouvée){
  18.         for (int i = positionTrouvée; i >= 0; i--)
  19.             if (this.tab[i] != tab[positionTrouvée])
  20.                 return i + 1;
  21.         return positionTrouvée;
  22.     }
  23.     public int chercherEltRecursif ( int element, int[] tab){
  24.         int dichotomie = tab.length / 2;
  25.         if (tab.length == 0) {
  26.             return -1;
  27.         }
  28.         if (tab[dichotomie] == element) {
  29.             return dichotomie;
  30.         }
  31.         if (tab[dichotomie] > element) {
  32.             int[] tabTmp = new int[dichotomie];
  33.             System.arraycopy(tab, 0, tabTmp, 0, dichotomie);
  34.             return chercherEltRecursif(element, tabTmp);
  35.         } else {
  36.             int[] tabTmp = new int[dichotomie];
  37.             if (tab.length % 2 != 0) {
  38.                 System.arraycopy(tab, dichotomie + 1, tabTmp, 0, tab.length - (dichotomie + 1));
  39.                 return dichotomie + 1 + chercherEltRecursif(element, tabTmp);
  40.             }
  41.             System.arraycopy(tab, dichotomie, tabTmp, 0, tab.length - (dichotomie));
  42.             return dichotomie + chercherEltRecursif(element, tabTmp);
  43.         }
  44.     }
  45.  
  46.  
  47.        
  48.        
  49.        
  50.         /* classe donnant 5 méthodes de recherche dichotomique à tester
  51.          *  Le style de redaction de cette classe n'est pas à prendre
  52.          * comme exemple!*/
  53.            
  54.             public boolean chercher1(int x, int []  tab){        
  55.                 /*recherche dichotomique 1*/
  56.                 int i,j,m;
  57.                 int n = tab.length;
  58.                 i = 0; j= n - 1;
  59.                 while (i <= j) {
  60.                     m = (i + j) / 2;
  61.                     if (tab[m] < x){
  62.                         i = m + 1;
  63.                     }
  64.                     if (tab[m] > x) {
  65.                         j = m - 1;
  66.                     }
  67.                     if (tab[m] == x){
  68.                         return true;
  69.                     }
  70.                     if (i > n-1 || j < 0) {
  71.                         return false;
  72.                     }
  73.                 }
  74.                 return false;
  75.             }
  76.  
  77.             public boolean chercher2(int x, int [] tab){
  78.                 /*recherche dichotomique 2*/
  79.                 int i,j,m;
  80.                 boolean found;
  81.                 i=1;
  82.                 j= tab.length;
  83.                 m=0;
  84.                 found=false;
  85.                 while (!(i==j && !found)) {
  86.                     m=(i+j)/2;
  87.                     if (tab[m]<x){
  88.                         i=m+1;
  89.                     }    else {
  90.                         if(tab[x]==m) {
  91.                             found=true;
  92.                         }    else {
  93.                             j=m-1;
  94.                         }
  95.                     }
  96.                 }
  97.                 return found;
  98.             }
  99.  
  100.             public boolean chercher3(int x, int [] tab){        
  101.                 /*recherche dichotomique 3*/
  102.                 int i,j,m;
  103.                 i = 1;
  104.                 j = tab.length;
  105.                 m=0;
  106.                 while (i!=j) {
  107.                     m=(i+j)/2;
  108.                     if (tab[m]<=x) i=m;
  109.                     else j=m;
  110.                 }
  111.                 return (x==tab[m]);
  112.             }
  113.  
  114.             public boolean chercher4(int x, int [] tab){  
  115.                 /*recherche dichotomique 4*/
  116.                 int i,j,m;
  117.                 i=1;
  118.                 j = tab.length;
  119.                 m=0;
  120.                 boolean trouve = false;
  121.                 while (i!=j) {
  122.                     m=(i+j)/2;
  123.                     if (tab[m]<=x) i=m;
  124.                     if (tab[m] == x) trouve = true;
  125.                     else j=m;
  126.                 }
  127.                 return trouve;
  128.             }
  129.  
  130.             public boolean chercher5(int x, int [] tab){  
  131.                 /*recherche dichotomique 5*/
  132.                 int i,j,m;
  133.                 boolean trouve;
  134.                 i=0;
  135.                 j=tab.length - 1;
  136.                 m=0;
  137.                 trouve=false;
  138.                 while ((i<=j)&&(!trouve) ){
  139.                     m=(i+j)/2;
  140.                     if (x==tab[m]){
  141.                         trouve=true;
  142.                     }
  143.                     else { if (x < tab[m]){
  144.                         j=m-1;     
  145.                     } else {
  146.                         i=m+1;
  147.                     }
  148.                     }
  149.                 }
  150.                 return trouve;
  151.             }      
  152.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement