As I recall, you had *two* arrays of different sizes, and needed to merge sort them together into one *big* array.
And let's remember Tabstop's good advice, in his post:
You're going to need one big array to hold the result. You just compare the "top of the deck" from each one -- the lower one goes in the big array and you move on to the next number in that array. Keep comparing until one array runs out of numbers. (And of course, the magic word is "merge sort".)
Usually in a merge sort, you have sorted data, and *then* you merge those pieces of sorted data together. So array1 (a1[]) gets sorted, then array2 (a2[]) gets sorted, and finally those arrays are merged together into array big[].
Your description didn't exactly match that, imo, so I thought we'd do this just as you described you wanted it done. We'll leave both small arrays unsorted, and sort everything out, as we merge them together, into the big[] array.
Code:
#include <stdio.h>
#define SIZE 17
void show_it(int a1_size, int a2_size);
int a1[] = {99, 21, 64, 12, 85, 88, 41};
int a2[] = {27, 55, 50, 33, 19, 64, 11, 78, 92, 40};
int big[SIZE] = {0};
int main(void) {
int i, j, k, a1_size, a2_size, sorted, hand1, hand2, gar;
//get each array's size
a1_size = sizeof(a1) /sizeof(int);
a2_size = sizeof(a2) /sizeof(int);
printf("\n a1 has: %d numbers in it. a2 has: %d in it.", a1_size, a2_size);
gar = getchar(); gar++;
//show the arrays
show_it(a1_size, a2_size);
//let's merge
i = j = k = 0;
while(i < a1_size && j < a2_size) {
hand1 = hand2 = 0;
hand1 = a1[i];
hand2 = a2[j];
if(hand1 < hand2) {
big[k] = hand1;
++i; ++k;
}
else if (hand2 < hand1) {
big[k] = hand2;
++j; ++k;
}
else { //hand1 == hand2
big[k] = hand1;
big[++k] = hand2;
++i; ++j; ++k;
}
}
show_it(a1_size, a2_size);
//does a1 still have some numbers that need to go into big?
while(i < a1_size) {
big[k++] = a1[i++];
}
//does a2 still have some numbers that need to go into big?
while(j < a2_size) {
big[k++] = a2[j++];
}
show_it(a1_size, a2_size);
return 0;
}
void show_it(int a1_size, int a2_size) {
static int first = 0;
int i, gar;
if(!first) {
printf("\n a1[]:\n");
for(i = 0; i < a1_size; ++i)
printf(" %d", a1[i]);
printf("\n");
printf("\n a2[]:\n");
for(i = 0; i < a2_size; ++i)
printf(" %d", a2[i]);
printf("\n");
}
printf("\n big[]:\n");
for(i = 0; i < SIZE; ++i)
printf(" %d", big[i]);
printf("\n");
++first;
printf("\n\t\t\t\t Hit Enter to Continue");
gar = getchar(); gar++;
}
If you run this, you'll notice that the numbers aren't sorted right. That's because I very smartly put a 99 right
at the first element of one of the arrays.
<sigh>
Doesn't matter though. Now we can merge sort the big array, and finish it off.
I'm going to take the above code, and put it into my IDE to do that. I'll post it up, later.
Do you have any questions, so far?