Two Sorted Array, merge them

View previous topic View next topic Go down

Two Sorted Array, merge them

Post  Admin on Thu Aug 25, 2011 12:10 am

public int[] Merge(int[] a1, int[] a2){
int len1=a1.length()-1;
int len2=a2.length()-1;
int len=len1+len2+1;
int[] arr=new int[len];
while(len1>=0&&len2>=0){
if(a1[len1]>a2[len2])
arr[len--]=a1[len1--];
else
arr[len--]=a1[len2--];
}
if(a1.length()>a2.length()){
while(len1>0){
arr[len--]=a1[len1--];
}
}
else{
while(len2>0){
arr[len--]=a2[len2--];
}
}
return arr;
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

Re: Two Sorted Array, merge them

Post  viterbi on Thu Sep 08, 2011 9:56 pm

Why you put this topic here?

Admin wrote:public int[] Merge(int[] a1, int[] a2){
int len1=a1.length()-1;
int len2=a2.length()-1;
int len=len1+len2+1;
int[] arr=new int[len];
while(len1>=0&&len2>=0){
if(a1[len1]>a2[len2])
arr[len--]=a1[len1--];
else
arr[len--]=a1[len2--];
}
if(a1.length()>a2.length()){
while(len1>0){
arr[len--]=a1[len1--];
}
}
else{
while(len2>0){
arr[len--]=a2[len2--];
}
}
return arr;
}

viterbi

Posts : 32
Join date : 2011-09-03

View user profile

Back to top Go down

Re: Two Sorted Array, merge them

Post  Admin on Thu Sep 08, 2011 11:31 pm

The code is to merge two sorted array. Anything wrong here? Shocked

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

Re: Two Sorted Array, merge them

Post  viterbi on Fri Sep 09, 2011 9:16 am

I mean most questions in this folder are knowledge-based questions. Then there's one algorithm question, looks strange.

Anyway, I think there should be some folders for algorithm, I have "Sorting" (which you already have), "recursive", "dynamic programming", "bit operation", "binary search", "random algorithm" and "other algorithm" on my computer. I hope you could also create these forums.

viterbi

Posts : 32
Join date : 2011-09-03

View user profile

Back to top Go down

Re: Two Sorted Array, merge them

Post  Admin on Fri Sep 09, 2011 11:58 am

I already created some topics as what you listed. By the way, what is random algorithm?

viterbi wrote:I mean most questions in this folder are knowledge-based questions. Then there's one algorithm question, looks strange.

Anyway, I think there should be some folders for algorithm, I have "Sorting" (which you already have), "recursive", "dynamic programming", "bit operation", "binary search", "random algorithm" and "other algorithm" on my computer. I hope you could also create these forums.

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

Re: Two Sorted Array, merge them

Post  viterbi on Sun Sep 11, 2011 10:43 pm

Random algorithm is the name I gave Very Happy It includes the algorithms that deal with randomness, which includes the classic algorithms: Fisher-Yates shuffle, reservoir sampling, and some others.

Admin wrote:I already created some topics as what you listed. By the way, what is random algorithm?

viterbi wrote:I mean most questions in this folder are knowledge-based questions. Then there's one algorithm question, looks strange.

Anyway, I think there should be some folders for algorithm, I have "Sorting" (which you already have), "recursive", "dynamic programming", "bit operation", "binary search", "random algorithm" and "other algorithm" on my computer. I hope you could also create these forums.

viterbi

Posts : 32
Join date : 2011-09-03

View user profile

Back to top Go down

Re: Two Sorted Array, merge them

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum