6 Replies - 936 Views - Last Post: 13 September 2012 - 01:40 PM Rate Topic: -----

#1 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1424
  • Posts: 3,166
  • Joined: 05-April 11

Re: pseudo code of an algorithm

Posted 13 September 2012 - 04:12 AM

View PostCuzzie, 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

Was This Post Helpful? 2
  • +
  • -

#4 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 DavsX  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • 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;
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8009
  • View blog
  • Posts: 13,716
  • Joined: 19-March 11

Re: pseudo code of an algorithm

Posted 13 September 2012 - 01:30 PM

View PostCasiOo, 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.
Was This Post Helpful? 2
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • 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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1