0
{
public void sort(String arr[])
{
String small;
int l=arr.length;
for(int i=0;i<l;i++)
{
small=arr[i];
pos=i;
for(int j=i=1;j<l;j++)
{
if((arr[i](compareToarr[j]))>1)
{
small=arr[j];
pos=j;
}
String temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp;
}
}
for(int i=0;i<l;i++)
System.out.print(arr[i]);
}
public void main(String a[],String b[])
{
int l=a.length;
int m=b.length;
int j=0,e=o;
String c[]=new String[l+m];
Strink k[]=sort(a[]);
String y[]=sort(b[]);
System.out.println("frst array \t second array");
for (int i=0;i<l;i++)
System.out.print(a[i]);
for(int j=0;j<m;j++)
System.out.print(b[j]);
System.out.println("sorted fst array="+)
for(int p=0;p<l;p++)
System.out.print(k[p]);
System.out.println("sorted second array=")
for (int q=0;q<l;q++)
{
System.out.print(y[q]);
}
for(int r=0;r<m;r++)
{
c[r]=k[r];
j++;
}
for(int z=j;z<n;z++}
{
c[z]=y[e];
e++;
}
String f[]=new String[m+n];
for(int h=0;h<(m+n);h++)
{
f[h]=c[h];}
System.out.println("merged");
for(int s=0;s<(m+n);s++)
{
System.out.println(f[s]);
}
}
}
output isnt cmng
3 answers
1
Lots of syntactical errors Abhishreya. I am removing syntax error from your code. Go through the solution and note the errors that you made.
class abc
{
public void sort(String arr[])
{
String small;
int pos=0;
int l=arr.length;
for(int i=0;i<l;i++)
{
small=arr[i];
pos=i;
for(int j=i=1;j<l;j++)
{
if((arr[i].compareTo(arr[j]))>1)
{
small=arr[j];
pos=j;
}
String temp=arr[i];
arr[i]=arr[pos];
arr[pos]=temp;
}
}
for(int i=0;i<l;i++)
System.out.print(arr[i]);
}
public void main(String a[],String b[])
{
int l=a.length;
int m=b.length;
int j=0,e=0;
String c[]=new String[l+m];
sort(a);
sort(b);
System.out.println("frst array \t second array");
for (int i=0;i<l;i++)
System.out.print(a[i]);
for(j=0;j<m;j++)
System.out.print(b[j]);
System.out.println("sorted fst array=");
for(int p=0;p<l;p++)
System.out.print(a[p]);
System.out.println("sorted second array=");
for (int q=0;q<l;q++)
{
System.out.print(b[q]);
}
for(int r=0;r<l;r++)
{
c[r]=a[r];
j++;
}
for(int z=j;z<m;z++)
{
c[z]=b[e];
e++;
}
String f[]=new String[l+m];
for(int h=0;h<(l+m);h++)
{
f[h]=c[h];}
System.out.println("merged");
for(int s=0;s<(l+m);s++)
{
System.out.println(f[s]);
}
}
}
0
0
Arrays are passed by reference in functions. That means when you pass an array 'a' in a function, any changes that you make on the array inside the function will be reflected back. In other words, say you are working on the original array. When you call function sort like sort(a), you are passing a reference of array 'a' that is stored in array 'arr'. When you sort 'arr', array 'a' is also sorted. Similarily for sort(b), reference of array 'b' is copied to 'arr' and changes made to 'arr' is reflected back. Consider, array 'arr' as a temporary array that is sorting the values of the array whose reference it receives.
Hope that helps a bit.
Log in to your account to answer this questions.