Search algorithm with best Time Complexity [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
How do I search for a number in a 2d array sorted left to right and top to bottom?
19 answers
Given the following data:
[4]
[5, 8]
[9, 12, 20]
[10, 15, 23, 28]
[14, 19, 31, 36, 48]
[15, 22, 34, 41, 53, 60]
[19, 26, 42, 49, 65, 72, 88]
[20, 29, 45, 54, 70, 79, 95, 104]
[24, 33, 53, 62, 82, 91, 111, 120, 140]
[25, 36, 56, 67, 87, 98, 118, 129, 149, 160]
[29, 40, 64, 75, 99, 110, 134, 145, 169, 180, 204]
[30, 43, 67, 80, 104, 117, 141, 154, 178, 191, 215, 228]
[34, 47, 75, 88, 116, 129, 157, 170, 198, 211, 239, 252, 280]
[35, 50, 78, 93, 121, 136, 164, 179, 207, 222, 250, 265, 293, 308]
[Etc.]
What could be the best searching algorithm with the most optimal Time Complexity for finding a given number?
- The rows are sorted
- The columns are sorted
- A number may occur more than once
Extra info:
Suppose we are looking for the number 26:
Due to order, this means we can eliminate the first 3 rows and the remaining columns to the right.
Due to order, this also means we can ignore every row after row=11.
Which results to this:
[10, 15, 23]
[14, 19, 31]
[15, 22, 34]
[19, 26, 42]
[20, 29, 45]
[24, 33, 53]
[25, 36, 56]
[29, 40, 64]
My current algorithm has a time complexity of O(x log(y)) where x is the amount of columns and y is the size for the Binary Search algorithm for each column.
I'm looking for something faster because I'm dealing with huge amount of data.
Currently I'm using BST on every column, but could I use BST on rows aswell? maybe achieving a O(log(x) log(y))?
algorithm search time time-complexity
marked as duplicate by Eli, Raidri, Community♦ Jan 4 at 17:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I search for a number in a 2d array sorted left to right and top to bottom?
19 answers
Given the following data:
[4]
[5, 8]
[9, 12, 20]
[10, 15, 23, 28]
[14, 19, 31, 36, 48]
[15, 22, 34, 41, 53, 60]
[19, 26, 42, 49, 65, 72, 88]
[20, 29, 45, 54, 70, 79, 95, 104]
[24, 33, 53, 62, 82, 91, 111, 120, 140]
[25, 36, 56, 67, 87, 98, 118, 129, 149, 160]
[29, 40, 64, 75, 99, 110, 134, 145, 169, 180, 204]
[30, 43, 67, 80, 104, 117, 141, 154, 178, 191, 215, 228]
[34, 47, 75, 88, 116, 129, 157, 170, 198, 211, 239, 252, 280]
[35, 50, 78, 93, 121, 136, 164, 179, 207, 222, 250, 265, 293, 308]
[Etc.]
What could be the best searching algorithm with the most optimal Time Complexity for finding a given number?
- The rows are sorted
- The columns are sorted
- A number may occur more than once
Extra info:
Suppose we are looking for the number 26:
Due to order, this means we can eliminate the first 3 rows and the remaining columns to the right.
Due to order, this also means we can ignore every row after row=11.
Which results to this:
[10, 15, 23]
[14, 19, 31]
[15, 22, 34]
[19, 26, 42]
[20, 29, 45]
[24, 33, 53]
[25, 36, 56]
[29, 40, 64]
My current algorithm has a time complexity of O(x log(y)) where x is the amount of columns and y is the size for the Binary Search algorithm for each column.
I'm looking for something faster because I'm dealing with huge amount of data.
Currently I'm using BST on every column, but could I use BST on rows aswell? maybe achieving a O(log(x) log(y))?
algorithm search time time-complexity
marked as duplicate by Eli, Raidri, Community♦ Jan 4 at 17:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I search for a number in a 2d array sorted left to right and top to bottom?
19 answers
Given the following data:
[4]
[5, 8]
[9, 12, 20]
[10, 15, 23, 28]
[14, 19, 31, 36, 48]
[15, 22, 34, 41, 53, 60]
[19, 26, 42, 49, 65, 72, 88]
[20, 29, 45, 54, 70, 79, 95, 104]
[24, 33, 53, 62, 82, 91, 111, 120, 140]
[25, 36, 56, 67, 87, 98, 118, 129, 149, 160]
[29, 40, 64, 75, 99, 110, 134, 145, 169, 180, 204]
[30, 43, 67, 80, 104, 117, 141, 154, 178, 191, 215, 228]
[34, 47, 75, 88, 116, 129, 157, 170, 198, 211, 239, 252, 280]
[35, 50, 78, 93, 121, 136, 164, 179, 207, 222, 250, 265, 293, 308]
[Etc.]
What could be the best searching algorithm with the most optimal Time Complexity for finding a given number?
- The rows are sorted
- The columns are sorted
- A number may occur more than once
Extra info:
Suppose we are looking for the number 26:
Due to order, this means we can eliminate the first 3 rows and the remaining columns to the right.
Due to order, this also means we can ignore every row after row=11.
Which results to this:
[10, 15, 23]
[14, 19, 31]
[15, 22, 34]
[19, 26, 42]
[20, 29, 45]
[24, 33, 53]
[25, 36, 56]
[29, 40, 64]
My current algorithm has a time complexity of O(x log(y)) where x is the amount of columns and y is the size for the Binary Search algorithm for each column.
I'm looking for something faster because I'm dealing with huge amount of data.
Currently I'm using BST on every column, but could I use BST on rows aswell? maybe achieving a O(log(x) log(y))?
algorithm search time time-complexity
This question already has an answer here:
How do I search for a number in a 2d array sorted left to right and top to bottom?
19 answers
Given the following data:
[4]
[5, 8]
[9, 12, 20]
[10, 15, 23, 28]
[14, 19, 31, 36, 48]
[15, 22, 34, 41, 53, 60]
[19, 26, 42, 49, 65, 72, 88]
[20, 29, 45, 54, 70, 79, 95, 104]
[24, 33, 53, 62, 82, 91, 111, 120, 140]
[25, 36, 56, 67, 87, 98, 118, 129, 149, 160]
[29, 40, 64, 75, 99, 110, 134, 145, 169, 180, 204]
[30, 43, 67, 80, 104, 117, 141, 154, 178, 191, 215, 228]
[34, 47, 75, 88, 116, 129, 157, 170, 198, 211, 239, 252, 280]
[35, 50, 78, 93, 121, 136, 164, 179, 207, 222, 250, 265, 293, 308]
[Etc.]
What could be the best searching algorithm with the most optimal Time Complexity for finding a given number?
- The rows are sorted
- The columns are sorted
- A number may occur more than once
Extra info:
Suppose we are looking for the number 26:
Due to order, this means we can eliminate the first 3 rows and the remaining columns to the right.
Due to order, this also means we can ignore every row after row=11.
Which results to this:
[10, 15, 23]
[14, 19, 31]
[15, 22, 34]
[19, 26, 42]
[20, 29, 45]
[24, 33, 53]
[25, 36, 56]
[29, 40, 64]
My current algorithm has a time complexity of O(x log(y)) where x is the amount of columns and y is the size for the Binary Search algorithm for each column.
I'm looking for something faster because I'm dealing with huge amount of data.
Currently I'm using BST on every column, but could I use BST on rows aswell? maybe achieving a O(log(x) log(y))?
This question already has an answer here:
How do I search for a number in a 2d array sorted left to right and top to bottom?
19 answers
algorithm search time time-complexity
algorithm search time time-complexity
edited Jan 3 at 22:01
ruakh
128k14206259
128k14206259
asked Jan 3 at 21:18
EliEli
401622
401622
marked as duplicate by Eli, Raidri, Community♦ Jan 4 at 17:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Eli, Raidri, Community♦ Jan 4 at 17:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It can be done in O(x)
Let's call the element we are trying to find n
Start with the bottom left element.
For each element we search through (let's call it e):
if e == n: we found it
if e < n: move to the right
Justification:
All elements to the left of e, including the column that e is in, are less than e. Those elements cannot == n and can be eliminated.
- if e > n: move up
Justification:
All elements below e are greater than e and can be eliminated. What about the values less than e to the left of e? Can't those be == n? No. For e to make those moves to the right and have values to it's left, those values would have been already eliminated in step 2
Repeat until n found or index out of bounds in which case such an element does not exist.
Time complexity:
The worst case scenario is if the element isn't in the array and we have an index out of bounds. This occurs at the main diagonal and the total distance to the right and total distance up to any element on the long diagonal always sums to x
.
But this is slower?
– Eli
Jan 3 at 21:52
It's not, you lose thelog y
factor, this isO(x)
. In the initial edit it wasO(N)
, that N meant the number of rows not the number of total elements.
– Primusa
Jan 3 at 21:52
I think it'sO(x + y)
. Worst case you check a value in each row and each column.
– AShelly
Jan 3 at 21:54
I'm pretty sure this is slower, I presume that your O(x) is where x = mn where m = rows and n = columns. Think about it, you start at bottom left, if the value is top right, it will take the amount of columns and the amount of rows to get there, ergo x = mn which is slower than O(m log(n)) ?
– Eli
Jan 3 at 21:55
1
@GeorgiGerganov that's true but the worst case for that is stillO(x)
, which is u gotta move up and to the right once each time
– Primusa
Jan 3 at 22:09
|
show 7 more comments
You can find the bottom left of your trimmed array with a binary search of the first column, and the top right with a binary search of the last column of each row.
From there, the problem degenerates to How do I search for a number in a 2d array sorted left to right and top to bottom? which is well-studied in the linked question. The best algorithm is dependent on the shape of the result.
+1, though I think you're actually better off skipping the "You can find the bottom left of your trimmed array with a binary search of the first column" part: finding the top right is enough to reduce this problem to the one that you link to, so you should use the strategies there instead of trying to pre-shrink the problem in a way that might turn out to not be optimal.
– ruakh
Jan 4 at 17:10
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It can be done in O(x)
Let's call the element we are trying to find n
Start with the bottom left element.
For each element we search through (let's call it e):
if e == n: we found it
if e < n: move to the right
Justification:
All elements to the left of e, including the column that e is in, are less than e. Those elements cannot == n and can be eliminated.
- if e > n: move up
Justification:
All elements below e are greater than e and can be eliminated. What about the values less than e to the left of e? Can't those be == n? No. For e to make those moves to the right and have values to it's left, those values would have been already eliminated in step 2
Repeat until n found or index out of bounds in which case such an element does not exist.
Time complexity:
The worst case scenario is if the element isn't in the array and we have an index out of bounds. This occurs at the main diagonal and the total distance to the right and total distance up to any element on the long diagonal always sums to x
.
But this is slower?
– Eli
Jan 3 at 21:52
It's not, you lose thelog y
factor, this isO(x)
. In the initial edit it wasO(N)
, that N meant the number of rows not the number of total elements.
– Primusa
Jan 3 at 21:52
I think it'sO(x + y)
. Worst case you check a value in each row and each column.
– AShelly
Jan 3 at 21:54
I'm pretty sure this is slower, I presume that your O(x) is where x = mn where m = rows and n = columns. Think about it, you start at bottom left, if the value is top right, it will take the amount of columns and the amount of rows to get there, ergo x = mn which is slower than O(m log(n)) ?
– Eli
Jan 3 at 21:55
1
@GeorgiGerganov that's true but the worst case for that is stillO(x)
, which is u gotta move up and to the right once each time
– Primusa
Jan 3 at 22:09
|
show 7 more comments
It can be done in O(x)
Let's call the element we are trying to find n
Start with the bottom left element.
For each element we search through (let's call it e):
if e == n: we found it
if e < n: move to the right
Justification:
All elements to the left of e, including the column that e is in, are less than e. Those elements cannot == n and can be eliminated.
- if e > n: move up
Justification:
All elements below e are greater than e and can be eliminated. What about the values less than e to the left of e? Can't those be == n? No. For e to make those moves to the right and have values to it's left, those values would have been already eliminated in step 2
Repeat until n found or index out of bounds in which case such an element does not exist.
Time complexity:
The worst case scenario is if the element isn't in the array and we have an index out of bounds. This occurs at the main diagonal and the total distance to the right and total distance up to any element on the long diagonal always sums to x
.
But this is slower?
– Eli
Jan 3 at 21:52
It's not, you lose thelog y
factor, this isO(x)
. In the initial edit it wasO(N)
, that N meant the number of rows not the number of total elements.
– Primusa
Jan 3 at 21:52
I think it'sO(x + y)
. Worst case you check a value in each row and each column.
– AShelly
Jan 3 at 21:54
I'm pretty sure this is slower, I presume that your O(x) is where x = mn where m = rows and n = columns. Think about it, you start at bottom left, if the value is top right, it will take the amount of columns and the amount of rows to get there, ergo x = mn which is slower than O(m log(n)) ?
– Eli
Jan 3 at 21:55
1
@GeorgiGerganov that's true but the worst case for that is stillO(x)
, which is u gotta move up and to the right once each time
– Primusa
Jan 3 at 22:09
|
show 7 more comments
It can be done in O(x)
Let's call the element we are trying to find n
Start with the bottom left element.
For each element we search through (let's call it e):
if e == n: we found it
if e < n: move to the right
Justification:
All elements to the left of e, including the column that e is in, are less than e. Those elements cannot == n and can be eliminated.
- if e > n: move up
Justification:
All elements below e are greater than e and can be eliminated. What about the values less than e to the left of e? Can't those be == n? No. For e to make those moves to the right and have values to it's left, those values would have been already eliminated in step 2
Repeat until n found or index out of bounds in which case such an element does not exist.
Time complexity:
The worst case scenario is if the element isn't in the array and we have an index out of bounds. This occurs at the main diagonal and the total distance to the right and total distance up to any element on the long diagonal always sums to x
.
It can be done in O(x)
Let's call the element we are trying to find n
Start with the bottom left element.
For each element we search through (let's call it e):
if e == n: we found it
if e < n: move to the right
Justification:
All elements to the left of e, including the column that e is in, are less than e. Those elements cannot == n and can be eliminated.
- if e > n: move up
Justification:
All elements below e are greater than e and can be eliminated. What about the values less than e to the left of e? Can't those be == n? No. For e to make those moves to the right and have values to it's left, those values would have been already eliminated in step 2
Repeat until n found or index out of bounds in which case such an element does not exist.
Time complexity:
The worst case scenario is if the element isn't in the array and we have an index out of bounds. This occurs at the main diagonal and the total distance to the right and total distance up to any element on the long diagonal always sums to x
.
edited Jan 3 at 22:16
answered Jan 3 at 21:50
PrimusaPrimusa
8,04021032
8,04021032
But this is slower?
– Eli
Jan 3 at 21:52
It's not, you lose thelog y
factor, this isO(x)
. In the initial edit it wasO(N)
, that N meant the number of rows not the number of total elements.
– Primusa
Jan 3 at 21:52
I think it'sO(x + y)
. Worst case you check a value in each row and each column.
– AShelly
Jan 3 at 21:54
I'm pretty sure this is slower, I presume that your O(x) is where x = mn where m = rows and n = columns. Think about it, you start at bottom left, if the value is top right, it will take the amount of columns and the amount of rows to get there, ergo x = mn which is slower than O(m log(n)) ?
– Eli
Jan 3 at 21:55
1
@GeorgiGerganov that's true but the worst case for that is stillO(x)
, which is u gotta move up and to the right once each time
– Primusa
Jan 3 at 22:09
|
show 7 more comments
But this is slower?
– Eli
Jan 3 at 21:52
It's not, you lose thelog y
factor, this isO(x)
. In the initial edit it wasO(N)
, that N meant the number of rows not the number of total elements.
– Primusa
Jan 3 at 21:52
I think it'sO(x + y)
. Worst case you check a value in each row and each column.
– AShelly
Jan 3 at 21:54
I'm pretty sure this is slower, I presume that your O(x) is where x = mn where m = rows and n = columns. Think about it, you start at bottom left, if the value is top right, it will take the amount of columns and the amount of rows to get there, ergo x = mn which is slower than O(m log(n)) ?
– Eli
Jan 3 at 21:55
1
@GeorgiGerganov that's true but the worst case for that is stillO(x)
, which is u gotta move up and to the right once each time
– Primusa
Jan 3 at 22:09
But this is slower?
– Eli
Jan 3 at 21:52
But this is slower?
– Eli
Jan 3 at 21:52
It's not, you lose the
log y
factor, this is O(x)
. In the initial edit it was O(N)
, that N meant the number of rows not the number of total elements.– Primusa
Jan 3 at 21:52
It's not, you lose the
log y
factor, this is O(x)
. In the initial edit it was O(N)
, that N meant the number of rows not the number of total elements.– Primusa
Jan 3 at 21:52
I think it's
O(x + y)
. Worst case you check a value in each row and each column.– AShelly
Jan 3 at 21:54
I think it's
O(x + y)
. Worst case you check a value in each row and each column.– AShelly
Jan 3 at 21:54
I'm pretty sure this is slower, I presume that your O(x) is where x = mn where m = rows and n = columns. Think about it, you start at bottom left, if the value is top right, it will take the amount of columns and the amount of rows to get there, ergo x = mn which is slower than O(m log(n)) ?
– Eli
Jan 3 at 21:55
I'm pretty sure this is slower, I presume that your O(x) is where x = mn where m = rows and n = columns. Think about it, you start at bottom left, if the value is top right, it will take the amount of columns and the amount of rows to get there, ergo x = mn which is slower than O(m log(n)) ?
– Eli
Jan 3 at 21:55
1
1
@GeorgiGerganov that's true but the worst case for that is still
O(x)
, which is u gotta move up and to the right once each time– Primusa
Jan 3 at 22:09
@GeorgiGerganov that's true but the worst case for that is still
O(x)
, which is u gotta move up and to the right once each time– Primusa
Jan 3 at 22:09
|
show 7 more comments
You can find the bottom left of your trimmed array with a binary search of the first column, and the top right with a binary search of the last column of each row.
From there, the problem degenerates to How do I search for a number in a 2d array sorted left to right and top to bottom? which is well-studied in the linked question. The best algorithm is dependent on the shape of the result.
+1, though I think you're actually better off skipping the "You can find the bottom left of your trimmed array with a binary search of the first column" part: finding the top right is enough to reduce this problem to the one that you link to, so you should use the strategies there instead of trying to pre-shrink the problem in a way that might turn out to not be optimal.
– ruakh
Jan 4 at 17:10
add a comment |
You can find the bottom left of your trimmed array with a binary search of the first column, and the top right with a binary search of the last column of each row.
From there, the problem degenerates to How do I search for a number in a 2d array sorted left to right and top to bottom? which is well-studied in the linked question. The best algorithm is dependent on the shape of the result.
+1, though I think you're actually better off skipping the "You can find the bottom left of your trimmed array with a binary search of the first column" part: finding the top right is enough to reduce this problem to the one that you link to, so you should use the strategies there instead of trying to pre-shrink the problem in a way that might turn out to not be optimal.
– ruakh
Jan 4 at 17:10
add a comment |
You can find the bottom left of your trimmed array with a binary search of the first column, and the top right with a binary search of the last column of each row.
From there, the problem degenerates to How do I search for a number in a 2d array sorted left to right and top to bottom? which is well-studied in the linked question. The best algorithm is dependent on the shape of the result.
You can find the bottom left of your trimmed array with a binary search of the first column, and the top right with a binary search of the last column of each row.
From there, the problem degenerates to How do I search for a number in a 2d array sorted left to right and top to bottom? which is well-studied in the linked question. The best algorithm is dependent on the shape of the result.
answered Jan 3 at 22:18
AShellyAShelly
26.2k1173129
26.2k1173129
+1, though I think you're actually better off skipping the "You can find the bottom left of your trimmed array with a binary search of the first column" part: finding the top right is enough to reduce this problem to the one that you link to, so you should use the strategies there instead of trying to pre-shrink the problem in a way that might turn out to not be optimal.
– ruakh
Jan 4 at 17:10
add a comment |
+1, though I think you're actually better off skipping the "You can find the bottom left of your trimmed array with a binary search of the first column" part: finding the top right is enough to reduce this problem to the one that you link to, so you should use the strategies there instead of trying to pre-shrink the problem in a way that might turn out to not be optimal.
– ruakh
Jan 4 at 17:10
+1, though I think you're actually better off skipping the "You can find the bottom left of your trimmed array with a binary search of the first column" part: finding the top right is enough to reduce this problem to the one that you link to, so you should use the strategies there instead of trying to pre-shrink the problem in a way that might turn out to not be optimal.
– ruakh
Jan 4 at 17:10
+1, though I think you're actually better off skipping the "You can find the bottom left of your trimmed array with a binary search of the first column" part: finding the top right is enough to reduce this problem to the one that you link to, so you should use the strategies there instead of trying to pre-shrink the problem in a way that might turn out to not be optimal.
– ruakh
Jan 4 at 17:10
add a comment |