#include <windows.h>
using namespace std;
void selectSort(int A[], int n);
void bubbleSort(int A[], int n);
int binarySearch(int A[], int n, int val);
int main()
{
int A[4]={3, 1, 0, 5};
int B[4]={3, 1, 0, 5};
selectSort(A , 4);
cout<<"\n Selection Sort:"<<endl;
for(int i=0 ; i<4 ; i++)
{
cout<<"\n";
cout<<A[i];
}
cout<<"\n"<<endl;
cout<<" ";system("PAUSE");
bubbleSort(B , 4);
cout<<"\n Bubble Sort:"<<endl;
for(int j=0 ; j<4 ; j++)
{
cout<<"\n";
cout<<B[j];
}
cout<<"\n"<<endl;
cout<<" ";system("PAUSE");
binarySearch(A, 4, 3);
cout<<"\n Binary Search:"<<endl;
return 0;
}
//------------------------------------------------------------------------------------------------------------------------
void selectSort (int A[], int n)
{ int minimo, indice;
for(int i=0 ; i<n ; i++)
{ indice=i;
for(int j=i+1 ; j<n ; j++)
{
if(A[j]<A[indice])
{
indice=j;
if(indice != i)
{
minimo=A[i];
A[i]=A[indice];
A[indice]=minimo;
}}}}}
//------------------------------------------------------------------------------------------------------------------------
void bubbleSort(int A[], int n)
{ int val;
bool mov=true;
int j=0;
while(mov)
{ mov=false;
j++;
for(int i=0 ; i<(n-j) ; i++)
{
if(A[i]>A[i+1])
{
val=A[i];
A[i]=A[i+1];
A[i+1]=val;
mov=true;
}}}}
//------------------------------------------------------------------------------------------------------------------------
int binarySearch(int A[], int in, int fin, int val)
{ int i=(in+fin)/2;
if(in>fin)
{
return -1;
}
if(val==A[i])
{
return i;
}
else if(val<A[i])
{
return binarySearch(A, in, i-1, val);
}
else if(val>A[i])
{
return binarySearch(A, i+1, fin, val);
}
}
No hay comentarios:
Publicar un comentario