// Autor: Carlos Castañeda Gallardo --> cronicacomputista.wordpre....
#include <iostream>
#include <windows.h>
#include <conio.h>
HANDLE hCon;
using namespace std;
void SetColor(int i);
void LeerArray(int num,float x[]);
void MostrarArray(int num,float x[]);
void MostrarColor(int num,float x[]);
void MostrarBubleSort(int num,float x[]);
void MostrarSeleccionSort(int num,float x[]);
void MostrarInsercionSort(int num,float x[]);
void MostrarShellSort(int num,float x[]);
void MostrarQuickSort(int ,int,float x[],int);
void MostrarMergeSort(float x[],int,int,int);
void merge(float x[], int ini, int m, int fin,int num);
int menu();
int main()
{
int op,num;
float x[50];
do
{
op=menu();
switch(op)
{
case 1: {
SetColor(10);
cout<<"\n\t\t\t [ INGRESO DE ARREGLO ]"<<endl;
SetColor(15);
cout<<" Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;
LeerArray(num,x);
SetColor(10);
cout<<"\n\t\t\t [ ARREGLO INGRESADO ]"<<endl<<endl;
SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;
SetColor(13);
cout<<"\n\t\t [ ARREGLO ORDENADO POR BUBLE SORT ]"<<endl<<endl;
MostrarBubleSort(num,x);
getch();
break;
}
case 2: {
SetColor(10);
cout<<"\n\t\t\t [ INGRESO DE ARREGLO ]"<<endl;
SetColor(15);
cout<<" Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;
LeerArray(num,x);
SetColor(10);
cout<<"\n\t\t\t [ ARREGLO INGRESADO ]"<<endl<<endl;
SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;
SetColor(9);
cout<<"\n\t\t [ ARREGLO ORDENADO POR SELECCION ]"<<endl<<endl;
MostrarSeleccionSort(num,x);
getch();
break;
}
case 3: {
SetColor(10);
cout<<"\n\t\t\t [ INGRESO DE ARREGLO ]"<<endl;
SetColor(15);
cout<<" Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;
LeerArray(num,x);
SetColor(10);
cout<<"\n\t\t\t [ ARREGLO INGRESADO ]"<<endl<<endl;
SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;
SetColor(14);
cout<<"\n\t\t [ ARREGLO ORDENADO POR INSERCION ]"<<endl<<endl;
MostrarInsercionSort(num,x);
getch();
break;
}
case 4: {
SetColor(10);
cout<<"\n\t\t\t [ INGRESO DE ARREGLO ]"<<endl;
SetColor(15);
cout<<" Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;
LeerArray(num,x);
SetColor(10);
cout<<"\n\t\t\t [ ARREGLO INGRESADO ]"<<endl<<endl;
SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;
SetColor(11);
cout<<"\n\t\t [ ARREGLO ORDENADO POR SHELL ]"<<endl<<endl;
MostrarShellSort(num,x);
getch();
break;
}
case 5: {
SetColor(10);
cout<<"\n\t\t\t [ INGRESO DE ARREGLO ]"<<endl;
SetColor(15);
cout<<" Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;
LeerArray(num,x);
SetColor(10);
cout<<"\n\t\t\t [ ARREGLO INGRESADO ]"<<endl<<endl;
SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;
SetColor(14);
cout<<"\n\t\t [ ARREGLO ORDENADO POR QUICK SORT ]"<<endl<<endl;
MostrarQuickSort(0,num-1,x,num);
getch();
break;
}
case 6: {
SetColor(10);
cout<<"\n\t\t\t [ INGRESO DE ARREGLO ]"<<endl;
SetColor(15);
cout<<" Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;
LeerArray(num,x);
SetColor(10);
cout<<"\n\t\t\t [ ARREGLO INGRESADO ]"<<endl<<endl;
SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;
SetColor(11);
cout<<"\n\t\t [ ARREGLO ORDENADO POR MERGE SORT ]"<<endl<<endl;
MostrarMergeSort(x,0,num-1,num);
//cout<<"\t";MostrarArray(num,x);
getch();
break;
}
}
}while(1);
system("PAUSE>>null");
return 0;
}
void MostrarBubleSort(int num,float x[])
{
float temp;
int z=0;
for(int i=1;i<num;i++)
{
for(int j=num-1;j>=i;j--)
{
cout<<"\t";MostrarColor(num,x);
if(x[j-1]>x[j])
{
temp = x[j-1];
x[j-1]= x[j];
x[j] = temp;
}
z++;
}
}
}
void MostrarSeleccionSort(int num,float x[])
{
float temp;
for(int i=0;i<num-1;i++)
{
int k=i;
temp = x[i];
for(int j=i+1;j<num;j++)
{
cout<<"\t";MostrarColor(num,x);
if(x[j]<temp)
{
k=j;
temp=x[j];
}
}
x[k]= x[i];
x[i]= temp;
}
}
void MostrarInsercionSort(int num,float x[])
{
int i,j;
float temp;
for( i=1;i<num+1;i++)//para que me imprima la ultima cifra
{
cout<<"\t";MostrarColor(num,x);
temp=x[i];
for(j=i-1;j>=0 && temp<x[j];j--)
x[j+1] = x[j];
x[j+1] = temp;
}
}
void MostrarShellSort(int num,float x[])
{
int band;
float temp;
for(int salto=num/2;salto>0;salto=salto/2)
do
{
band=0;
cout<<"\t";MostrarColor(num,x);
for(int i=0;i<num-salto;i++)
{
if(x[i]>x[i+salto])
{
temp = x[i];
x[i] = x[i+salto];
x[i+salto] = temp;
band=1;
}
}
}while(band);
}
void MostrarQuickSort(int ini,int fin,float x[],int num)
{
int i=ini;
int j=fin;
float central=x[(ini+fin)/2];
float temp;
do
{
cout<<"\t";MostrarColor(num,x);
while(central>x[i])i++;
while(central<x[j])j--;
if(i<=j)
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
i++;
j--;
}
}while(i<=j);
if(ini<j)MostrarQuickSort(ini,j,x,num);
if(fin>i)MostrarQuickSort(i,fin,x,num);
}
void MostrarMergeSort(float x[], int ini, int fin,int num)
{
if(ini!=fin)
{
int m = (ini+fin)/2;
MostrarMergeSort(x, ini, m,num);
MostrarMergeSort(x, m+1,fin,num);
merge(x, ini, m, fin,num);
}
}
void merge(float x[], int ini, int m, int fin,int num)
{
int *aux = new int[m-ini+1];
for(int j=ini; j<=m; j++)
aux[j-ini] = x[j];
int c1=0, c2=m+1;
for(int j=ini; j<=fin; j++)
{
cout<<"\t";MostrarColor(num,x);
if(aux[c1] < x[c2])
{
x[j] = aux[c1++];
if(c1==m-ini+1)
for(int k=c2; k<=fin; k++)
x[++j] = x[k];
}
else
{
x[j] = x[c2++];
if(c2==fin+1)
for(int k=c1; k<=m-ini; k++)
x[++j] = aux[k];
}
}
}
int menu()
{
int op;
do
{
system("cls");
SetColor(15);
cout<<"\t ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» "<<endl;
cout<<"\t º Algoritmos de Ordenamiento º "<<endl;
cout<<"\t º -------------------------------------------- º "<<endl;
cout<<"\t º [1] Ordenar por Buble Sort º "<<endl;
cout<<"\t º [2] Ordenar por Seleccion º "<<endl;
cout<<"\t º [3] Ordenar por Insercion º "<<endl;
cout<<"\t º [4] Ordenar por Shell º "<<endl;
cout<<"\t º [5] Ordenar por Quick Sort º "<<endl;
cout<<"\t º [6] Ordenar por Merge Sort º "<<endl;
cout<<"\t ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ "<<endl<<endl;
cout<<" > Ingrese opcion :";SetColor(15);cin>>op;
}while(op<1 || op>6);
return op;
}
void LeerArray(int num,float x[])
{
for(int i=0;i<num;i++)
{
SetColor(13);
cout<<" - Elemento ["<<i<<"]: ";SetColor(15);cin>>x[i];
}
}
void MostrarArray(int num,float x[])
{
int c=num;
for(int i=0;i<num;i++)
{
SetColor(15);
cout<<x[i];
c--;
if(c>0)
{
SetColor(14);cout<<" , ";
}
}
}
void MostrarColor(int num,float x[])
{
int c=num;
for(int i=0;i<num;i++)
{
SetColor(15);
cout<<x[i];
c--; //centinela para imprimir las comas
if(c>0)
{
SetColor(14);cout<<" , ";
}
}
cout<<endl<<endl;
}
void SetColor(int i)
{
if(hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, i);
}