threaded merge sort help


 
Thread Tools Search this Thread
Top Forums Programming threaded merge sort help
# 1  
Old 04-30-2005
threaded merge sort help

I am working on a merge sort of two files of integers, and am fuzzy on some of the logic\syntax. I need two threads, each of which will open a file, read its contents into an array, and then sort the array using qsort. One thread will operate on file f1.dat(10000 numbers) and leave its sorted contents in array x, while the other will use file f2.dat(11999 numbers) and array y. When the threads have finished, I need to merge x and y into array z so that z is also sorted. Also, I am wanting to use one function for both threads, passing the array, its size, and the name of the file in a structure.

Currently, I have a struct set up with the arrays, their sizes, and the name of the two files, a comparison function for qsort, a sort function for the contents of the files, a merge sort for the z array, and of course the creation of the threads.

I think the biggest area I have questions about is how to properly pass a structure to a generalized sort function and then sort data using that structure parameter.

So far:

Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

typedef struct Data
{
  int x[10000];
  int y[12000];
  int size;
  char *filename;
} data_t;


// Comparison function for qsort
int cmpint (int *a, int *b)
{
  if (*a < *b)
     return -1;
  else
     if (*a > *b)
       return 1;
     else
       return 0;
}


// Sort contents of files
// How to generalize this to work with two separate arrays???????
void *sort(void *data)
{
  FILE *fp;
  //long lSize = ftell (fp);
  fp = fopen(filename.x[], "rb");
  fread(x, 10000, size, fp);
  
  //data_t *my_data = (data_t *) data; 

  qsort(x,1000,sizeof(int),cmpint);
  pthread_exit(0);

}


int main()  {

  int i=0,j=0,k=0;
  char z[22000];
  pthread_t px, py;

  pthread_create(&px, NULL, sort, (void *) &data_x);
  pthread_create(&py, NULL, sort, (void *) &data_x);

  pthread_join(px, 0);
  pthread_join (py, 0);

  // Merge x and y into z, sorted
  while (i<1000 || j < 1000)
  {
    if (j == 1000)
       z[k++]=x[i++];

    else
       if (i<1000 && (x[i]<y[j]))
          z[k++]=x[i++];
    else
          z[k++]=y[j++];
  }


}


Last edited by AusTex; 04-30-2005 at 10:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge and Sort tabular data from different text files

I have 42 text files; each containing up to 34 lines with following structure; file1 H-01 23 H-03 5 H-05 9 H-02 14 . . file2 H-01 17 H-02 43 H-04 7 H-05 8 H-03 7 . . file3 (6 Replies)
Discussion started by: Syeda Sumayya
6 Replies

2. Shell Programming and Scripting

Seperated by columns, merge in a file, sort them on common column

Hi All, I have 4 files in below format. I took them as an example. File 1: Cut from position 1-4 then 6-7 then 8-14 then rest left and make them as columns in one new file. Inserting character H to the initial of all line like HCTOT. CTOT 456787897 Low fever CTOR 556712345 High fever... (2 Replies)
Discussion started by: Mannu2525
2 Replies

3. Shell Programming and Scripting

Need threaded python script

I have a single threaded bash script that I am using to create secgroup rules in openstack. The process to add the rules is taking forever. Any of you python gurus know how to convert this bash script into a thread python script? Thanks in advanced. create-secgroup-rules.sh: #!/bin/bash cat ... (2 Replies)
Discussion started by: stovie1000
2 Replies

4. Programming

Merge sort when array starts from zero(0) index???

Hi friends, I have implemented the merge sort algorith in c, before I put forward my question, you please have a look at my code. // The array is sorted, as 1234 #include <stdio.h> #include <stdlib.h> #include <math.h> int A = {4, 3, 2, 1}; void Merge_Sort(int , int, int); void... (0 Replies)
Discussion started by: gabam
0 Replies

5. Shell Programming and Scripting

sort split merge -u unique

Hi, this is about sorting a very large file (like 10 gb) to keep lines with unique entries across SOME of the columns. The line originally looked like this: sort -u -k2,2 -k3,3n -k4,4n -k5,5n -k6,6n file_unsorted > file_sorted please note the -u flag. The problem is that this single... (4 Replies)
Discussion started by: jbr950
4 Replies

6. UNIX for Dummies Questions & Answers

Merge files and sort them

hi all, i have three files naming file1 , file2, file3 . i want to merge the content of these three files, sort them and display the sorted output on the screen page by page. (1 Reply)
Discussion started by: sonu_pal
1 Replies

7. Programming

help me out with my threaded c++ mudbase - c++, pthread_cond_wait

hello, in my free time i am writing on a c++ mud codebase, a while ago i decided that i would move to pthreads, so i could make use of smp. now i have a problem which i just cant fix - for weeks now. i have a main thread which spawns my threads, as soon as spawned they get a pthread_cond_wait, so... (4 Replies)
Discussion started by: sonicx
4 Replies

8. Programming

Threaded 'find' utility

I need to modify my version of find in unix and get it to create and use two POSIX threads to carry out concurrent finding operations. How do i get about doing this>? If anyone could help me it would be much appreciated. Thanx Mariuca (1 Reply)
Discussion started by: mariuca
1 Replies

9. UNIX for Dummies Questions & Answers

Threaded Discussions for Webpages

Dear All, I run a website for a non-profit. Does anyone know where I can get free or cheap software to run threaded discussions for our website? Our website is obviously running off a unix platform. Thanks (4 Replies)
Discussion started by: evertk
4 Replies
Login or Register to Ask a Question