-2

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:

  1. I cannot show the time that was needed
  2. 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;
}
Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
teobass
  • 1
  • 1
  • hi all, I forget it again! – teobass May 03 '15 at 12:10
  • SO guys won't do your homework. – alk May 03 '15 at 12:12
  • well, my homework works! but it needs, a little "push". if you are bored, no problem my friend alk... – teobass May 03 '15 at 12:20
  • Please reformat your code. Use 4 spaces for blocks of code. – byako May 03 '15 at 12:26
  • 1
    I am not bored, but perhaps partly blind, as I do not seem to be able to read a lot of what you posted. At least go back over this and apply a suitbale formatting, mark-up, indention to what you show to **the world** ... – alk May 03 '15 at 12:26
  • 1
    Searching for "output redirection" on google turned up nothing? Wow; who broke the internet? – WhozCraig May 03 '15 at 12:39
  • 1
    Your `main` has too much detail. It can (and should) look more like `int(main) (int argc, char **argv) {int lim; int s; int p[N];p2[N]; lim=argc > 1 ? strtol(argv[1], NULL, 0); build_table(p); for(s=0; s < lim; s++) { copy_table(p, p2); bubble_sort(p2); copy_table(p,p2); quick_sort(p2); }` This provides 3 major advantages: it's much easier to read, it's easier to use a debugger with, and it bins your functions for easier profiling. – William Pursell May 03 '15 at 14:38

1 Answers1

0

Output redirection in bash can be accomplished with >.

For example, if you want to redirect the output of myprog to the file myout, you would use:

./myprog >myout

To time a program's execution, you can use the time command, like so:

time ./myprog

It will count the total amount of time elapsed since your program started until it exited (usually referred to as the wall-clock time), the amount of time you spend in user-space code and the amount of time spent executing kernel code (executing syscalls, for example, as part of printing the output).

So, to time your program and redirect output to a file, you would do this:

time ./myprog >myout

As others have mentioned in the comments, these are relatively easy tasks, you could probably find this information in a few seconds. Please make sure to do some research in the future before posting new questions. Good luck with your project!

Note: This, of course, assumes you have a different program for each sorting algorithm. If you'd rather stick to a single executable, I suggest you look at Execution time of C program to learn how to time the execution of each function.

Community
  • 1
  • 1
Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • dear Goncalves thank you, but i make this program for windows 7 and not for linux. that's my problem! – teobass May 03 '15 at 13:29
  • @teobass - for windows 7, open a dos console window and manually run the program from there. Use cd to change directory to get to your program, then use "myprog >myout.txt" . – rcgldr May 03 '15 at 14:35