Techno Blender
Digitally Yours.

Binary search in an object Array for the field of an element

0 31


What is Binary Searching?

Binary searching is a type of algorithm that can quickly search through a sorted array of elements. The algorithm works by comparing the search key to the middle element of the array. If the key is larger than the middle element, the algorithm will search the right side of the array. If the key is smaller than the middle element, the algorithm will search the left side of the array. This process is repeated until the key is found or the array is exhausted.

What is an Object Array?

An object array is an array of objects, which is a data structure consisting of a collection of related data items. Each object in the array has a specific set of properties, and each property can contain a different type of data. 
For example, an object array might contain objects representing people, where each object has a name, age, and gender property. Object arrays are useful for storing and retrieving data. They can be used to store records of data, such as customer information, or to store and retrieve complex data structures, such as trees or graphs.

Why Use Binary Searching on Object Arrays?

Object arrays are collections of objects that have an associated “key”. In other words, a key is a field or property of the object that can be used to identify the object. 
For example, if you have an array of people, you could use the person’s name as the key. When you use binary searching on object arrays, you can quickly and accurately find the object that contains the key you’re searching for.

How to Set Up the Array for Binary Searching?

Before you can perform a binary search on an object array, you must first set up the array so that it is sorted. This is done by comparing the elements in the array and arranging them in order of their field. 
For example, if you are searching for an element with a specific name, you must arrange the elements in the array in alphabetical order by name.
Once, the array is sorted, you can begin searching for the element.

Steps Involved in Performing a Binary Search

Binary searching an object array involves the following steps:

  1. Determine the field of the element you are searching for. This can be done by looking at the properties of the objects in the array.
  2. Find the midpoint of the array. This is done by taking the length of the array and dividing it by two.
  3. Compare the element at the midpoint with the field of the element you are searching for.
  4. If the element at the midpoint matches the field of the element you are searching for, you have found the element.
  5. If the element at the midpoint does not match the field of the element you are searching for, split the array into two halves and repeat the process on one of the halves.
  6. Continue this process until you find the element or until the array is empty.

Example:

const arr = [
    {name: “John”, age: 25},
    {name: “Tim”, age: 28},
    {name: “Michelle”, age: 21},
    {name: “Alice”, age: 29}
]

here we want to search age of the Alice, pseudo code for the same is explained below:

Pseudo code:

// sort array by age
arr.sort((a, b) => a.age – b.age);

// perform binary search
function binarySearch(arr, target) {

    let low = 0;
    let high =  arr.length – 1;

    while (low <= high) {
        let mid = Math.floor((low + high) / 2);
        let guess = arr[mid];
        if (guess.name === target) {

             // return age if name matches target
             return guess.age;
         }  
        else if (guess.name > target) {
            high = mid – 1;
        } 
        else{
            low = mid + 1;
        }
    }
    return -1;
}

// search for Alice

binarySearch(arr, “Alice”); // 29

Time Complexity: O(log n).
Auxiliary Space: O(1).

Conclusion:

Binary searching an object array is a powerful tool for quickly and accurately retrieving data. By using binary searching on a sorted array, you can quickly find the object that contains the key you’re searching for. Implementing binary searching with object arrays requires sorting the array first, then using the binary search algorithm to find the index of the object. Once the index is found, you can access the object from the array. With binary searching, you can save time and energy when working with large amounts of data. 


What is Binary Searching?

Binary searching is a type of algorithm that can quickly search through a sorted array of elements. The algorithm works by comparing the search key to the middle element of the array. If the key is larger than the middle element, the algorithm will search the right side of the array. If the key is smaller than the middle element, the algorithm will search the left side of the array. This process is repeated until the key is found or the array is exhausted.

What is an Object Array?

An object array is an array of objects, which is a data structure consisting of a collection of related data items. Each object in the array has a specific set of properties, and each property can contain a different type of data. 
For example, an object array might contain objects representing people, where each object has a name, age, and gender property. Object arrays are useful for storing and retrieving data. They can be used to store records of data, such as customer information, or to store and retrieve complex data structures, such as trees or graphs.

Why Use Binary Searching on Object Arrays?

Object arrays are collections of objects that have an associated “key”. In other words, a key is a field or property of the object that can be used to identify the object. 
For example, if you have an array of people, you could use the person’s name as the key. When you use binary searching on object arrays, you can quickly and accurately find the object that contains the key you’re searching for.

How to Set Up the Array for Binary Searching?

Before you can perform a binary search on an object array, you must first set up the array so that it is sorted. This is done by comparing the elements in the array and arranging them in order of their field. 
For example, if you are searching for an element with a specific name, you must arrange the elements in the array in alphabetical order by name.
Once, the array is sorted, you can begin searching for the element.

Steps Involved in Performing a Binary Search

Binary searching an object array involves the following steps:

  1. Determine the field of the element you are searching for. This can be done by looking at the properties of the objects in the array.
  2. Find the midpoint of the array. This is done by taking the length of the array and dividing it by two.
  3. Compare the element at the midpoint with the field of the element you are searching for.
  4. If the element at the midpoint matches the field of the element you are searching for, you have found the element.
  5. If the element at the midpoint does not match the field of the element you are searching for, split the array into two halves and repeat the process on one of the halves.
  6. Continue this process until you find the element or until the array is empty.

Example:

const arr = [
    {name: “John”, age: 25},
    {name: “Tim”, age: 28},
    {name: “Michelle”, age: 21},
    {name: “Alice”, age: 29}
]

here we want to search age of the Alice, pseudo code for the same is explained below:

Pseudo code:

// sort array by age
arr.sort((a, b) => a.age – b.age);

// perform binary search
function binarySearch(arr, target) {

    let low = 0;
    let high =  arr.length – 1;

    while (low <= high) {
        let mid = Math.floor((low + high) / 2);
        let guess = arr[mid];
        if (guess.name === target) {

             // return age if name matches target
             return guess.age;
         }  
        else if (guess.name > target) {
            high = mid – 1;
        } 
        else{
            low = mid + 1;
        }
    }
    return -1;
}

// search for Alice

binarySearch(arr, “Alice”); // 29

Time Complexity: O(log n).
Auxiliary Space: O(1).

Conclusion:

Binary searching an object array is a powerful tool for quickly and accurately retrieving data. By using binary searching on a sorted array, you can quickly find the object that contains the key you’re searching for. Implementing binary searching with object arrays requires sorting the array first, then using the binary search algorithm to find the index of the object. Once the index is found, you can access the object from the array. With binary searching, you can save time and energy when working with large amounts of data. 

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment