954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Complexity problem

Hello i'm new in java and i'm sorry if my english is not very good

i have a question to solve and i cann't figure it out how to solve it

i have this class

public class B
{
public boolean what(int []arr1,int[] arr2,int num)
{
for (int i=0;i

shila80
Newbie Poster
1 post since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

Why don't you check google. It tells you all the sorting algos and what their time complexity is.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Since the arrays are sorted, you can use a binary search for this problem. That would make the what method n(log n). Take the middle value of the array and compare it to the value you are searching for. You found your answer if the values match. Or the value you are searching for would be in the first half of the array if the middle value is greater than the value you are searching for. Or the value you are searching for is in the second half of the array if the middle value is less than the value you are searching for. Now take the half of the array where value is located and repeat the same procedure. You are splitting the array in half every time you do this. Which makes binary search a log n operation. Since you are searching for n values from array 1, your what method will be n(log n).

Here is link to an example: http://www.geocities.com/cool_ranju/bsearch.html

It will make sense if you trace the code on paper.

Rayhan Muktader
Light Poster
30 posts since Oct 2006
Reputation Points: 28
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You