Sorting & Spliting

This is a discussion on Sorting & Spliting within the C Programming forums, part of the General Programming Boards category; Hi there, I want to adjust my o/e transposition sort algorithm, so that before it sorts the list using odd ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Question Sorting & Spliting

    Hi there,

    I want to adjust my o/e transposition sort algorithm, so that before it sorts the list using odd and even phases, it splits it into two first.

    But i've been trying it for ages now and I am a bit lost. Im trying to write a function that will split the user entered array in half, sort the two halves ,and merge them back together.

    So far I can get it sorting the first half, but not the second. Also when I do eventually get it sorting both halves. I guess I will have to merge them back together. But then the whole list won't be sorted. So do I have to go through the above recursively so it is? Also I am a bit lost on how to do this. Could anyone give me any advice? point in the right direction?

    Many Thanks
    Last edited by akidamo; 04-12-2006 at 02:14 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Just adding.....

    cus if I split the array into two say 3 5 4 7 2 8

    it will end up 345 and 2 7 8

    merged it will be: 3 4 5 2 7 8 or will it?

    so yea its how can I get the whole list sorted? where do I introduce recursion and still use the O/E method.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    oh but mostly pleaseee......first things first

    say i have a o/e sort function.

    and I want a split function that splits my array, and sends both halves to the o/e function. How do i do this?

    I only have:

    Code:
    void split_array(int array[], int n)
    {
      /* Cuts the array in half */
    
    
      int* array2;
      int i;
      int j;
      int split;
    
    
    	  split = n/2; /* integer division truncates */
    
    		   for (i = 0; i < split; i++) {
    			  array2[i] = array[i];
    			  }
    
    
    	   Odd_even_sort(array2, split);
    	   Print_list( array2, n, "After sort" );
    
         }
    which gets the first half.

    sorry keep posting, but trying to be clearer.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Well, here you appear to be writing to a pointer that does not point anywhere.
    Code:
    array2[i] = array[i];
    Why not post your actual code? The whole thing. That shows what you are doing.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hiya yea I was noticing a problem with that, my original O/E sort works, without any splitting, although it isn't very correct, because:

    if I have

    int a[];

    I get an error: size of a is unknown or zero in function main.

    but I dont know the size of a, until the user enters the size they want a to be.

    so for some reason I seen some sample code somewhere, using *, so I tried that and it did work, but yea I need to change it.

    but how do I declare the unknow array size, without the error?

    :s sorry

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Dynamic allocation. See malloc.

    [Perhaps your problem is on line 42? Translation: post code if you want help with code. Until you are an expert debugger and already know the solution in advance, post more than you think is necessary.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    ahh thanks,

    i see that using

    a = (int*) malloc(n*sizeof(int)); helped.

    I'll maybe post my full code a bit later once I work on it a bit more first.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    is there any functions that allow me to cut or chop an array.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    oh god please why am i so dumb
    if anyone knows where i can find sample code on an o/e merge sort, it would be great.
    I don't wanna copy I just want to comprehend.
    I already have my own oe sort and I want to modify it. But im having no luck, after spending agessssssssssssssssssssss on it. It's not clicking with me, how it should run, even thought I have read up on it.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    How do I add two arrays together into a new array?

    Say I have
    a1[3 5 6] and
    a2[7 8 9]

    and i want to add them together so I have a3 [3 5 6 7 8 9]?

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    First, get lengths of a1 and a2, say n1 and n2.

    C code for declaring an array of fixed size at runtime -- you don't need *alloc()! Just declare
    Code:
    int n = 12345;
    int array[n]; /* array now has 12345 elements */
    array[12344] = 0; /* No segfaults! */
    Declare new_array[n1 + n2] and concatenate both arrays together into the new array. Note that you can now use the built-in stack garbage-collection without needing to use free() and risk leaks.

    Still, if you're handling millions of records, *alloc() will perform better, and use up less memory.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  4. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 05:21 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21