Skip to main content
added 2 characters in body
Source Link
coredump
  • 6k
  • 2
  • 23
  • 30

If you have a linked list, then you are going to need to iterate ofeach elements in sequence. You can have a binary search if you use an array instead.

Let's call L the list you are searching, and V the list of values that are searched for.

Your search function should return the sublist of L that follows the element v from V that you are searching for. Since elements are sorted, you know that the next element in V is going to be greater than v, and thus you only need to search in the sublist that follows v in L.

For example, say your lists are named as follow:

L = [1,2,3,4,5,6,7]
V = [3,4,7,8]
  1. Search for 3 in [1,2,3,4,5,6,7].
  2. Search for 4 in [4,5,6,7]
  3. Search for 7 in [5,6,7]
  4. Search for 8 in []

Anytime your search returns an empty list, the result for the search element is false (true otherwise). Since you advance in both lists at the same time, you are avoiding some useless search.

If you have a linked list, then you are going to need to iterate of elements in sequence. You can have a binary search if you use an array instead.

Let's call L the list you are searching, and V the list of values that are searched for.

Your search function should return the sublist of L that follows the element v from V that you are searching for. Since elements are sorted, you know that the next element in V is going to be greater than v, and thus you only need to search in the sublist that follows v in L.

For example, say your lists are named as follow:

L = [1,2,3,4,5,6,7]
V = [3,4,7,8]
  1. Search for 3 in [1,2,3,4,5,6,7].
  2. Search for 4 in [4,5,6,7]
  3. Search for 7 in [5,6,7]
  4. Search for 8 in []

Anytime your search returns an empty list, the result for the search element is false (true otherwise). Since you advance in both lists at the same time, you are avoiding some useless search.

If you have a linked list, then you are going to need to iterate each elements in sequence. You can have a binary search if you use an array instead.

Let's call L the list you are searching, and V the list of values that are searched for.

Your search function should return the sublist of L that follows the element v from V that you are searching for. Since elements are sorted, you know that the next element in V is going to be greater than v, and thus you only need to search in the sublist that follows v in L.

For example, say your lists are named as follow:

L = [1,2,3,4,5,6,7]
V = [3,4,7,8]
  1. Search for 3 in [1,2,3,4,5,6,7].
  2. Search for 4 in [4,5,6,7]
  3. Search for 7 in [5,6,7]
  4. Search for 8 in []

Anytime your search returns an empty list, the result for the search element is false (true otherwise). Since you advance in both lists at the same time, you are avoiding some useless search.

Source Link
coredump
  • 6k
  • 2
  • 23
  • 30

If you have a linked list, then you are going to need to iterate of elements in sequence. You can have a binary search if you use an array instead.

Let's call L the list you are searching, and V the list of values that are searched for.

Your search function should return the sublist of L that follows the element v from V that you are searching for. Since elements are sorted, you know that the next element in V is going to be greater than v, and thus you only need to search in the sublist that follows v in L.

For example, say your lists are named as follow:

L = [1,2,3,4,5,6,7]
V = [3,4,7,8]
  1. Search for 3 in [1,2,3,4,5,6,7].
  2. Search for 4 in [4,5,6,7]
  3. Search for 7 in [5,6,7]
  4. Search for 8 in []

Anytime your search returns an empty list, the result for the search element is false (true otherwise). Since you advance in both lists at the same time, you are avoiding some useless search.