martes, 13 de agosto de 2013

BUSQUEDA RECURSIVA EN UNA MATRIZ




public void Buscar(int[][] matriz,int dato,int x, int y)
{
     if (matriz[x][y]==dato)
    {
        JOptionPane.showMessageDialog(null,"se encuentra en la posicion"+x+","+y);
    }
    else{
        if (y < matriz[y].length-1&&x<matriz[x].length)
        {
             Buscar(int[][] matriz,dato,x, y+1);
        }
        if (y==matriz[y].length-1 && x< matriz[x].length-1)
        {
            y=0;
            x=x+1;
            Buscar(int[][] matriz,dato,x, y);


        }
     }

un metodo recursivo es un metodo que se invoca a si mismo generando un ciclo, el uso de los if es para controlarlo y dependiendo de la condicion se invoca o se sale.

METODO DE ORDENACION POR SELECCION


int  posMayor;
                for (int cont1 = 0; cont1 < tama - 1; cont1++)
                {
                    posMayor = cont1;
                    for (cont2 = cont1 + 1; cont2 < tama; cont2++)// tama es el tamaño del vector
                    {
                        if (words[posMayor].CompareTo(words[cont2]) < 0)
                        {
                            posMayor = cont2;
                        }

                    }
                    aux = words[posMayor];
                    words[posMayor] = words[cont1];
                    words[cont1] = aux;
                }

METODO DE ORDENACION SHELL


       //Metodo de Ordenacion SHELL
        private void button5_Click(object sender, EventArgs e)
        {
            int i, j, inc, temp;
            int n = 10;

            for (inc = 1; inc < n; inc = (inc * 3) + 1) ;

            while (inc > 0)
            {
                for (i = inc; i < n; i++)
                {
                    j = i;
                    temp = A[i];
                    while ((j >= inc) && (A[j - inc] > temp))
                    {
                        A[j] = A[j - inc];
                        j = j - inc;
                    }
                    A[j] = temp;
                }
                inc /= 2;
            }

        }

FIBONACCI RECURSIVO



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            Console.WriteLine("digite la cantidad de terminos");
            n = int.Parse(Console.ReadLine());
            int y = fibo(n-1);
            Console.WriteLine(y);
            Console.Read();

        }
        static int fibo(int n)
        {
            if (n < 2)
            {
                return (n);
            }
            else
                return (fibo(n - 1) + fibo(n - 2));
        }
   
    }
}

BUSQUEDA BINARIA RECURSIVA


 private int busquedaRecursiva(string[] Vec,int limisup,int liminf)
        {

          
            int limiteInferior = liminf;
            int limiteSuperior = limisup;
            int Medio = (limiteSuperior - limiteInferior) / 2;
            if (Vec[Medio].CompareTo(datoparabuscar.Text) < 0)
                  limiteSuperior = (busquedaRecursiva(Vec, Medio-1,limiteInferior));
            if (Vec[Medio].CompareTo(datoparabuscar.Text) > 0)
                limiteInferior = (busquedaRecursiva(Vec, limisup+1,Medio));
            if (Vec[Medio].CompareTo(datoparabuscar.Text) == 0)
            {
                MessageBox.Show("su dato fue encontrado en la posicion" + Medio);
                swiche = true;
            }
      
          

            return(Medio);
              
        }
        }