#include<iostream>
#include<iomanip>
using namespace std;
void ord_rapido(int *v, int ind_inicial, int ind_final, int n);
int separar(int *v, int ind_inicial, int ind_final, int n);
bool busqueda_binaria(int *v, int elemento_a_buscar, int n);
void mostrar(int *v, int n);
int main(){
int *notas=nullptr,n=0,target=0;
srand(time(nullptr));
cout<<"Ingrese las n notas a generar: \n"; cin>>n;
notas=new int [n];
for(int i=0; i<n; i++){
notas[i]=rand()%21;
}
target=rand()%21;
ord_rapido(notas,0,n-1,n);
cout<<" -> Arreglo dinamico: \n";
mostrar(notas,n);
if (busqueda_binaria(notas,target,n))
cout<<" El elemento "<<target<<" SI se encuentra en el arreglo";
else
cout<<" El elemento "<<target<<" NO se encuentra en el arreglo";
return 0;
}
void ord_rapido(int *v, int ind_inicial, int ind_final, int n){
if (ind_inicial < ind_final) {
int i_pivote = separar(v, ind_inicial, ind_final,n);
ord_rapido(v, ind_inicial, i_pivote - 1,n);
ord_rapido(v, i_pivote + 1, ind_final,n);
}
}
int separar(int *v, int ind_inicial, int ind_final, int n) {
int pivote=v[ind_final];
int i_aux=ind_inicial;
int aux=0;
for (int j=ind_inicial; j<ind_final; j++) {
if (v[j] < pivote) {
aux=v[i_aux];
v[i_aux]=v[j];
v[j]=aux;
i_aux++;
}
}
aux = v[i_aux];
v[i_aux] = v[ind_final];
v[ind_final] = aux;
return i_aux;
}
bool busqueda_binaria(int *v, int elemento_a_buscar, int n) {
int indice_inicial = 0, ind_f = n - 1;
int indice_intermedio;
while (indice_inicial <= ind_f) {
indice_intermedio = (indice_inicial + ind_f)/2;
if (elemento_a_buscar == v[indice_intermedio])
return true;
else if (elemento_a_buscar < v[indice_intermedio])
ind_f = indice_intermedio - 1;
else
indice_inicial = indice_intermedio + 1;
}
return false;
}
void mostrar(int *v, int n){
for (int j=0; j<n; j++){
cout<<setw(4)<<v[j];
} cout<<endl;
}