Due algoritmi di ordinamento a confronto - Alessandro Barazzuol

Due algoritmi di ordinamento a confronto

#include<iostream>
#include<pthread.h>
#include<cstdlib>
#include<ctime>
#include<windows.h>
#define DIM 20000
using namespace std;

/*struct con parametri di uscita: tempo,cicli,confronti*/
struct param
{
	double tempo;
	int cicli;
	int confronti;
};

/*algoritmo bubbel sort originale*/
void *bubbel_sort_originale(void *arg)
{
	param *p=new param;
	p->cicli=0;
    p->confronti=0;
	int *v=(int *)arg;
	int cicli=0;
	clock_t start,end;
    double *tempo=new double;
    start=clock();
    
    
	for(int j=0;j<DIM-1;j++)
	{
		    
			for(int i=0;i<DIM-1;i++)
			{
			    p->cicli=p->cicli+1;
				if(v[i]>v[i+1])
			{
				p->confronti=p->confronti+1;
				swap(v[i],v[i+1]);
			
			}
			}
			
	}
	end=clock();
	*tempo=((double)(end-start));
	p->tempo=*tempo;
	
	cout<<"\nTermina Bubbel\n";
	
	pthread_exit((void *)p);
	
}


/*selection sort originale*/
void* selection_sort(void *arg)
{
	int *v=(int *)arg;
	param *p=new param;
	p->cicli=0;
	p->confronti;
	int cicli=0;
	clock_t start,end;
    double *tempo=new double;
    start=clock();
    
    
    for(int i=0; i<DIM-1; i++)
    {

     int min = i;

     for(int j=i+1; j<DIM; j++)
     {
        p->cicli=p->cicli+1;
     	if(v[j] < v[min])  //cambiare questa condizione per invertire l'ordine
     {
               p->confronti=p->confronti+1; 
               min = j;
     }
     swap(v[min],v[i]);
	 }
     
  


    }
    
    end=clock();
    *tempo=((double)(end-start));
	p->tempo=*tempo;
    cout<<"\nTermina Selection\n";

    pthread_exit((void *)p);
	
}



int main()
{
	param *p1=new param;
	param *p2=new param;
	
	int *v =new int[DIM];
	int *k =new int[DIM];
	void *s1,*s2;
	
	
	srand(time(0));
	for(int i=0;i<DIM;i++)
	v[i]=rand()%100;
	
	for(int i=0;i<DIM;i++)
	k[i]=v[i];
	
	pthread_t t1,t2;
	pthread_create(&t2,NULL,&bubbel_sort_originale,(void *)v);
	pthread_create(&t1,NULL,&selection_sort,(void *)k);

	

	
	pthread_join(t1,&s1);
	pthread_join(t2,&s2);
	
	
	cout<<endl;
	p1=(param *)s1;
	p2=(param *)s2;
    cout<<"Selection time: "<<p1->tempo<<" cicli: "<<p1->cicli<<" confronti: "<<p1->confronti<<endl;
    cout<<"Bubbel time: "<<p2->tempo<<" cilci: "<<p2->cicli<<" confronti: "<<p2->confronti<<endl;

	
}