1. ## Sorting Arrays

Hello Guys..
I need to ask a question about how to make a program by defining two arrays
one of them has 135 and another has 246
and then they are merged together in one array by order by sorting
I am just practising arrays and I am stuck in it a little ?? 2. 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".) 3. I will try  4. Originally Posted by DaniiChris I will try Here's a good exercise: take a sheet of paper and tear or cut it up into 1 inch squares (2.5 cm or so). On each square, write in an integer (whole number).

Take the squares and make two piles with them, of unequal sizes.

Now, with the numbers all tossed up into their two piles so they are not in sequence at all, take the two piles and sort them out into a new pile which will contain all the numbered squares, in sorted order.

That's *exactly* what you want to do with your program.

This is a fun one, and has some wrinkles in it, also.  5. Thanks Adak..I will try this out! I guess it will work  6. Just don't cheat and use some other algorithm in the exercise, now! You have to use tabstop's basic description of merging, to have a merge sort.

Anything else is *cheating*!   7. ok  8. Welll....Its been 3 days am trying but,I give up ! I am so dumb! and I can't do this program :'( 9. Originally Posted by DaniiChris Welll....Its been 3 days am trying but,I give up ! I am so dumb! and I can't do this program :'(
So can you post what you've got so far? Assuming of course that not your final word on the subject.

--
Mats 10. Code:
```#include <stdio.h>
#define SIZE 10

int main()
{
int a[ SIZE ] = {2,4,6,8,10,12 };
int pass;
int i;
int hold;
printf("Data items in original order\n");
for(i=0; i < SIZE ; i++)
{printf("&#37;4d",a[i]);}

for (pass=1; pass < SIZE; pass++)
{
for (i=0; i <SIZE -1; i++)
{
if
(
a[i]>a[i+1])
{hold= a[i];
a[i]-a[i+1];
a[i+1]=hold;
}
}
}
printf("\nData items in ascending order\n");
for (i=0;i < SIZE;i++)
{
printf("%4d",a[i]);
}
printf("\n");
return(0);
}```
Is that right...I did my best  11. 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: &#37;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? 12. The OP may be long gone, I don't know. I wanted to add a few things:

1) If you add a simple bubble type sort to the small arrays, before merging them, it makes a lot of sense. Then, after the data is merged, everything is in proper sorted order, and in just the one array.

If you have more than 10,000 numbers to be sorted, then OK, pick a faster sorting algorithm.

2) This website has more info on merge sort, than I could ever post up, here, including applets on how they work, etc.:
http://www.inf.fh-flensburg.de/lang/...rge/mergen.htm

3) Coding up one of the faster sorting algorithms (quicksort, mergesort, radix sort), is not a trivial job. Find an example of each one that you find useful, and then use it. Even if you get your own "version" of the sort, running, the odds are that it will not be running at the optimum speed and memory size, any more. Unless you test it thoroughly, you may find it won't even sort right, for all combinations of numbers.  Popular pages Recent additions 