# pseudo code of an algorithm

Page 1 of 1

## 6 Replies - 408 Views - Last Post: 13 September 2012 - 01:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291814&amp;s=eea7ca0a0834cec5800b08f69605f5ce&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 swim_5318

Reputation: 1
• Posts: 84
• Joined: 05-December 11

# pseudo code of an algorithm

Posted 13 September 2012 - 03:13 AM

I need to write a pseudo code of an algorithm that returns 1 if every element of array A is in Array B, and 0 otherwise.
I know I need to sort both arrays first then I want to do a linear compare.... I just don't know how to write it. Here is what I have. Some guidance would be great, Thanks!

Algorithm arrayEqual(A, B )
Input: Arrays A and B of n elements
Ouput: 1 or 0

This post has been edited by swim_5318: 13 September 2012 - 03:13 AM

Is This A Good Question/Topic? 0

## Replies To: pseudo code of an algorithm

### #2 Cuzzie

• D.I.C Regular

Reputation: 72
• Posts: 341
• Joined: 16-July 10

## Re: pseudo code of an algorithm

Posted 13 September 2012 - 03:41 AM

I don't think you need to sort the arrays. You can use 2 for loops to compare the elements in both array A and array B. Also, you will need an integer that defaults to 1. Change it to 0 if you find any element in array A that doesn't exist in array B.

### #3 CasiOo

• D.I.C Lover

Reputation: 994
• Posts: 2,203
• Joined: 05-April 11

## Re: pseudo code of an algorithm

Posted 13 September 2012 - 04:12 AM

Cuzzie, on 13 September 2012 - 10:41 AM, said:

I don't think you need to sort the arrays. You can use 2 for loops to compare the elements in both array A and array B. Also, you will need an integer that defaults to 1. Change it to 0 if you find any element in array A that doesn't exist in array B.

True he doesn't need to sort the arrays. You don't have to declare an integer to hold the value 1/0 and change it on the way. When you find an element that is not in array B you return 0 immediatly, no need to save it. If the checking has finished without the method returning, you would then return 1.

edit: You said you wanted a linear compare, but what if the sorted arrays are like these
```int[] a1 = { 5, 6, 20, 22 };
int[] a2 = { 5, 6, 8, 10, 11, 13, 16, 17 20, 22 };

```

This post has been edited by CasiOo: 13 September 2012 - 04:18 AM

### #4 swim_5318

Reputation: 1
• Posts: 84
• Joined: 05-December 11

## Re: pseudo code of an algorithm

Posted 13 September 2012 - 04:28 AM

Okay, now I am lost. What would my loop look like to get them to compare? So I need Array A elements to compare one at a time to the B array?

### #5 DavsX

• New D.I.C Head

Reputation: 7
• Posts: 23
• Joined: 23-August 12

## Re: pseudo code of an algorithm

Posted 13 September 2012 - 04:50 AM

for every item in array A:
- Is it in array B? yes -> do nothing; no -> return 0 ;
return 1;

### #6 jon.kiparsky

• Pancakes!

Reputation: 5422
• Posts: 8,717
• Joined: 19-March 11

## Re: pseudo code of an algorithm

Posted 13 September 2012 - 01:30 PM

CasiOo, on 13 September 2012 - 06:12 AM, said:

True he doesn't need to sort the arrays.

However, it'll save a lot of time if he does, as the size of the arrays gets large.

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8022
• Posts: 31,133
• Joined: 06-March 08

## Re: pseudo code of an algorithm

Posted 13 September 2012 - 01:40 PM

lot less iterations... just compare one by one and return 0 if not found