Algoritmos de Ordenamiento

Moderador: ivancp

Temas sobre programacion en C/C++ (no Visual C++)

Algoritmos de Ordenamiento

Notapor car10scs » Lun Nov 14, 2011 8:09 pm

Estos algoritmos los implemente para la clase de Estructura de Datos...espero les sirva en algo,utilize la API de windows para darle una mejor presentación... :lol:
Código: Seleccionar todo
  1.  

  2. // Autor: Carlos Castañeda Gallardo --> cronicacomputista.wordpre....

  3. #include <iostream>

  4. #include <windows.h>

  5. #include <conio.h>

  6. HANDLE hCon;

  7. using namespace std;

  8. void SetColor(int i);

  9. void LeerArray(int num,float x[]);

  10. void MostrarArray(int num,float x[]);

  11. void MostrarColor(int num,float x[]);

  12. void MostrarBubleSort(int num,float x[]);

  13. void MostrarSeleccionSort(int num,float x[]);

  14. void MostrarInsercionSort(int num,float x[]);

  15. void MostrarShellSort(int num,float x[]);

  16. void MostrarQuickSort(int ,int,float x[],int);

  17. void MostrarMergeSort(float x[],int,int,int);

  18. void merge(float x[], int ini, int m, int fin,int num);

  19. int menu();

  20. int main()

  21. {

  22.  int op,num;

  23.  float x[50];

  24.  do

  25.     {

  26.         op=menu();

  27.         switch(op)

  28.         {

  29.          case 1:   {

  30.                       SetColor(10);

  31.                       cout<<"\n\t\t\t   [ INGRESO DE ARREGLO ]"<<endl;

  32.                       SetColor(15);

  33.                       cout<<"   Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;

  34.                       LeerArray(num,x);

  35.                       SetColor(10);

  36.                       cout<<"\n\t\t\t   [ ARREGLO  INGRESADO ]"<<endl<<endl;

  37.                       SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;

  38.                       SetColor(13);

  39.                       cout<<"\n\t\t   [ ARREGLO  ORDENADO POR BUBLE SORT ]"<<endl<<endl;

  40.                       MostrarBubleSort(num,x);

  41.                       getch();

  42.                       break;

  43.                       }

  44.          case 2:   {

  45.                       SetColor(10);

  46.                       cout<<"\n\t\t\t   [ INGRESO DE ARREGLO ]"<<endl;

  47.                       SetColor(15);

  48.                       cout<<"   Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;

  49.                       LeerArray(num,x);

  50.                       SetColor(10);

  51.                       cout<<"\n\t\t\t   [ ARREGLO  INGRESADO ]"<<endl<<endl;

  52.                       SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;

  53.                       SetColor(9);

  54.                       cout<<"\n\t\t   [ ARREGLO  ORDENADO POR SELECCION ]"<<endl<<endl;

  55.                       MostrarSeleccionSort(num,x);

  56.                       getch();

  57.                       break;

  58.                     }

  59.          case 3:   {

  60.                       SetColor(10);

  61.                       cout<<"\n\t\t\t   [ INGRESO DE ARREGLO ]"<<endl;

  62.                       SetColor(15);

  63.                       cout<<"   Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;

  64.                       LeerArray(num,x);

  65.                       SetColor(10);

  66.                       cout<<"\n\t\t\t   [ ARREGLO  INGRESADO ]"<<endl<<endl;

  67.                       SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;

  68.                       SetColor(14);

  69.                       cout<<"\n\t\t   [ ARREGLO  ORDENADO POR INSERCION ]"<<endl<<endl;

  70.                       MostrarInsercionSort(num,x);

  71.                       getch();

  72.                       break;

  73.                     }

  74.          case 4:   {

  75.                       SetColor(10);

  76.                       cout<<"\n\t\t\t   [ INGRESO DE ARREGLO ]"<<endl;

  77.                       SetColor(15);

  78.                       cout<<"   Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;

  79.                       LeerArray(num,x);

  80.                       SetColor(10);

  81.                       cout<<"\n\t\t\t   [ ARREGLO  INGRESADO ]"<<endl<<endl;

  82.                       SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;

  83.                       SetColor(11);

  84.                       cout<<"\n\t\t   [ ARREGLO  ORDENADO POR SHELL ]"<<endl<<endl;

  85.                       MostrarShellSort(num,x);

  86.                       getch();

  87.                       break;

  88.                     }

  89.          case 5:   {

  90.                       SetColor(10);

  91.                       cout<<"\n\t\t\t   [ INGRESO DE ARREGLO ]"<<endl;

  92.                       SetColor(15);

  93.                       cout<<"   Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;

  94.                       LeerArray(num,x);

  95.                       SetColor(10);

  96.                       cout<<"\n\t\t\t   [ ARREGLO  INGRESADO ]"<<endl<<endl;

  97.                       SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;

  98.                       SetColor(14);

  99.                       cout<<"\n\t\t   [ ARREGLO  ORDENADO POR QUICK SORT ]"<<endl<<endl;

  100.                       MostrarQuickSort(0,num-1,x,num);

  101.                       getch();

  102.                       break;

  103.                     }

  104.          case 6:   {

  105.                       SetColor(10);

  106.                       cout<<"\n\t\t\t   [ INGRESO DE ARREGLO ]"<<endl;

  107.                       SetColor(15);

  108.                       cout<<"   Ingrese tamanio : ";SetColor(14);cin>>num;cout<<endl;

  109.                       LeerArray(num,x);

  110.                       SetColor(10);

  111.                       cout<<"\n\t\t\t   [ ARREGLO  INGRESADO ]"<<endl<<endl;

  112.                       SetColor(13);cout<<"\t [ ";MostrarArray(num,x);SetColor(13);cout<<"] "<<endl;

  113.                       SetColor(11);

  114.                       cout<<"\n\t\t   [ ARREGLO  ORDENADO POR MERGE SORT ]"<<endl<<endl;

  115.                       MostrarMergeSort(x,0,num-1,num);

  116.                       //cout<<"\t";MostrarArray(num,x);

  117.                       getch();

  118.                       break;

  119.                     }

  120.             }

  121.     }while(1);

  122.     system("PAUSE>>null");

  123.     return 0;

  124. }

  125.  

  126. void MostrarBubleSort(int num,float x[])

  127. {

  128.     float temp;

  129.     int z=0;

  130.     for(int i=1;i<num;i++)

  131.     {

  132.         for(int j=num-1;j>=i;j--)

  133.         {

  134.             cout<<"\t";MostrarColor(num,x);

  135.             if(x[j-1]>x[j])

  136.             {

  137.                 temp  = x[j-1];

  138.                 x[j-1]= x[j];

  139.                 x[j]  = temp;

  140.             }

  141.             z++;

  142.  

  143.         }

  144.     }

  145.  

  146. }

  147.  

  148. void MostrarSeleccionSort(int num,float x[])

  149. {

  150.     float temp;

  151.     for(int i=0;i<num-1;i++)

  152.     {

  153.         int k=i;

  154.         temp = x[i];

  155.         for(int j=i+1;j<num;j++)

  156.         {

  157.             cout<<"\t";MostrarColor(num,x);

  158.             if(x[j]<temp)

  159.             {

  160.                 k=j;

  161.                 temp=x[j];

  162.             }

  163.         }

  164.         x[k]= x[i];

  165.         x[i]= temp;

  166.     }

  167. }

  168.  

  169. void MostrarInsercionSort(int num,float x[])

  170. {

  171.     int i,j;

  172.     float temp;

  173.     for( i=1;i<num+1;i++)//para que me imprima la ultima cifra

  174.     {

  175.         cout<<"\t";MostrarColor(num,x);

  176.         temp=x[i];

  177.         for(j=i-1;j>=0 && temp<x[j];j--)

  178.             x[j+1] = x[j];

  179.             x[j+1] = temp;

  180.     }

  181. }

  182.  

  183. void MostrarShellSort(int num,float x[])

  184. {

  185.     int band;

  186.     float temp;

  187.     for(int salto=num/2;salto>0;salto=salto/2)

  188.     do

  189.     {

  190.         band=0;

  191.         cout<<"\t";MostrarColor(num,x);

  192.         for(int i=0;i<num-salto;i++)

  193.         {

  194.             if(x[i]>x[i+salto])

  195.             {

  196.                 temp = x[i];

  197.                 x[i] = x[i+salto];

  198.                 x[i+salto] = temp;

  199.                 band=1;

  200.             }

  201.         }

  202.     }while(band);

  203. }

  204.  

  205. void MostrarQuickSort(int ini,int fin,float x[],int num)

  206. {

  207.     int i=ini;

  208.     int j=fin;

  209.     float central=x[(ini+fin)/2];

  210.     float temp;

  211.     do

  212.     {

  213.         cout<<"\t";MostrarColor(num,x);

  214.         while(central>x[i])i++;

  215.         while(central<x[j])j--;

  216.         if(i<=j)

  217.         {

  218.             temp = x[i];

  219.             x[i] = x[j];

  220.             x[j] = temp;

  221.             i++;

  222.             j--;

  223.         }

  224.     }while(i<=j);

  225.     if(ini<j)MostrarQuickSort(ini,j,x,num);

  226.     if(fin>i)MostrarQuickSort(i,fin,x,num);

  227. }

  228.  

  229. void MostrarMergeSort(float x[], int ini, int fin,int num)

  230. {

  231.   if(ini!=fin)

  232.    {

  233.      int m = (ini+fin)/2;

  234.      MostrarMergeSort(x, ini, m,num);

  235.      MostrarMergeSort(x, m+1,fin,num);

  236.      merge(x, ini, m, fin,num);

  237.    }

  238. }

  239.  

  240. void merge(float x[], int ini, int m, int fin,int num)

  241. {

  242.    int *aux = new int[m-ini+1];

  243.    for(int j=ini; j<=m; j++)

  244.    aux[j-ini] = x[j];

  245.  

  246.    int c1=0, c2=m+1;

  247.    for(int j=ini; j<=fin; j++)

  248.    {

  249.     cout<<"\t";MostrarColor(num,x);

  250.     if(aux[c1] < x[c2])

  251.      {

  252.       x[j] = aux[c1++];

  253.       if(c1==m-ini+1)

  254.       for(int k=c2; k<=fin; k++)

  255.       x[++j] = x[k];

  256.      }

  257.      else

  258.      {

  259.       x[j] = x[c2++];

  260.       if(c2==fin+1)

  261.       for(int k=c1; k<=m-ini; k++)

  262.       x[++j] = aux[k];

  263.      }

  264.    }

  265. }

  266. int menu()

  267. {

  268.    int op;

  269.    do

  270.    {

  271.      system("cls");

  272.      SetColor(15);

  273.      cout<<"\t     ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»  "<<endl;

  274.      cout<<"\t     º              Algoritmos de Ordenamiento            º  "<<endl;

  275.      cout<<"\t     º    --------------------------------------------    º  "<<endl;

  276.      cout<<"\t     º        [1]  Ordenar por Buble Sort                 º  "<<endl;

  277.      cout<<"\t     º        [2]  Ordenar por Seleccion                  º  "<<endl;

  278.      cout<<"\t     º        [3]  Ordenar por Insercion                  º  "<<endl;

  279.      cout<<"\t     º        [4]  Ordenar por Shell                      º  "<<endl;

  280.      cout<<"\t     º        [5]  Ordenar por Quick Sort                 º  "<<endl;

  281.      cout<<"\t     º        [6]  Ordenar por Merge Sort                 º  "<<endl;

  282.      cout<<"\t     ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ  "<<endl<<endl;

  283.      cout<<" > Ingrese opcion   :";SetColor(15);cin>>op;

  284.    }while(op<1 || op>6);

  285.    return op;

  286. }

  287.  

  288. void LeerArray(int num,float x[])

  289. {

  290.     for(int i=0;i<num;i++)

  291.     {

  292.         SetColor(13);

  293.         cout<<"     - Elemento ["<<i<<"]: ";SetColor(15);cin>>x[i];

  294.     }

  295. }

  296.  

  297. void MostrarArray(int num,float x[])

  298. {

  299.     int c=num;

  300.     for(int i=0;i<num;i++)

  301.     {

  302.         SetColor(15);

  303.         cout<<x[i];

  304.         c--;

  305.         if(c>0)

  306.         {

  307.           SetColor(14);cout<<" , ";

  308.         }

  309.     }

  310. }

  311.  

  312. void MostrarColor(int num,float x[])

  313. {

  314.     int c=num;

  315.     for(int i=0;i<num;i++)

  316.     {

  317.         SetColor(15);

  318.         cout<<x[i];

  319.         c--; //centinela para imprimir las comas

  320.         if(c>0)

  321.         {

  322.           SetColor(14);cout<<" , ";

  323.         }

  324.     }

  325.     cout<<endl<<endl;

  326. }

  327.  

  328. void SetColor(int i)

  329. {

  330.         if(hCon == NULL)

  331.                 hCon = GetStdHandle(STD_OUTPUT_HANDLE);

  332.         SetConsoleTextAttribute(hCon, i);

  333. }

  334.  

car10scs
Novato
Novato
 
Mensajes: 1
Registrado: Lun Nov 14, 2011 7:50 pm


    

Volver a C/C++

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 0 invitados