Thread: sort

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    sort

    Hey, i want to sort an array by using 4 processes in parallel.
    i have 4 processes that should sort the same array.
    i have to create 4 fork, and make a sort on the same array.
    i am having an array, that i read from file-fd, in size. the number of elements is n.
    i send it to a qsort that using comp function.
    right now, i can sort the array in one processes, but i have to do it by 4 processes in parallel.
    my idea was to regard the array as 4 subarrays, sort each of them, at the end to merge them.
    but my problems are: i dont know how can i regard them as 4 arrays, by using this function, how can i "tell" each processes its boundaries?
    The writing of merge function isnt so difficult, but right now i have much urgent problems..
    my code:
    Code:
    01    rfile = read ( fd , array , size );
    02	size_t n= st.st_size/sizeof array[0];
    03	qsort(shm, n,atoi(argv[3]) , comp);
    04	write(1, shm,st.st_size);
    05	return 1;
    06	}
    07	    
    08	int comp(const void * a,const void * B){
    09	 const  int *the_a = a;
    10	 const  int *the_b = b;
    11	 if (*the_a > *the_B) return 1;
    12	 else if (*the_a < *the_B) return -1;
    13	 else return 0;
    14	}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    For 2 processes, you would do

    Code:
    // the first half
    qsort(&shm[0], n/2,atoi(argv[3]) , comp);
    and
    Code:
    // the second half
    qsort(&shm[n/2], n-n/2,atoi(argv[3]) , comp);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Just a heads up that this was cross-posted here and here as well.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Great - it can be closed here then. No point in wasting any more effort on the selfish poster.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sort function
    By compnub1 in forum C Programming
    Replies: 1
    Last Post: 04-30-2010, 09:57 AM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  4. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM