I have a project for my university. In this project I must make a program in C language for sorting a huge table (30000 integers) with some sorting methods like bubble,quick,straight insertion and straight selection. In the output should be the number of changes in every sorting method and the time that was needed to completed. I have two problems:
- I cannot show the time that was needed
- I must redirect the output to a file but I don't know how to make it.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 300
int getUniqueNumber(int p[N],int i);
int StraightInsertion(int p[]);
int StraightSelection(int p[]);
int BubbleSort(int p[]);
int quicksort(int left, int right, int p[]);
int main(int argc, char *argv[])
{
FILE *f;
int c,p[N],p2[N];
int i,b;
int t0,t1,dt;
int s=0;
do{
for (i=0;i<N;i++)
p2[i]=getUniqueNumber(p2,i);
for(i=0;i<N;i++)
p[i]=p2[i];
printf("\nTable after sorts:\n");
printf("\n\n\n");
printf("straight selection %d\n",s+1);
time(&t0);
c=StraightSelection(p);
time(&t1);
dt=t1-t0;
printf("\n Number of changes: %d\n",c);
printf(" Processing time: %d\n",dt);
// straight insertion table
for(i=0;i<N;i++)
p[i]=p2[i];
printf("\n\n\n");
printf("straight isertion %d\n",s+1);
time(&t0);
c=StraightInsertion(p);
time(&t1);
printf("\n number of changes: %d",c);
dt=t1-t0;
printf(" Processing time = %f\n",dt);
// Bubble Sort table
for(i=0;i<N;i++)
p[i]=p2[i];
printf("\n\n\n");
printf("Bubble sort %d\n",s+1);
time(&t0);
c=BubbleSort(p);
time(&t1);
printf("\n Number of changes: %d\n",c);
dt=t1-t0;
printf(" Processing time = %f\n",dt);
// Quick Sort table
for(i=0;i<N;i++)
p[i]=p2[i];
printf("\n\n\n");
printf("Quick sort %d",s+1);
time(&t0);
c=quicksort(0,N-1,p);
time(&t1);
dt=t0-t1;
printf("\n Number of changes: %d\n",c);
printf(" Processing time = %f\n",dt);
s++;
}
while(s<20);
return 0;
}
int getUniqueNumber(int p[N],int i)
{
int x,j, found;
srand(time(NULL));
do
{
x = rand();
found = 0;
j = 0;
while (j<=i && found == 0)
{
if (p[j] == x)
found = 1;
else
j++;
}
}while (found == 1);
return x;
}
// STRAIGHT SELECTION
int StraightSelection(int p[])
{
int i,j,k,min=0,a[N];
int count=0;
for (i=0; i<N-1; i++)
{
k = i;
min = p[i];
for (j = i+1; j<N; j++)
{
if (p[j] < min)
{
k = j;
min = p[j];
count ++;
}
}
p[k] = p[i] ;
p[i] = min;
}
return count;
}
//Straight Insertion
int StraightInsertion(int p[])
{
int i,j,x;
int count=0;
for(i=1; i<N; i++)
{
x = p[i];
j = i -1;
while(x<p[j] && j>=0)
{
p[j+1] = p[j];
j = j-1;
count ++;
}
p[j+1] = x;
}
return count;
}
//Bubble Sort
int BubbleSort(int p[])
{
int i,j,temp;
int count;
for (i=1; i<N; i++)
for (j=N-1; j>=i; j--)
if (p[j-1] > p[j])
{
temp = p[j-1];
p[j-1] = p[j] ;
p[j] = temp ;
count ++;
}
return count;
}
//Quick Sort
int quicksort(int left, int right, int p[])
{
int i, j, mid, x, temp;
int count=0;
if (left < right)
{
i = left;
j = right;
mid = (left+right)/2;
x = p[mid];
while (i < j)
{
while (p[i] < x)
i++;
while (p[j] > x)
j--;
if (i < j)
{
if (p[i] == p[j])
{
if (i<mid)
i++;
if (j>mid)
j--;
}
else
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
count ++;
}
}
quicksort(left,j-1,p);
quicksort(j+1,right,p);
}
return count;
}