#include <iostream>
#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>


using namespace std;

void QuickSort(int A[],int,int,int);
void Llenar_Array(int A[],int);
void Muestra_Array(int A[],int);

int main() {
        
		initscr();		       
		
		if (has_colors()) {			
               start_color();			        		   
			   init_pair(1,COLOR_WHITE,COLOR_BLUE);
			   init_pair(2,COLOR_WHITE,COLOR_RED);
              }
			  
		int N;	
				
		bkgd(COLOR_PAIR(1));                                   //Fondo Azul
		
		
		
		
		mvprintw(1,2,"Introdusca el tamaño del Arreglo A: ");
		          scanw("%d",&N);		
		int *A=(int *)malloc(sizeof(int)*N);                   //Reservación de Memoria
 		for (int i=0; i<N; i++){A[i]=NULL;}                    //Inicialización del Array	
		
		Llenar_Array(A,N);
		std::cout<<"\n\n";                                     //2 Salto de lineas         
		QuickSort(A,N,0,N-1);
		
		
		attron(COLOR_PAIR(2) | A_BLINK | A_BOLD | A_UNDERLINE);
		printw("Se ordeno el arreglo por el metodo de ordenamiento RAPIDO");		
		Muestra_Array(A,N);				
		       std::cout<<"\n\n";                             //2 Salto de lineas 
		beep();
			refresh();
			getch();
			endwin();
		
		return 0;
}

 
void Llenar_Array(int A[],int N)
{		  long int Elemento;          
          for(int i=0; i<N; i++) 		                      //Llenado del Array
                 		     {                           
         		  			  mvprintw(3+i,2,"Enter Elemento [%i] :",i);
							             scanw("%ld",&Elemento);							  
                 			  A[i]=Elemento;							  
                          }				 
}

void Muestra_Array(int A[], int N)
{
	for (int i=0; i<N; i++){std::cout<<A[i]<<"   ";}           //Muestra del Array
}

void QuickSort(int A[],int N,int Inf,int Sup)
           {
            int Tem;
      	    int S=Inf;
            int I=Sup;
            int Pivote=A[(Inf+Sup)/2];
           do
             {
            	while (Pivote>A[S]){S++;}
               while(A[I]>Pivote){I--;}
               if(S<=I)
                 {
                  Tem=A[S];
                  A[S]=A[I];
                  A[I]=Tem;
                  S++;
                  I--;
                 }
             }
             while(S<=I);
         	if (Inf<=I)
            	QuickSort(A,N,Inf,I);
            if (S<Sup)
               QuickSort(A,N,S,Sup);
}
