Busqueda Binaria recursiva y ordenación shell recursiva

xino
29 de Marzo del 2004
A ver si alguen me puede realizar este método en c++ por favor gracias por todo

noel solw
29 de Marzo del 2004
A continuacion el programa de busqueda binaria
recursiva :


// program k4_3.CPP - page 69
// binary search.
// c++ exercices book - dr. gershon kagan (first edition : 2001)
// written in Borland CPP ver 3.1

#include <conio.h>
#include <iostream.h>
#include <iomanip.h>

#define MAX 18

void Show(int *a)
{
for(int i = 0; i < MAX; i++)
cout << setw(4) << a[i];
cout << endl << endl;
} // SHOW

int GetNum()
{
int x = -1;
while(x < 0 || x > 99)
{
cout << "search number = ";
cin >> x;
}
return x;
} // GET NUM

int BiSearch(int x,int *a,int left,int right)
{
if(left > right)
return MAX;
int middle = (left+right)/2;
if(a[middle] == x)
return middle;
else if(a[middle] > x)
return BiSearch(x,a,left,middle-1);
else
return BiSearch(x,a,middle+1,right);
} // BISEARCH

void main()
{
clrscr();
int a[MAX] = {1,5,7,9,11,13,24,25,33,44,47,50,51,54,61,81,85,99};
cout << "modified linear search : " << endl << endl;
Show(a);
int search_number = GetNum();
while(search_number)
{
int found = BiSearch(search_number,a,0,MAX-1);
cout << search_number;
if(found == MAX)
cout << " not found";
else
cout << " found in place " << found;
cout << endl << endl;
search_number = GetNum();
}
cout << endl << "end of program - good bye ! ! !n";
getch();
} // MAIN

/*
modified linear search :

1 5 7 9 11 13 24 25 33 44 47 50 51 54 61 81 85 99

search number = 99 99 found in place 17

search number = 1 1 found in place 0

search number = 32 32 not found

search number = 33 33 found in place 8

search number = 48 48 not found

search number = 51 51 found in place 12

search number = 0

end of program - good bye ! ! !
*/

noel solw
29 de Marzo del 2004
Este es el programa de ordenacion por el metodo de Shell, recursivo :

// program k4_11.CPP - page 73
// shell sort.
// c++ exercices book - dr. gershon kagan (first edition : 2001)
// written in Borland CPP ver 3.1

#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>

#define MAX 18

void Show(int *a)
{
for(int i = 0; i < MAX; i++)
cout << setw(4) << a[i];
cout << endl;
} // SHOW

void Init(int *a)
{
for(int i = 0; i < MAX; i++)
a[i] = random(100);
} // INIT

void Swap(int &a,int &b)
{
int c = a;
a = b;
b = c;
} // SWAPS INTEGER

void ShellSort(int *a)
{
for(int step = MAX/2; step; step /= 2)
{
for(int i = step; i < MAX; i++)
for(int j = i-step; j >= 0 ; j -= step)
{
if (a[j] > a[j+step])
Swap(a[j],a[j+step]);
else
break;
}
Show(a);
}
} // SHELL SORT

void main()
{
clrscr();
randomize();
int a[MAX];
cout << "shell sort : " << endl << endl;
Init(a);
Show(a);
cout << endl;
ShellSort(a);
cout << endl << "end of program - good bye ! ! !n";
getch();
} // MAIN

/*
shell sort :

69 29 8 31 36 16 10 47 37 58 27 41 46 55 3 36 37 45

58 27 8 31 36 3 10 37 37 69 29 41 46 55 16 36 47 45
36 3 8 31 37 27 10 36 46 45 16 37 47 55 29 41 58 69
8 3 10 27 16 31 29 36 36 37 37 41 46 45 47 55 58 69
3 8 10 16 27 29 31 36 36 37 37 41 45 46 47 55 58 69

end of program - good bye ! ! !
*/