bubble sort in parallel C programming

49 views Asked by At

I want to apply parallelisation method in following code reading data from text file which have ~399999 number of integer line ranging [-2334909843, +22339935449]. I have tried dividing min,max intervals into nproc, But sorting isn't working properly.

#include <stdio.h>
#include <sys/time.h>
#define max_len 400000
#define LENGTH 40

int main(int argc, char* argv[]){
    int number_of_lines=0;
    int swap;
    char in_name[] = "sort.txt";
    char out_name[] = "sorted.txt";
    char line[LENGTH];
    int items[max_len];
    FILE *fp;
    struct timeval t1,t2;
    double time;

    fp=fopen(in_name,"r"); //read the input

    while(1)
    {
        if(fgets(line, LENGTH,fp)==NULL) break; // finish reading when empty line is read
        if(sscanf(line, "%d",&items[number_of_lines])==-1) break; // finish reading after error
            
        number_of_lines++;
    }
    gettimeofday(&t1, NULL);
    
    //sorting bubble sort
    for (int i = 0; i < number_of_lines - 1; i++)
    {
        for (int j = 0; j < number_of_lines - i - 1; j++)
        {   
            if (items[j] > items[j+1])
            {
                swap = items[j];
                items[j] = items[j+1];
                items[j+1] = swap;
            }
        }
    }
    gettimeofday(&t2, NULL);
    time = timediff(t1,t2);
    fp = fopen(out_name, "w");
    for(int i = 0; i < number_of_lines; i++) 
    {
        fprintf(fp,"%d\n",items[i]);
    }
    fclose(fp);

}

'I don't know how to control processes in such a way whole big file/array can get sorted sequentially in parallel.'

0

There are 0 answers