hercioneto

pesquisa binaria

Oct 11th, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. //Rextester.Program.Main is the entry point for your code. Don't change it.
  2. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace Rextester
  10. {
  11.     public class Program
  12.     {
  13.        
  14.        
  15.        
  16.         public static void imprimirArray(int[] vetor) {
  17.             for (int r = 0; r < vetor.Length; r++)
  18.             {
  19.                 Console.Write("{0}  ", vetor[r]);
  20.             }
  21.        
  22.         }
  23.        
  24.         public static void pb(int[] vetor, int p , int u, int valor) {
  25.             int l = 0;
  26.            
  27.             l = Convert.ToInt32((p+u)/2);
  28.            
  29.             if (valor == vetor[l]) {
  30.                 Console.WriteLine("{0}  {1}", l, vetor[l]);
  31.             } else {
  32.             if (valor > vetor[l]) {
  33.                 p = l+1;
  34.                 pb(vetor, p, vetor.Length, valor);
  35.             }            
  36.             else {
  37.                 u = l-1;
  38.                 pb(vetor, p, u, valor);
  39.             }
  40.             }
  41.         }
  42.        
  43.        
  44.        
  45.         public static void Main(string[] args)
  46.         {
  47.             int[] vetor = new int[]{2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,48,50,52};
  48.             //imprimirArray(vetor);
  49.             int valor = 4;
  50.             int posi;
  51.             pb(vetor, 0, vetor.Length, valor);
  52.            
  53.  
  54.            
  55.            
  56.            
  57.            
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment