We have two arrays .merging them is simple.i want to know how can i sort then after that without declaring a single memory after that....

When you merged the arrays, you used pointers or indeces (int's usually), to help you do the merge.

Now, you can use that pointer or index, to temporarily hold (or point to) a value - now any of several in-place sorters will do what you want.

int a[20],b[20];
printf("enter element in first array");
{printf("Enter element %d:",i++)
printf("enter element in second array");
printf("Enter element %d:",j++)


till here these two arrays are s merged into one.now the elements in them r needed to be sorted without even declaring a int i after that.

The array a[] shall have sufficient memory for merging, the memory from a[20] is simply undefined in your code, the only valid region is from a[0] to a[19] .

okk ,i understand that...but tell me the logic after that...how can i sort after merging them,without defining a single memory...because i need to have two For loops and a temporary variable too.

If you mean you want no intermediate data, you may want to sort during the merge.

Before I continue I'd like to point out that you aren't actually merging your arrays, in the code you posted. You're overwriting one.

You're also incrementing i twice. Edit: I actually have no idea what you're doing with those 2 while loops, sorry. Also please use code tags.

If you don't care about the speed of sorting, you can use this method to swap the data of two variables without creating an intermediary variable:

if( a[19] < a[18] ) {
  a[19] ^= a[18];
  a[18] ^= a[19];
  a[19] ^= a[18];

I don't know why you would necessarily want to do this.

You would simply need to iterate backward through your array until the if statement does not pass for a single element, you could use a size 1 smaller each time, in the worst case scenario (i<20) -> (i<19) -> (i<18)

There are better ways to sort, but that's not necessarily in the scope of this explanation.

Edited 6 Years Ago by Unimportant: n/a

This question has already been answered. Start a new discussion instead.