does it work for unsotred array?
since it's checking if
arr[middle]
is less than or bigger than the target if u given unsorted array for example
and we r looking for 3 then
it'll first land on 1, then check if 1 is smaller or bigger than 3. Since it's smaller than 3 it'll look on the right side but 3 in on the left side.
so does that mean it'll only work with sorted array or am I getting something wrong??
i do know how to sort an array btw xD26 Replies
also, a side question, if u have some array containing some objects
how does sortation work for that? based of the first char of the first key?
That sorting algo requires the array to be pre-sorted, yes. So you'd need to do that first, but that also creates a lot more things you need to do, like copying the array, sorting the copy, finding the index in the copied array, then finding that entry in the original array and returning that index
To make a copy of the array you'd need to
is it that nescessary to find the actual index from the original one? we can just use the sorted one for everything no?
Note that
array.sort()
sorts the array based on strings, not numbers (as everything can be coerced to strings) so 2 is greater than 20.hmm..
wouldn't
arr.sort() work fine??
That depends on what you're doing with the array
.sort()
mutates the array, so doing arr.sort()
will change the initial array. Depending on what the array is being used for that's probably a bad thingis dat case u can also use the
right??
oke got it
Yes, but you don't need
return
in a one-line arrow function. If there are no {}
the result is implicitly returnedaaah so u can't directly do
??
it'll still mess with the original one??
aaah okeey got it
Yes. Calling
.sort()
modifies the array it's called on. .slice()
creates a copy of the array being slicedwhat about the
Array from
Array.from()
creates an array from another iterable, so it's not doing anything to the original arrayhmm..so slice returns a new array??
ok got it
Yes, it "slices" the initial array from
start
to end
(Array.slice(start, end)
). With no params passed it slices the entire array
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sliceand another question, when u r dealing with BIG array dataset, wouldn't sorting take a lot of timd??
aaah got it.. tnx ❤️
As for this question, it sorts the objects as if they're strings. If you want it to be based on a specific value in the object you need to use that in your comparator function
got it..
and what about this?
The bigger the dataset the longer it takes to sort, yes. And depending on the sorting algo used it can take more or less time.
hmm i see.. if we need sorting to use binary search and sorting takes additional time then is that still faster than normal for loop search??
It depends 😜
If the item you're looking for is at the beginning of the array than it's easier to for-loop it
But if the item is at the end then sorting then searching is faster 😉
How you do things is very much dependent on what you're trying to accomplish. With your questions I'm not sure if you're learning sorting algos or if you think binary search is the answer to the question you haven't asked yet
Because if you have a bunch of objects then you'd most likely be better off storing them in a hashmap (
new Map()
) than an array as finding an item is as easy as map.get(key)
instead of array.find( (el) => el.key === key)
there isn't any particular thing I'm trying to accomplish ahaha
it's just questions that pop up mind that I'm asking
like when i got to know of binary search I checked what it is, then when playing around with the code I come in realization that it doesn't work for unsotred array so I asked that.. then as I was thinking about sorting it came to my mind, if numbers r sorted like this and strings like this how does it sort objects so i asked that as well ahaha
but then i asked myself, if the whole point of binary search is to find item in less time wouldn't sorting take time to sort?
it's just random things that pop up in mind.. xD
but yaa, tnx for the help.. much appreciation
❤️
If you're into sorting algos, check out mergesort:
https://en.wikipedia.org/wiki/Merge_sort
Merge sort
In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same in the input and output. Merge sort is a divide-and-conquer algorithm that was invented by John von Neu...
There are a lot of others, but mergesort is one of the best
https://en.wikipedia.org/wiki/Sorting_algorithm
okii..tnx❤️