Categories
Data Structure & Algorithm

Hello there ! i hope you’re keeping yourself alive. If yes then i think today is a good day to talk about two of the important things in Data Structures and Algorithms. Arrays & Objects.

as a matter of fact you can’t work on a serious DSA Function without dealing with either of the two stated above so we are compel to look at it either i like it or yes.

Objects

don’t take my word for it without an extensive research but i think objects are just a list of Unordered Key  Value pairs. Unordered because you can store any information in an object without taking in consideration on it’s order in the collection. just don’t care and save it there, you won’t be breaking any rule at this level but don’t push it if you don’t know much about this just save it and go. so then in the process of saving the piece of information we need to identify that information with a Key which will be very important when it comes to retrieving your information you just threw in there. these important keys are what contains the the actual Values that you might be interacting with.

let’s see the representation in JavaScript of those jokes i just poured on you.


let dataStructure = {
    topicId:1,
    topic:"Big O Notation",
    descriptions: "Big O is my best friend now",
    completed:true,
    favourateNumber:[1,2,3]
}

Notice the Keys in this object are topicId, topic, descriptions, completed, favourateNumber and by now i know you have figured out their respective values so that’s what we mean by object. any collection in an unordered Key value pair records. now the point of bringing this into the picture is to test some inbuilt Methods or Functions that can be run on this object and determine their big O notation. because they are already written function into the JavaScript language for free you don’t need to crack your brain reproducing them again when in such needs.

Remember earlier we said Data Structures & Algorithms are Functions that solve a particular problem having in my how best or bad they perform ? if yes Clap for yourself but if no there you have it again.

you can copy the following into your console against the above Object to see the magics

console.log(Object.keys(dataStructure));
console.log(`----------------`);
console.log(Object.values(dataStructure));
console.log(`----------------`);
console.log(Object.entries(dataStructure));
console.log(`----------------`);
console.log(dataStructure.hasOwnProperty("topic")); 

The time complexity of these methods go this way.

Object.keys O(n)  ----> Linear time Notation
Object.values O(n)  ----> Linear time Notation
Object.entries O(n)  ----> Linear time Notation
hasOwnProperty O(1)  ----> Constant time Notation

you see the difference now with the various Algorithms it takes to get the desire results, with the exception of the last one ie hasOwnProperty  which runs at a constant time the rest will have to at least loop or iterate through the collection to get you your answer. I may not best explain this concept like I said I like baby English and keeping things simple. so kindly read more of other resources if this is not enough information for you.

On every object we will by all standard need to perform these following tasks.

Insertion  –> the Big O will always be constant O(1) because we don’t care where the records will be inserted.

Deletion  –> O(1) Because we can specify the exact key to delete directly without any jargons

Access      –> O(1) Because accessing information is straight forward

Searching –> O(n) Linear time because your function needs to loop through for the exact record or matching value

Arrays

arrays in the contrary are a collection of ordered list in a container. obviously we use arrays when we are dealing with ordered list. i use to get confused about this but not anymore. the order in array is not the actual values order but rather the positions the values are occupying are ordered with what we call an Indexes. an Index is the  representation of an element in an Array mostly the value of that particular index. so that said the indexes are ordered from 0,1,2,3,…. let’s now see the representation.

let dataStructure = [1,"O Notation","Big O is my best friend now",true,
[1,2,3]]

Notice in this case there is no Key reference to the elements in the array, they are just standing on their own but having a virtual index holding down in place  as 0,1,2,3,4.

if you whish to access any of this element just box the array with an index to see what’s inside there like this

dataStructure[1]    Doing this will retrieve the Second Item which is —>  O Notation . This kind of access do not involve any expensive computation so obviously the Big O notation of this process is O(1) constant time.

Let’s have a look at some few built in methods on these so called arrays.

similarly to objects we will need to do the following operations on Arrays

Insertion  –> Big O depends on the position where the action is to be perform ie. the end is much easier for O(1), but the Beginning and the middle will require O(n) because we need to shift the position of elements to pave way to new ones.

Deletion  –> Also dependent on the position as well but in this case the beginning and the end will be mush faster so O(1) in case of deleting in the middle will require to loop through for the exact records bringing us to O(n)

Access      –> O(1) Because accessing information is straight forward

Searching –> O(n) Linear time because your function needs to loop through for the exact record or matching value

Some Few Arrays Inbuilt Methods and their big O notations.

dataStructure.push() Inserting at the end O(1)  remember ?
dataStructure.pop() Deleting at the end O(1)  remember ?
dataStructure.splice() O(n) 
dataStructure.slice() O(n) 
dataStructure.sort() O(n) 
dataStructure.shift() O(n) 
dataStructure.unshift() O(n) 

Please take some times and read about the various methods and how they work. Here we would like to talk about the Data Structures and Algorithms on these so forgive me keeping it this simple for now. I hope we are still friends even though i didn’t explain the rest much enough.

See you in the next post my friend…

Leave a Reply

Your email address will not be published. Required fields are marked *