-
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
-
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.
-
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.
-
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.
-
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
-
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.]
-
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.
-
-
is there any functions that allow me to cut or chop an array.
-
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.
-
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]?
-
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.