środa, 6 lipca 2016

Array sorting in class

#include <iostream>
#include <unistd.h>
#include <stdlib.h>
using namespace std;

#define min_size_array 5
#define max_size_array 20

void init_random()
{
srand(time(NULL));
}
class My_sort
{
private:

int my_size;
int *my_array1;
int *my_array2;
int *my_array3;
public:

My_sort(int *array,int size)
{
my_size=size;
my_array1=new int[my_size];
my_array2=new int[my_size];
my_array3=new int[my_size];
int i;
for(i=0;i<my_size;i++)
{
my_array1[i]=array[i];
my_array2[i]=array[i];
my_array3[i]=array[i];
}
}
int growing_number(int i, int j)
{
return (i>j);

int decreasing_number(int i, int j)
{
return (i<j);

void replace(int *i, int *j)
{
int k;
k=*i;
*i=*j;
*j=k;
}
void sort_growing()
{
int i,j;
for(i=0;i<my_size;i++)
{
for(j=0;j<my_size-1;j++)
{
if(growing_number(my_array1[j],my_array1[j+1]))
replace(&my_array1[j],&my_array1[j+1]);
}
}
}
void sort_decr()
{
int i,j;
for(i=0;i<my_size;i++)
{
for(j=0;j<my_size-1;j++)
{
if(decreasing_number(my_array2[j],my_array2[j+1]))
replace(&my_array2[j],&my_array2[j+1]);
}
}
}
void print()
{
int i;
cout<<"Oryginal array : ";
for(i=0;i<my_size;i++)
{
cout<<my_array3[i]<<" ";
}
cout<<"\n";
cout<<"After decr: ";
for(i=0;i<my_size;i++)
{
cout<<my_array2[i]<<" ";
}
cout<<"\n";
cout<<"After incr: ";
for(i=0;i<my_size;i++)
{
cout<<my_array1[i]<<" ";
}

}
~My_sort()
{
delete my_array3;
delete my_array2;
delete my_array1;
}
};

int main(int argc, char** argv)
{
init_random();
int my_size_array,i;
my_size_array=min_size_array+rand()%(max_size_array-min_size_array);
int *my_array=new int[my_size_array];
for(i=0;i<my_size_array;i++)
my_array[i]=rand()%30;
My_sort M1_sort(my_array,my_size_array); 
M1_sort.sort_growing();
M1_sort.sort_decr();
M1_sort.print();
delete my_array;
return 0;
}

Brak komentarzy:

Prześlij komentarz