The document discusses linear arrays and their memory representation. It defines key concepts related to linear arrays like basic terminology, size/length of an array, indexes, upper and lower bounds. It describes how elements of a linear array are stored sequentially in contiguous memory locations. It also covers traversing arrays, insertion and deletion of elements, and searching techniques like linear search and binary search.
The document describes the quicksort algorithm. Quicksort works by:
1) Partitioning the array around a pivot element into two sub-arrays of less than or equal and greater than elements.
2) Recursively sorting the two sub-arrays.
3) Combining the now sorted sub-arrays.
In the average case, quicksort runs in O(n log n) time due to balanced partitions at each recursion level. However, in the worst case of an already sorted input, it runs in O(n^2) time due to highly unbalanced partitions. A randomized version of quicksort chooses pivots randomly to avoid worst case behavior.
This document discusses stacks as a linear data structure. It defines a stack as a last-in, first-out (LIFO) collection where the last item added is the first removed. The core stack operations of push and pop are introduced, along with algorithms to insert, delete, and display items in a stack. Examples of stack applications include reversing strings, checking validity of expressions with nested parentheses, and converting infix notation to postfix.
The document discusses priority queues, which are data structures that allow elements to be inserted and removed based on priority. Elements with higher priority are served before those with lower priority. There are two main types - ascending order queues prioritize lower numbers, while descending order queues prioritize higher numbers. Priority queues can be implemented using linked lists, arrays, binary heaps, and binary search trees. Common applications include shortest path algorithms, heap sorting, and operating system processes.
The document discusses linked lists as an alternative to arrays for storing data in memory. Linked lists allow for easy insertion and removal of nodes anywhere in the list by using pointers to link nodes together rather than storing them in contiguous memory blocks. The key aspects of linked lists covered are the node structure containing data and a pointer to the next node, initializing an empty list, traversing the list, and common operations like insertion and deletion at different positions. Different types of linked lists are also introduced.
Arrays in Python can hold multiple values and each element has a numeric index. Arrays can be one-dimensional (1D), two-dimensional (2D), or multi-dimensional. Common operations on arrays include accessing elements, adding/removing elements, concatenating arrays, slicing arrays, looping through elements, and sorting arrays. The NumPy library provides powerful capabilities to work with n-dimensional arrays and matrices.
The document discusses different types of queues including their representations, operations, and applications. It describes queues as linear data structures that follow a first-in, first-out principle. Common queue operations are insertion at the rear and deletion at the front. Queues can be represented using arrays or linked lists. Circular queues and priority queues are also described as variants that address limitations of standard queues. Real-world and technical applications of queues include CPU scheduling, cashier lines, and data transfer between processes.
An ordered collection of items from which items may be deleted from one end called the front and into which items may be inserted from other end called rear is known as Queue.
It is a linear data structure.
It is called the First In First Out (FIFO) list. Since in queue, the first element will be the first element out.
The document presents an overview of selection sort, including its definition, algorithm, example, advantages, and disadvantages. Selection sort works by iteratively finding the minimum element in an unsorted sublist and exchanging it with the first element. It has a time complexity of O(n2) but performs well on small lists since it is an in-place sorting algorithm with minimal additional storage requirements. However, it is not efficient for huge datasets due to its quadratic time complexity.
The document describes the quicksort algorithm. Quicksort works by:
1) Partitioning the array around a pivot element into two sub-arrays of less than or equal and greater than elements.
2) Recursively sorting the two sub-arrays.
3) Combining the now sorted sub-arrays.
In the average case, quicksort runs in O(n log n) time due to balanced partitions at each recursion level. However, in the worst case of an already sorted input, it runs in O(n^2) time due to highly unbalanced partitions. A randomized version of quicksort chooses pivots randomly to avoid worst case behavior.
This document discusses stacks as a linear data structure. It defines a stack as a last-in, first-out (LIFO) collection where the last item added is the first removed. The core stack operations of push and pop are introduced, along with algorithms to insert, delete, and display items in a stack. Examples of stack applications include reversing strings, checking validity of expressions with nested parentheses, and converting infix notation to postfix.
The document discusses priority queues, which are data structures that allow elements to be inserted and removed based on priority. Elements with higher priority are served before those with lower priority. There are two main types - ascending order queues prioritize lower numbers, while descending order queues prioritize higher numbers. Priority queues can be implemented using linked lists, arrays, binary heaps, and binary search trees. Common applications include shortest path algorithms, heap sorting, and operating system processes.
The document discusses linked lists as an alternative to arrays for storing data in memory. Linked lists allow for easy insertion and removal of nodes anywhere in the list by using pointers to link nodes together rather than storing them in contiguous memory blocks. The key aspects of linked lists covered are the node structure containing data and a pointer to the next node, initializing an empty list, traversing the list, and common operations like insertion and deletion at different positions. Different types of linked lists are also introduced.
Arrays in Python can hold multiple values and each element has a numeric index. Arrays can be one-dimensional (1D), two-dimensional (2D), or multi-dimensional. Common operations on arrays include accessing elements, adding/removing elements, concatenating arrays, slicing arrays, looping through elements, and sorting arrays. The NumPy library provides powerful capabilities to work with n-dimensional arrays and matrices.
The document discusses different types of queues including their representations, operations, and applications. It describes queues as linear data structures that follow a first-in, first-out principle. Common queue operations are insertion at the rear and deletion at the front. Queues can be represented using arrays or linked lists. Circular queues and priority queues are also described as variants that address limitations of standard queues. Real-world and technical applications of queues include CPU scheduling, cashier lines, and data transfer between processes.
An ordered collection of items from which items may be deleted from one end called the front and into which items may be inserted from other end called rear is known as Queue.
It is a linear data structure.
It is called the First In First Out (FIFO) list. Since in queue, the first element will be the first element out.
The document presents an overview of selection sort, including its definition, algorithm, example, advantages, and disadvantages. Selection sort works by iteratively finding the minimum element in an unsorted sublist and exchanging it with the first element. It has a time complexity of O(n2) but performs well on small lists since it is an in-place sorting algorithm with minimal additional storage requirements. However, it is not efficient for huge datasets due to its quadratic time complexity.
The document discusses various applications of stacks including:
1) Reversing strings by pushing characters onto a stack and popping them off in reverse order.
2) Calculator operations using postfix notation and a stack.
3) Recursive functions using a stack to store previous function calls and variables.
4) Determining if a word is a palindrome by pushing characters onto a stack and comparing to the popped off characters.
Code examples are provided for reversing strings, recursive summation, and determining palindromes using stacks.
The document discusses queues, which implement the FIFO (first-in, first-out) policy. It describes the queue ADT interface with functions like enqueue(), dequeue(), and getFront(). It provides examples of using a queue and implementing queues using linked lists or arrays. Circular arrays are described as another implementation where the front and back pointers wrap around the end of the array. Checks for empty and full states are also covered.
Insertion sort works by iterating through an array, inserting each element into its sorted position by shifting other elements over. It finds the location where each element should be inserted into the sorted portion using a linear search, moving larger elements out of the way to make room. This sorting algorithm is most effective for small data sets and can be implemented recursively or iteratively through comparisons and shifts.
Selection sort is an in-place comparison sorting algorithm where the minimum element from the unsorted section of the list is selected in each pass and swapped with the first element. It has a time complexity of O(n2) making it inefficient for large lists. The algorithm involves dividing the list into sorted and unsorted sublists, finding the minimum element in the unsorted sublist, swapping it with the first element and moving the imaginary wall between the two sublists by one element. This process is repeated for n-1 passes to completely sort an input list of n elements. Pseudocode for the algorithm using a nested for loop to find the minimum element and swap it is also provided.
This document discusses the implementation of a single linked list data structure. It describes the nodes that make up a linked list, which have an info field to store data and a next field pointing to the next node. The document outlines different ways to represent linked lists, including static arrays and dynamic pointers. It also provides algorithms for common linked list operations like traversing, inserting, and deleting nodes from the beginning, end, or a specified position within the list.
This document describes binary search and provides an example of how it works. It begins with an introduction to binary search, noting that it can only be used on sorted lists and involves comparing the search key to the middle element. It then provides pseudocode for the binary search algorithm. The document analyzes the time complexity of binary search as O(log n) in the average and worst cases. It notes the advantages of binary search are its efficiency, while the disadvantage is that the list must be sorted. Applications mentioned include database searching and solving equations.
A list in Python is a mutable ordered sequence of elements of any data type. Lists can be created using square brackets [] and elements are accessed via indexes that start at 0. Some key characteristics of lists are:
- They can contain elements of different types
- Elements can be modified, added, or removed
- Common list methods include append(), insert(), remove(), pop(), and sort()
An array is a data structure that stores fixed number of items of the same type. It allows fast access of elements using indices. Basic array operations include traversing elements, inserting/deleting elements, searching for elements, and updating elements. Arrays are zero-indexed and elements are accessed via their index.
The document discusses arrays in data structures using C programming language. It defines what an array is and describes different types of arrays like one-dimensional, two-dimensional, and multi-dimensional arrays. It also explains array operations such as insertion, deletion, traversal, reversing, sorting, and searching. Additionally, it covers merging of arrays, arrays of pointers, and using arrays to represent polynomials.
This document presents selection sort, an in-place comparison sorting algorithm. It works by dividing the list into a sorted part on the left and unsorted part on the right. It iterates through the list, finding the smallest element in the unsorted section and swapping it into place. This process continues until the list is fully sorted. Selection sort has a time complexity of O(n^2) in all cases. While it requires no extra storage, it is inefficient for large lists compared to other algorithms.
This document discusses linear search. It contains the names of group members working on the topic. It then defines linear search as checking all elements of an array or structure sequentially until the desired result is found. It provides an example of searching for a phone number in a telephone directory. The document includes pseudocode and C++ code implementations of linear search. It discusses using linear search on a linked list. It covers the advantages of linear search running in constant time if the item is first in the list, and the disadvantages of search time increasing linearly with the number of elements.
Selection sort is a sorting algorithm that finds the smallest element in an unsorted list and swaps it with the first element, then finds the next smallest element and swaps it with the second element, continuing in this way until the list is fully sorted. It works by iterating through the list, finding the minimum element, and swapping it into its correct place at each step.
This document discusses data structures and linked lists. It provides definitions and examples of different types of linked lists, including:
- Single linked lists, which contain nodes with a data field and a link to the next node.
- Circular linked lists, where the last node links back to the first node, forming a loop.
- Doubly linked lists, where each node contains links to both the previous and next nodes.
- Operations on linked lists such as insertion, deletion, traversal, and searching are also described.
Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
A stack is a data structure where items can only be inserted and removed from one end. The last item inserted is the first item removed (LIFO). Common examples include stacks of books, plates, or bank transactions. Key stack operations are push to insert, pop to remove, and functions to check if the stack is empty or full. Stacks can be used to implement operations like reversing a string, converting infix to postfix notation, and evaluating arithmetic expressions.
The document discusses Python's four main collection data types: lists, tuples, sets, and dictionaries. It provides details on lists, including that they are ordered and changeable collections that allow duplicate members. Lists can be indexed, sliced, modified using methods like append() and insert(), and have various built-in functions that can be used on them. Examples are provided to demonstrate list indexing, slicing, changing elements, adding elements, removing elements, and built-in list methods.
The document discusses stacks in C++. It defines a stack as a data structure that follows LIFO (Last In First Out) principle where the last element added is the first to be removed. Stacks can be implemented using arrays or linked lists. The key operations on a stack are push which adds an element and pop which removes an element. Example applications of stacks include function call stacks, converting infix to postfix notation, and reversing arrays.
This document discusses insertion sort, including its mechanism, algorithm, runtime analysis, advantages, and disadvantages. Insertion sort works by iterating through an unsorted array and inserting each element into its sorted position by shifting other elements over. Its worst case runtime is O(n^2) when the array is reverse sorted, but it performs well on small, nearly sorted lists. While simple to implement, insertion sort is inefficient for large datasets compared to other algorithms.
This document provides an overview of algorithm analysis. It discusses how to analyze the time efficiency of algorithms by counting the number of operations and expressing efficiency using growth functions. Different common growth rates like constant, linear, quadratic, and exponential are introduced. Examples are provided to demonstrate how to determine the growth rate of different algorithms, including recursive algorithms, by deriving their time complexity functions. The key aspects covered are estimating algorithm runtime, comparing growth rates of algorithms, and using Big O notation to classify algorithms by their asymptotic behavior.
A one-dimensional array stores elements linearly such that they can be accessed using an index, the document provides an example of finding the address of an element in a 1D array and taking user input to store in an array and display all elements, and abstract data types provide only the interface of a data structure without implementation details.
Data Structures- Part3 arrays and searching algorithmsAbdullah Al-hazmy
This document discusses arrays and searching algorithms. It begins by defining data structures and describing different types, including arrays. Arrays are introduced as a way to store multiple values of the same type in contiguous memory locations. Sequential and binary search algorithms are then described. Sequential search has linear time complexity, while binary search has logarithmic time complexity when used on a sorted array. Key concepts of arrays like indexing, dimensionality, and operations are also covered. The document concludes by looking ahead to sorting algorithms to be discussed in the next week.
The document discusses various applications of stacks including:
1) Reversing strings by pushing characters onto a stack and popping them off in reverse order.
2) Calculator operations using postfix notation and a stack.
3) Recursive functions using a stack to store previous function calls and variables.
4) Determining if a word is a palindrome by pushing characters onto a stack and comparing to the popped off characters.
Code examples are provided for reversing strings, recursive summation, and determining palindromes using stacks.
The document discusses queues, which implement the FIFO (first-in, first-out) policy. It describes the queue ADT interface with functions like enqueue(), dequeue(), and getFront(). It provides examples of using a queue and implementing queues using linked lists or arrays. Circular arrays are described as another implementation where the front and back pointers wrap around the end of the array. Checks for empty and full states are also covered.
Insertion sort works by iterating through an array, inserting each element into its sorted position by shifting other elements over. It finds the location where each element should be inserted into the sorted portion using a linear search, moving larger elements out of the way to make room. This sorting algorithm is most effective for small data sets and can be implemented recursively or iteratively through comparisons and shifts.
Selection sort is an in-place comparison sorting algorithm where the minimum element from the unsorted section of the list is selected in each pass and swapped with the first element. It has a time complexity of O(n2) making it inefficient for large lists. The algorithm involves dividing the list into sorted and unsorted sublists, finding the minimum element in the unsorted sublist, swapping it with the first element and moving the imaginary wall between the two sublists by one element. This process is repeated for n-1 passes to completely sort an input list of n elements. Pseudocode for the algorithm using a nested for loop to find the minimum element and swap it is also provided.
This document discusses the implementation of a single linked list data structure. It describes the nodes that make up a linked list, which have an info field to store data and a next field pointing to the next node. The document outlines different ways to represent linked lists, including static arrays and dynamic pointers. It also provides algorithms for common linked list operations like traversing, inserting, and deleting nodes from the beginning, end, or a specified position within the list.
This document describes binary search and provides an example of how it works. It begins with an introduction to binary search, noting that it can only be used on sorted lists and involves comparing the search key to the middle element. It then provides pseudocode for the binary search algorithm. The document analyzes the time complexity of binary search as O(log n) in the average and worst cases. It notes the advantages of binary search are its efficiency, while the disadvantage is that the list must be sorted. Applications mentioned include database searching and solving equations.
A list in Python is a mutable ordered sequence of elements of any data type. Lists can be created using square brackets [] and elements are accessed via indexes that start at 0. Some key characteristics of lists are:
- They can contain elements of different types
- Elements can be modified, added, or removed
- Common list methods include append(), insert(), remove(), pop(), and sort()
An array is a data structure that stores fixed number of items of the same type. It allows fast access of elements using indices. Basic array operations include traversing elements, inserting/deleting elements, searching for elements, and updating elements. Arrays are zero-indexed and elements are accessed via their index.
The document discusses arrays in data structures using C programming language. It defines what an array is and describes different types of arrays like one-dimensional, two-dimensional, and multi-dimensional arrays. It also explains array operations such as insertion, deletion, traversal, reversing, sorting, and searching. Additionally, it covers merging of arrays, arrays of pointers, and using arrays to represent polynomials.
This document presents selection sort, an in-place comparison sorting algorithm. It works by dividing the list into a sorted part on the left and unsorted part on the right. It iterates through the list, finding the smallest element in the unsorted section and swapping it into place. This process continues until the list is fully sorted. Selection sort has a time complexity of O(n^2) in all cases. While it requires no extra storage, it is inefficient for large lists compared to other algorithms.
This document discusses linear search. It contains the names of group members working on the topic. It then defines linear search as checking all elements of an array or structure sequentially until the desired result is found. It provides an example of searching for a phone number in a telephone directory. The document includes pseudocode and C++ code implementations of linear search. It discusses using linear search on a linked list. It covers the advantages of linear search running in constant time if the item is first in the list, and the disadvantages of search time increasing linearly with the number of elements.
Selection sort is a sorting algorithm that finds the smallest element in an unsorted list and swaps it with the first element, then finds the next smallest element and swaps it with the second element, continuing in this way until the list is fully sorted. It works by iterating through the list, finding the minimum element, and swapping it into its correct place at each step.
This document discusses data structures and linked lists. It provides definitions and examples of different types of linked lists, including:
- Single linked lists, which contain nodes with a data field and a link to the next node.
- Circular linked lists, where the last node links back to the first node, forming a loop.
- Doubly linked lists, where each node contains links to both the previous and next nodes.
- Operations on linked lists such as insertion, deletion, traversal, and searching are also described.
Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
A stack is a data structure where items can only be inserted and removed from one end. The last item inserted is the first item removed (LIFO). Common examples include stacks of books, plates, or bank transactions. Key stack operations are push to insert, pop to remove, and functions to check if the stack is empty or full. Stacks can be used to implement operations like reversing a string, converting infix to postfix notation, and evaluating arithmetic expressions.
The document discusses Python's four main collection data types: lists, tuples, sets, and dictionaries. It provides details on lists, including that they are ordered and changeable collections that allow duplicate members. Lists can be indexed, sliced, modified using methods like append() and insert(), and have various built-in functions that can be used on them. Examples are provided to demonstrate list indexing, slicing, changing elements, adding elements, removing elements, and built-in list methods.
The document discusses stacks in C++. It defines a stack as a data structure that follows LIFO (Last In First Out) principle where the last element added is the first to be removed. Stacks can be implemented using arrays or linked lists. The key operations on a stack are push which adds an element and pop which removes an element. Example applications of stacks include function call stacks, converting infix to postfix notation, and reversing arrays.
This document discusses insertion sort, including its mechanism, algorithm, runtime analysis, advantages, and disadvantages. Insertion sort works by iterating through an unsorted array and inserting each element into its sorted position by shifting other elements over. Its worst case runtime is O(n^2) when the array is reverse sorted, but it performs well on small, nearly sorted lists. While simple to implement, insertion sort is inefficient for large datasets compared to other algorithms.
This document provides an overview of algorithm analysis. It discusses how to analyze the time efficiency of algorithms by counting the number of operations and expressing efficiency using growth functions. Different common growth rates like constant, linear, quadratic, and exponential are introduced. Examples are provided to demonstrate how to determine the growth rate of different algorithms, including recursive algorithms, by deriving their time complexity functions. The key aspects covered are estimating algorithm runtime, comparing growth rates of algorithms, and using Big O notation to classify algorithms by their asymptotic behavior.
A one-dimensional array stores elements linearly such that they can be accessed using an index, the document provides an example of finding the address of an element in a 1D array and taking user input to store in an array and display all elements, and abstract data types provide only the interface of a data structure without implementation details.
Data Structures- Part3 arrays and searching algorithmsAbdullah Al-hazmy
This document discusses arrays and searching algorithms. It begins by defining data structures and describing different types, including arrays. Arrays are introduced as a way to store multiple values of the same type in contiguous memory locations. Sequential and binary search algorithms are then described. Sequential search has linear time complexity, while binary search has logarithmic time complexity when used on a sorted array. Key concepts of arrays like indexing, dimensionality, and operations are also covered. The document concludes by looking ahead to sorting algorithms to be discussed in the next week.
The document discusses three quadratic sorting algorithms: selection sort, insertion sort, and bubble sort. It provides pseudocode for selection sort and insertion sort, and describes their operation through examples. Both selection sort and insertion sort have a worst-case and average-case runtime of O(n^2) where n is the number of elements to sort.
This document discusses various sorting and searching algorithms. It begins by listing sorting algorithms like selection sort, insertion sort, bubble sort, merge sort, and radix sort. It then discusses searching algorithms like linear/sequential search and binary search. It provides details on the implementation and time complexity of linear search, binary search, bubble sort, insertion sort, selection sort, and merge sort. Key points covered include the divide and conquer approach of merge sort and how binary search recursively halves the search space.
The document discusses two searching algorithms - linear search and binary search. Linear search sequentially compares the target element to each element in the array, while binary search uses a divide and conquer approach to quickly hone in on the target element in a sorted array. Both algorithms are demonstrated with pseudocode and examples.
An array is a collection of variables of the same type that are referenced using a common name and contiguous memory locations. One-dimensional arrays allow storing multiple variables of the same type under a single variable name. Linear/sequential search compares each element to the search key while binary search divides the array in half at each step to find the search key faster than linear search.
This document provides an introduction to data structures presented by Prof. K. Adisesha. It defines data structures as representations of logical relationships between data elements that consider both the elements and their relationships. Data structures affect both structural and functional aspects of programs. They are classified as primitive or non-primitive, with primitive structures operated on directly by machine instructions and non-primitive structures derived from primitive ones. Linear data structures like stacks and queues have elements in sequence, while non-linear structures like trees and graphs have hierarchical or parent-child relationships. Common operations on data structures include traversal, insertion, selection, searching, sorting, merging, and deletion. Arrays are also discussed in detail as a fundamental data structure.
This chapter discusses searching and sorting algorithms for arrays and strings. It covers sequential search, binary search, bubble sort, selection sort, and insertion sort. It also discusses using the Vector class to implement dynamic arrays and provides examples of searching voter data and converting strings to pig latin.
This document discusses sequential and interval searching techniques in data structures. It provides examples of linear search and binary search algorithms. Linear search sequentially checks each element to find a match, having a worst-case complexity of O(n). Binary search repeatedly targets the midpoint of a sorted array, dividing the search space in half, with a worst-case complexity of O(log n). Selection sort is also discussed as an example of an iterative sorting algorithm, which finds the minimum element and places it in the first position in each pass.
The document discusses various sorting and searching algorithms. It begins by introducing selection sort, insertion sort, and bubble sort. It then covers merge sort and explains how it works by dividing the list, sorting sublists recursively, and merging the results. Finally, it discusses linear/sequential search and binary search, noting that sequential search checks every element while binary search repeatedly halves the search space.
The document discusses various searching techniques used in computer science. It describes linear search, binary search, jump search, interpolation search, and Fibonacci search. For each search method, it provides details on the algorithm, time complexity, and examples. It also presents problems to solve using jump search and interpolation search and concludes with questions about the different search techniques.
This chapter discusses searching and sorting algorithms. It covers sequential search, binary search, selection sort and insertion sort. Sequential search has linear time complexity while binary search has logarithmic time complexity, making it more efficient for large lists. Selection sort works by finding the minimum element and swapping it into place, taking on average n(n-1)/2 comparisons to sort a list of length n. Insertion sort iterates through the list and inserts each element into its sorted position, taking on average (n^2 + 3n - 4)/4 comparisons.
Arrays allow storing and manipulating a collection of related data elements. They can hold groups of integers, characters, or other data types. Declaring individual variables becomes difficult when storing many elements, so arrays provide an efficient solution. Arrays are declared with a datatype and size, and elements can be initialized, accessed, inserted, deleted, and manipulated using their index positions. Common array operations include displaying values, finding maximum/minimum values, calculating sums, and passing arrays to functions. Multi-dimensional arrays extend the concept to store elements in a table structure accessed by row and column indexes.
This document discusses various searching methods for arrays, including linear search, binary search, and table search. Linear search sequentially checks each element until the target is found. Binary search works on sorted data by eliminating half the remaining elements at each step. Table search searches arrays of strings by first comparing individual string characters, then using binary search on the sorted string table. The document provides pseudocode algorithms and examples for each search method.
The document provides an introduction to data structures. It defines data structures as representations of logical relationships between data elements that consider both the elements and their relationships. It classifies data structures as either primitive or non-primitive. Primitive structures are directly operated on by machine instructions while non-primitive structures are built from primitive ones. Common non-primitive structures include stacks, queues, linked lists, trees and graphs. The document then discusses arrays as a data structure and operations on arrays like traversal, insertion, deletion, searching and sorting.
This document provides an introduction and overview of arrays in C++. It defines what an array is, how to declare and initialize arrays, and how to access, insert, search, sort, and merge array elements. Key points covered include:
- An array is a sequenced collection of elements of the same data type. Elements are referenced using a subscript index.
- Arrays can be declared with a fixed or variable length. Elements are initialized sequentially in memory.
- Common array operations like accessing, inserting, searching, sorting and merging are demonstrated using for loops and examples. Searching techniques include sequential and binary search. Sorting techniques include selection, bubble and insertion sort.
- Arrays are passed
The document discusses arrays and their representation in memory. It contains 3 main points:
1) It introduces linear arrays and how they are represented in memory with sequential and contiguous locations. It also discusses multidimensional arrays.
2) It provides algorithms for common linear array operations like traversing, inserting, deleting and searching using binary search. It explains how each algorithm works through examples.
3) It discusses how 2D arrays are represented in memory and visualized, using an example of storing student exam marks in a 2D array.
Impartiality as per ISO /IEC 17025:2017 StandardMuhammadJazib15
This document provides basic guidelines for imparitallity requirement of ISO 17025. It defines in detial how it is met and wiudhwdih jdhsjdhwudjwkdbjwkdddddddddddkkkkkkkkkkkkkkkkkkkkkkkwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwioiiiiiiiiiiiii uwwwwwwwwwwwwwwwwhe wiqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq gbbbbbbbbbbbbb owdjjjjjjjjjjjjjjjjjjjj widhi owqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq uwdhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhwqiiiiiiiiiiiiiiiiiiiiiiiiiiiiw0pooooojjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj whhhhhhhhhhh wheeeeeeee wihieiiiiii wihe
e qqqqqqqqqqeuwiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiqw dddddddddd cccccccccccccccv s w c r
cdf cb bicbsad ishd d qwkbdwiur e wetwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww w
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffw
uuuuhhhhhhhhhhhhhhhhhhhhhhhhe qiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc ccccccccccccccccccccccccccccccccccc bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbu uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum
m
m mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm m i
g i dijsd sjdnsjd ndjajsdnnsa adjdnawddddddddddddd uw
Determination of Equivalent Circuit parameters and performance characteristic...pvpriya2
Includes the testing of induction motor to draw the circle diagram of induction motor with step wise procedure and calculation for the same. Also explains the working and application of Induction generator
Open Channel Flow: fluid flow with a free surfaceIndrajeet sahu
Open Channel Flow: This topic focuses on fluid flow with a free surface, such as in rivers, canals, and drainage ditches. Key concepts include the classification of flow types (steady vs. unsteady, uniform vs. non-uniform), hydraulic radius, flow resistance, Manning's equation, critical flow conditions, and energy and momentum principles. It also covers flow measurement techniques, gradually varied flow analysis, and the design of open channels. Understanding these principles is vital for effective water resource management and engineering applications.
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELijaia
As digital technology becomes more deeply embedded in power systems, protecting the communication
networks of Smart Grids (SG) has emerged as a critical concern. Distributed Network Protocol 3 (DNP3)
represents a multi-tiered application layer protocol extensively utilized in Supervisory Control and Data
Acquisition (SCADA)-based smart grids to facilitate real-time data gathering and control functionalities.
Robust Intrusion Detection Systems (IDS) are necessary for early threat detection and mitigation because
of the interconnection of these networks, which makes them vulnerable to a variety of cyberattacks. To
solve this issue, this paper develops a hybrid Deep Learning (DL) model specifically designed for intrusion
detection in smart grids. The proposed approach is a combination of the Convolutional Neural Network
(CNN) and the Long-Short-Term Memory algorithms (LSTM). We employed a recent intrusion detection
dataset (DNP3), which focuses on unauthorized commands and Denial of Service (DoS) cyberattacks, to
train and test our model. The results of our experiments show that our CNN-LSTM method is much better
at finding smart grid intrusions than other deep learning algorithms used for classification. In addition,
our proposed approach improves accuracy, precision, recall, and F1 score, achieving a high detection
accuracy rate of 99.50%.
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Transcat
Join us for this solutions-based webinar on the tools and techniques for commissioning and maintaining PV Systems. In this session, we'll review the process of building and maintaining a solar array, starting with installation and commissioning, then reviewing operations and maintenance of the system. This course will review insulation resistance testing, I-V curve testing, earth-bond continuity, ground resistance testing, performance tests, visual inspections, ground and arc fault testing procedures, and power quality analysis.
Fluke Solar Application Specialist Will White is presenting on this engaging topic:
Will has worked in the renewable energy industry since 2005, first as an installer for a small east coast solar integrator before adding sales, design, and project management to his skillset. In 2022, Will joined Fluke as a solar application specialist, where he supports their renewable energy testing equipment like IV-curve tracers, electrical meters, and thermal imaging cameras. Experienced in wind power, solar thermal, energy storage, and all scales of PV, Will has primarily focused on residential and small commercial systems. He is passionate about implementing high-quality, code-compliant installation techniques.
Road construction is not as easy as it seems to be, it includes various steps and it starts with its designing and
structure including the traffic volume consideration. Then base layer is done by bulldozers and levelers and after
base surface coating has to be done. For giving road a smooth surface with flexibility, Asphalt concrete is used.
Asphalt requires an aggregate sub base material layer, and then a base layer to be put into first place. Asphalt road
construction is formulated to support the heavy traffic load and climatic conditions. It is 100% recyclable and
saving non renewable natural resources.
With the advancement of technology, Asphalt technology gives assurance about the good drainage system and with
skid resistance it can be used where safety is necessary such as outsidethe schools.
The largest use of Asphalt is for making asphalt concrete for road surfaces. It is widely used in airports around the
world due to the sturdiness and ability to be repaired quickly, it is widely used for runways dedicated to aircraft
landing and taking off. Asphalt is normally stored and transported at 150’C or 300’F temperature
Build the Next Generation of Apps with the Einstein 1 Platform.
Rejoignez Philippe Ozil pour une session de workshops qui vous guidera à travers les détails de la plateforme Einstein 1, l'importance des données pour la création d'applications d'intelligence artificielle et les différents outils et technologies que Salesforce propose pour vous apporter tous les bénéfices de l'IA.
5G Radio Network Througput Problem Analysis HCIA.pdf
03 Linear Arrays Memory Representations .pdf
1. Data Structures & Algorithms
By
Dr. Prakash Singh Tanwar
Associate Professor
Lovely Professional University, Punjab
Topic: Linear Arrays Memory
Representations
2. Linear Arrays Memory
Representation
Basic Terminology
Linear Array
Memory Representation of Linear Array
Traversing Array
Insertion and Deletion in Array
Sorting (Bubble Sort)
Searching (Linear Search and Binary Search)
Review Questions
By:Dr. Prakash Singh Tanwar
3. Basic Terminology
Linear Data Structures:
A data structure is said to be linear if its elements form a
sequence or a linear list.
Linear Array:
It is a list of a finite number n of homogeneous data
elements (same type of data elements) such that:
• (a) the elements of the array are referenced by an index set
consisting of n consecutive numbers.
• (b) the elements of the array are stored respectively in successive
memory locations
By:Dr. Prakash Singh Tanwar
4. Revision
Definition
Array is a group of variables of same data type
having a common name.
Elements of an array can be accessed through
its subscript(0 to n-1) where n is the size of
array.
Continous memory allocation
By:Dr. Prakash Singh
Tanwar
5. Revision: 1D Array
A single- dimensional or 1-D array consists of
a fixed number of elements of the same data
type organized as a single linear sequence.
Array declaration
Syntax
Ex
<data_type> <array_name>[<size>];
int a[5];
char name[20];
float num[4];
By:Dr. Prakash Singh Tanwar
7. 1D Array Memory
Representation
Array declaration
a[0] a[1] a[2] a[3] a[4]
20
int a[5];
a[0]=9;
a[1]=6;
a[2]=7;
a[3]=5;
a[4]=20;
9 6 7 5
1001 1003 1005 1007 1009
size of int = 2 bytes
&a[0]= 1001
&a[1]= 1001+2=1003
&a[2]= 1003+2=1005
&a[3]= 1005+2=1007
&a[4]= 1007+2=1009
size of int = 2 bytes
&a[0]= 1001
&a[1]= 1001+2=1003
&a[2]= 1003+2=1005
&a[3]= 1005+2=1007
&a[4]= 1007+2=1009
Note: size of int in 32 bit compiler =2 bytes
Note: size of int in 32 bit compiler =2 bytes
By:Dr. Prakash Singh Tanwar
8. 1D Array Memory
Representation
Array declaration
a[0] a[1] a[2] a[3] a[4]
20
int a[5];
a[0]=9;
a[1]=6;
a[2]=7;
a[3]=5;
a[4]=20;
9 6 7 5
1001 1005 1009 1013 1017
size of int = 4 bytes
&a[0]= 1001
&a[1]= 1001+4=1005
&a[2]= 1005+4=1009
&a[3]= 1009+4=1013
&a[4]= 1013+4=1017
size of int = 4 bytes
&a[0]= 1001
&a[1]= 1001+4=1005
&a[2]= 1005+4=1009
&a[3]= 1009+4=1013
&a[4]= 1013+4=1017
Note: size of int in 64 bit compiler =4 bytes
Note: size of int in 64 bit compiler =4 bytes
By:Dr. Prakash Singh Tanwar
9. Q/A
Are these valid statements?
int a[10];
a[10]=10;
a[10]=10; is not valid
Because, in array a[10] we have only 10 elements
from a[0] to a[9]
By:Dr. Prakash Singh Tanwar
11. Basic Terminology
Size / Length of Array
Index of Array
Upper bound and Lower bound of Array
By:Dr. Prakash Singh Tanwar
a[0] a[1] a[2] a[3] a[4]
Size / Length of Array=5
Index of array : 0 to 4
Lower Bound Upper Bound
12. Traversing Linear Array
Suppose we have to count the number of element in an
array or print all the elements of array.
By:Dr. Prakash Singh Tanwar
Algorithm 1: (Using While Loop)
1. [Initialize Counter.] Set K = LB.
2. Repeat Step 3 and 4 while K<= UB.
3. [Visit Element.] Apply PROCESS to A[K].
4. [Increase Counter.] Set K = K+1.
[End of Step 2 Loop.]
5. Exit.
a[0] a[1] a[2] a[3] a[4]
20
9 6 7 5
K
LB UB
13. Traversing Linear Array
Algorithm 2: (Using for loop)
By:Dr. Prakash Singh Tanwar
1. Repeat for K = LB to UB
Apply PROCESS to A[K].
[ End of Loop.]
2. Exit.
a[0] a[1] a[2] a[3] a[4]
20
9 6 7 5
K
LB UB
14. Q/A
Question.1: Find the Number of elements in an array
which are greater than 25
Question 2: Find out the sum of all the two digit
numbers in an array.
By:Dr. Prakash Singh Tanwar
a[0] a[1] a[2] a[3] a[4]
20
25 6 27 5
K
LB UB
15. Insertion in an Array
Two types of insertion are
possible:
Insertion at the end of array
Insertion in the middle of the array
By:Dr. Prakash Singh Tanwar
16. Insert an element in an
Array
Write a program to insert an element in
an array?
Where to insert?
What are the current elements in the array?
What value you want to insert?
How many elements are there in this array?
a[0] a[1] a[2] a[3] a[4]
10 20 30
By:Dr. Prakash Singh Tanwar
17. Insertion in an Array
Algorithm: (Insertion of an element ITEM into Kth position in a Linear
Array A with N elements)
By:Dr. Prakash Singh Tanwar
Input:
A: Array A ; N: Array A have N elements in it
ITEM :item to insert ; Pos: insert item at position Pos
Output:
Array A with item inserted at position index ‘Pos’
Algorithm
1. [Initialize Counter.] Set I = N.
2. Repeat Steps 3 and 4 while I >= Pos.
3. [Move Ith element downward] Set A[I+1] = A[I].
4. [Decrease Counter.] Set I = I-1.
[End of Step 2 loop]
5. [Insert element.] Set A[Pos] = ITEM.
6. [Reset N] N = N+1.
7. Exit
18. Insert an element in an
Array
Insert 15 at pos index=1, Size=3
a[0] a[1] a[2] a[3] a[4]
10 20 30
a[0] a[1] a[2] a[3] a[4]
10 20 30 30
a[0] a[1] a[2] a[3] a[4]
10 20 20 30
a[0] a[1] a[2] a[3] a[4]
10 15 20 30
A[I+1]=A[I]
A[I+1]=A[I]
Algorithm(for index starting from 1)
1. [Initialize Counter.] Set I = N.
2. Repeat Steps 3 and 4 while I >= Pos.
3. [Move Ith element downward] Set A[I+1] = A[I].
4. [Decrease Counter.] Set I = I-1.
[End of Step 2 loop]
5. [Insert element.] Set A[Pos] = ITEM.
6. [Reset N] N = N+1.
7. Exit
I
A[Pos]=ITEM
Algorithm (for index starting from 0 and pos is also in the
form of index)
1. [Initialize Counter.] Set I = N-1.
2. Repeat Steps 3 and 4 while I >=Pos.
3. [Move Ith element downward] Set A[I+1] = A[I].
4. [Decrease Counter.] Set I = I-1.
[End of Step 2 loop]
5. [Insert element.] Set A[Pos] = ITEM.
6. [Reset N] N = N+1.
7. Exit
By:Dr. Prakash Singh Tanwar
19. Insert an element in an
Array
int insert(int a[], int n, int key, int pos)
{
int i;
//shift the elements to right
for(i=n-1;i>=pos; i--)
{
a[i+1]=a[i];
}
//insert the element
a[pos]=key;
//increase the size
n++;
return n;
}
//display array
printf("n After insertion in arrayn");
for(i=0;i<size;i++)
{
printf("%dt",a[i]);
}
By:Dr. Prakash Singh Tanwar
cout<<"n After insertion in array“<<endl;
cout<<a[i]<<“t”;
int main(void)
{
int arr[6] = { 10,20,30 };
int n = 3;
int key=15;
int pos=1;
//insert element key at given pos
n=insert(arr,n,key,pos);
//display array
display(arr,n);
return 0;
}
20. Linear Search
Write a program to search an element in
an array?
What are the current elements in the array?
What value you want to search?
By:Dr. Prakash Singh
Tanwar
a[0] a[1] a[2] a[3] a[4]
10 8 30 5 60
21. Linear Search
(un-ordered data)
Input:
Array a[] with size n
Key to search
Output :
pos index of key element in the array. If not found then return -1
Algo:
Start searching the element from the left most position to the end of an array.
• If key element is found the return the position index
• Otherwise repeat the step 1 for next position
If it is not found in all position index of array then
• return not found
By:Dr. Prakash Singh
Tanwar
22. Linear Search
(un-ordered data)
Search key=15
By:Dr. Prakash Singh
Tanwar
a[0] a[1] a[2] a[3] a[4]
10 8 30 5 60
i=0
i=0
a[i]==key
Not
matched
i=1
i=1
Not
matched
i=2
i=2
Not
matched
i=3
i=3
Not
matched
i=4
i=4
Not
matched
i=5 (out)
i=5 (out)
Key=15
23. Linear Search
(un-ordered data)
Search key= 30
a[0] a[1] a[2] a[3] a[4]
10 8 30 5 60
i=0
i=0
a[i]==key
a[i]==30
Not
matched
i=1
i=1
Not
matched
i=2
i=2
Matched
30 is found at pos i=2
Key=30
By:Dr. Prakash Singh Tanwar
24. Linear Search
(un-ordered data)
// linear search for unordered data
int linearSearch(int a[], int size, int key)
{
int i;
//check each element to search the key
for(i=0; i < size ; i++)
{
//if key is found in array
if( a[i] == key )
{
return i; // then return the pos i
}
}
//if not found then return -1
return -1;
}
By:Dr. Prakash Singh Tanwar
25. Linear Search
(Sorted Array)
// linear search for ordered data
int linearSearch(int a[], int size, int key)
{
int i;
//check each element to search the key
for(i=0; i<size && a[i]<=key ;i++)
{
//if key is found in array
if( a[i] == key )
{
return i;// then return the pos i
}
}
//if not found then return -1
return -1;
}
By:Dr. Prakash Singh Tanwar
26. Linear Search
(Sorted Array)
Linear Search (sorted array)
a[0] a[1] a[2] a[3] a[4]
10 20 30 40 50
i=0
i=0 i=1
i=1
key= 30
Key not
matched
30<10
=false
Key not
matched
30<20
=false
10==30 20==30
i=2
i=2
Key
matched
30==30
Try next
element
Try next
element
30 found at
pos i=2
By:Dr. Prakash Singh Tanwar
27. Linear Search
(Sorted Array)
Linear Search (sorted array)
a[0] a[1] a[2] a[3] a[4]
10 20 30 40 50
i=0
i=0 i=1
i=1
key= 25
Key not
matched
25<10
=false
Key not
matched
25<20
=false
10==25 20==25
i=2
i=2
Key not
matched
30==25
Try next
element
Try next
element 25 not found in
sorted array
25<30
=true
Come
out from
loop
Where the control will move
from this break statement?
By:Dr. Prakash Singh Tanwar
28. Q/A
Where the control will move from this break statement?
A. Out side of loop
B. come out from if
C. Come out from else if
D. move to increment section
By:Dr. Prakash Singh Tanwar
29. Linear Search
(Sorted Array)
// linear search for ordered data
int linearSearch(int a[], int size, int key)
{
int i;
//check each element to search the key
for(i=0; i<size && a[i]<=key ;i++)
{
//if key is found in array
if( a[i] == key )
{
return i;// then return the pos i
}
}
//if not found then return -1
return -1;
}
Both program works same
Both program works same
Opposite
condition
By:Dr. Prakash Singh Tanwar
30. Q/A
It can search sorted and unsorted elements both
best case : key is found at the first element (index=0):
Ω (1)
worst case: key element is found at last position n
(index=n-1) : O(n)
By:Dr. Prakash Singh Tanwar
31. Q/A
What is the best case search order for
linear search?
A. Ω(1)
B. O(n)
C.O(n2)
D. Ω(n)
By:Dr. Prakash Singh Tanwar
32. Q/A
What is the best case search order for
linear search?
A. Ω(1)
B. O(n)
C.O(n2)
D. Ω(n)
By:Dr. Prakash Singh Tanwar
33. Q/A
What is the worst case search order for
linear search?
A. Ω(1)
B. O(n)
C.O(n2)
D. Ω(n)
By:Dr. Prakash Singh Tanwar
34. Q/A
What is the worst case search order for
linear search?
A. O(1)
B. O(n)
C.O(n2)
D. Ω(n)
By:Dr. Prakash Singh Tanwar
35. Q/A
Which of the program is correct for linear search algorithm?
A B C D
int index = -1;
int i = 0;
while(size > 0)
{
if(a[i] == key)
{
index = i;
}
if(a[i] > key))
{
index = i;
break;
}
i++;
}
return index;
int index = -1;
int i = 0;
while(size > 0)
{
if(a[i] == key)
{
index = i;
break;
}
if(a[i] > key))
{
break;
}
i++;
}
return index;
int index = -1;
int i = 0;
while(size > 0)
{
if(a[i] == key)
{
index=i;
break;
}
if(a[i] > key))
{
index = i;
}
i++;
}
return index;
int index = -1;
int i = 0;
while(size > 0)
{
if(a[i] == key)
{
break;
}
if(a[i] > key))
{
break;
index = i;
}
i++;
}
return index;
By:Dr. Prakash Singh Tanwar
36. Q/A
Write a program to delete an element
from an array?
What are the current elements in the array?
What you wants to delete?
How many elements are there in this array?
a[0] a[1] a[2] a[3] a[4]
10 20 30
By:Dr. Prakash Singh Tanwar
37. Delete Element of an
array
Delete 10 , Size=3
Search 10 in this array using search algo.
a[0] a[1] a[2] a[3] a[4]
10 20 30
a[0] a[1] a[2] a[3] a[4]
20 20 30
a[0] a[1] a[2] a[3] a[4]
20 30 30
a[0] a[1] a[2] a[3] a[4]
20 30
Size--
a[0]=a[1]
a[1]=a[2]
posToDelete=0
By:Dr. Prakash Singh Tanwar
38. Delete Element of an
array
By:Dr. Prakash Singh Tanwar
Input: Pointer to an array a[], size of array, Key to delete
Output: array with deleted item and
size of array
Step1: posToDelete =search the element in the array and get the position
to delete
Step 2: if key not found
display message
Step 3: Otherwise (if key found in array)
replace the elements from posToDelete to the size of array
Step 3(a): repeat the step from last element to the posToDelete
Shift elements to the left hand side a[i]=a[i+1];
Step 3(b): decrease the size by one
size--;
Step 4 return the size
39. Delete Element of an
array
By:Dr. Prakash Singh Tanwar
cout<<“Element ” <<key<<“ not fount in array”<<endl;
cout<<“Before deletion of an array”<<endl;
cout<<“Which element you want to remove?”<<endl;
cin>> key;
cout<<“After deletion from array”<<endl;
40. Binary Search
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50 60 70 80 90 100
mid=(9+0)/2=4 Mid=4
Mid=4
Key=60
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50 60 70 80 90 100
Mid=(5+9)/2=7
Mid+1
Mid-1
Mid-1
Mid=(5+6)/2=5
Left=0
Left=0 Right=9
Right=9
key not found
key not found
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50 60 70 80 90 100
Mid=7
Mid=7 Mid+1
Mid+1
Mid-1
Mid-1
Left
=mid+1
Right
Right
key not found
key not found
Mid=5
Mid=5
key found at
pos 5
key found at
pos 5
Right
=mid-1
By:Dr. Prakash Singh Tanwar
41. Binary Search
Algorithm
Step 1: calculate the mid position
Step 2: If the key to search is found at mid then return the mid pos
Step 3: If key < a[mid] value, then it is in left side.
so, recursively find it in left side from left to mid - 1
Step 4: otherwise , then it is in right side.
so, recursively find it in right side i.e. from mid + 1 to right
Step 5: if it is not found in left and right then return -1 (not found)
By:Dr. Prakash Singh Tanwar
42. Binary Search
cout<<“key is not found in array” <<endl;
cout<<“key is found at index ”<<pos<<endl;
By:Dr. Prakash Singh Tanwar
43. Binary Search
cout<<“key is not found in array” <<endl;
cout<<“key is found at index ”<<pos<<endl;
By:Dr. Prakash Singh Tanwar
44. Binary Search-Algo-2
Algorithm
1. BINARY ( DATA, LB, UB, ITEM, LOC )
2. [Initialize Segment Variables]
Set BEG = LB, END = UB and MID = INT ((BEG+END)/2).
3. Repeat Steps 3 and 4 while BEG <= END and DATA [MID] != ITEM.
4. If ITEM < DATA[MID], then:
Set END = MID - 1.
Else:
Set BEG = MID + 1.
[End of if Structure.]
5. Set MID = INT ((BEG+END)/2).
[End of Step 2 Loop.]
6. If DATA [MID] = ITEM, then: Set LOC= MID
Else:
Set LOC = NULL.
[End of if structure.]
7. Exit.
By:Dr. Prakash Singh Tanwar
45. Binary Search
Binary search order is
O(log n )
If n=1024 then it will take max.
log2(1024)=log2(210) =10 steps
1024512256128643216
8421
By:Dr. Prakash Singh Tanwar
46. Q/A
If there are 1000 elements in an array. Then
how many maximum steps are needed to
search an element in an array using binary
search ?
A. 9
B. 1000
C. 999
D. 10
By:Dr. Prakash Singh Tanwar
47. Limitations of
Binary Search
Although the complexity of Binary Search is
O (log n), it has some limitations:
The list must be sorted
One must have direct access to the middle element in
any sublist.
By:Dr. Prakash Singh Tanwar