Data Structures and Algorithms

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the primary purpose of a data structure?

  • To randomly scatter data across memory.
  • To store and organize data efficiently for effective use. (correct)
  • To limit the amount of data a computer can process.
  • To complicate data storage.

Which of the following is NOT a characteristic of a well-designed data structure implementation?

  • Efficient space complexity.
  • Correctness in implementing its interface.
  • Unpredictable behavior. (correct)
  • Minimal time complexity for operations.

Which factor is most crucial when determining the efficiency of a data structure?

  • The size of the computer screen displaying the data.
  • The color of the data structure.
  • The programming language used.
  • The correctness of the implementation and its effect on time complexity. (correct)

What distinguishes a 'linear' data structure from a 'non-linear' data structure?

<p>Linear structures store data in a sequential order, while non-linear structures do not. (A)</p> Signup and view all the answers

If a program needs to search data quickly, which aspect of data structures is most important?

<p>How well the data structures facilitate efficient searching. (B)</p> Signup and view all the answers

Why are algorithms considered to have a 'definite beginning and end'?

<p>Because without them, the algorithm could run forever or never start. (A)</p> Signup and view all the answers

What does the term 'array' signify in the context of data structures?

<p>A finite, ordered collection of similar data elements in contiguous memory locations. (B)</p> Signup and view all the answers

What happens if an array is declared to hold integers but a character is inserted?

<p>The array may cause an error because it is designed to hold only one type. (D)</p> Signup and view all the answers

In array terminology, what does 'lower bound' generally refer to?

<p>The index of the first element in the array. (D)</p> Signup and view all the answers

In the context of arrays, what is meant by the term 'range'?

<p>The number of elements contained in the array. (B)</p> Signup and view all the answers

When referring to a 2-D array, what do rows and columns represent?

<p>Rows represent horizontal data, and columns represent vertical data. (C)</p> Signup and view all the answers

What is the benefit of categorizing arrays as one-dimensional or multi-dimensional?

<p>It helps in optimizing memory usage and data access patterns. (A)</p> Signup and view all the answers

What does the term 'traversal' mean in the context of array operations?

<p>Accessing and processing each element in the array exactly once. (C)</p> Signup and view all the answers

In an array, if the 'Upper Bound' equals 'N-1' (where N is the size), what does this imply when inserting new elements?

<p>The array is full, and no more elements can be inserted without resizing. (D)</p> Signup and view all the answers

If you need to insert an element at the beginning of an array, what typically needs to happen to other elements?

<p>Other elements must be shifted to higher indexes to accommodate the new element. (B)</p> Signup and view all the answers

When deleting an element from the beginning of an array, what generally happens to the remaining elements?

<p>They shift to fill the gap left by the removed element. (A)</p> Signup and view all the answers

In the context of deleting elements from an array, what does 'Underflow' signify?

<p>The array is empty, so no elements can be removed. (D)</p> Signup and view all the answers

What adjustment needs to be made to the Upper Bound (UB) after deleting an element from an array?

<p>UB needs to be decreased by one. (C)</p> Signup and view all the answers

When discussing 2-D arrays, what is meant by 'Row-Major Arrangement'?

<p>Elements are stored row by row. (D)</p> Signup and view all the answers

What does the formula Base Address + W * (i - LR) calculate in the context of array addressing?

<p>The address of a specific element in a 1-D array. (D)</p> Signup and view all the answers

A stack is often described as 'Last-In, First-Out' (LIFO). What does this term imply about how elements are added and removed?

<p>The last element added is the first one removed. (A)</p> Signup and view all the answers

In stack terminology, what is the 'top' of the stack?

<p>The most recently added element in the stack. (C)</p> Signup and view all the answers

How is a stack typically implemented?

<p>Using arrays or linked lists with restricted access. (B)</p> Signup and view all the answers

In stack operations, what does the term 'push' refer to?

<p>Adding a new element to the top of the stack. (C)</p> Signup and view all the answers

What is meant by 'Overflow' in the context of stack operations?

<p>Adding an element to a full stack. (A)</p> Signup and view all the answers

What is a practical application of stacks?

<p>Reversing a string. (A)</p> Signup and view all the answers

What is the key characteristic of recursion?

<p>A function calling itself. (D)</p> Signup and view all the answers

What is one of the key requirements for a recursive function?

<p>It must have a base condition to prevent infinite loops. (A)</p> Signup and view all the answers

What does 'Polish Notation' refer to in the context of expressions?

<p>It is a notation where the position of operators determines the order of operations. (A)</p> Signup and view all the answers

What is the main characteristic of 'Infix' notation?

<p>Operators are written between their operands. (B)</p> Signup and view all the answers

In 'Postfix' notation, where are operators typically placed?

<p>After both operands. (B)</p> Signup and view all the answers

What is the primary purpose of converting an infix expression to postfix?

<p>To simplify the calculation process for computers. (D)</p> Signup and view all the answers

Considering the rules of the Tower of Hanoi puzzle, which action is NOT allowed?

<p>Placing a larger disk on top of a smaller disk. (A)</p> Signup and view all the answers

What is typically the recursive approach for solving the Tower of Hanoi problem?

<p>Breaking the problem into smaller, similar subproblems. (C)</p> Signup and view all the answers

Flashcards

Data Structure

A particular way of storing and organizing data in a computer so that it can be used efficiently.

Algorithm

A sequence of steps to solve any given problem.

Correctness

Data structure implementation should implement its interface correctly.

Time Complexity

Running time of the operations of data structure must be as small as possible.

Signup and view all the flashcards

Array

A finite collection of similar elements stored in adjacent memory locations.

Signup and view all the flashcards

Finite Array

Specific number of elements in an array.

Signup and view all the flashcards

Similar Array Elements

All elements in an array are of the same type. (e.g., integers or characters)

Signup and view all the flashcards

Lower Bound

Smallest index of an array.

Signup and view all the flashcards

Upper Bound

Largest index of an array.

Signup and view all the flashcards

Range

The number of elements in the array.

Signup and view all the flashcards

Array as Pairs

A set of index and value pairs

Signup and view all the flashcards

One-Dimensional Array

An array with one dimension.

Signup and view all the flashcards

Multi-Dimensional Array

An array with multiple dimensions.

Signup and view all the flashcards

Traversal

Visiting each element in the array.

Signup and view all the flashcards

Insertion

Adds an element to the array.

Signup and view all the flashcards

Deletion

Removes an element from the array.

Signup and view all the flashcards

Search

Looks for a specific element in the array.

Signup and view all the flashcards

Row-Major Arrangement

Stores array elements row by row in memory.

Signup and view all the flashcards

Column-Major Arrangement

Stores array elements column by column in memory.

Signup and view all the flashcards

Stacks

Linear data structures that allow to insert and delete an element

Signup and view all the flashcards

Stacks

The addition or deletion of elements is permitted at one end only, either the beginning or the end.

Signup and view all the flashcards

Push

When an item is added to the stack

Signup and view all the flashcards

Pop

When an item is removed from the stack

Signup and view all the flashcards

Push

Addition of an item to the stack

Signup and view all the flashcards

Pop

Removal of an item from the stack

Signup and view all the flashcards

Stacks

The linear data structures as array and a linked list allow us to insert and delete an element

Signup and view all the flashcards

Overflow

stack is full and no new elements can be added

Signup and view all the flashcards

Underflow

stack is empty and there are no elements to remove

Signup and view all the flashcards

Ordered Collection

Stack as an array

Signup and view all the flashcards

TOP element

is a pointer variable which contains the location of the top element of the stack

Signup and view all the flashcards

Postfix

is in which the operators come after their operands, the resulting expression

Signup and view all the flashcards

Infix

is when operators exist between two operands, the expression

Signup and view all the flashcards

Prefix

Polish Notation is when operators are written before their operands, the expression

Signup and view all the flashcards

Tower of Hanoi

Mathematical puzzle

Signup and view all the flashcards

Tower of Hanoi

moves the tower to another Tower

Signup and view all the flashcards

Study Notes

  • Data structure is a way of storing and organizing data in a computer so that it can be used efficiently.

Classification of Data Structures

  • There are two major classifications: Linear and Non-linear.
  • Linear data structures include link lists, stacks and queues.
  • Non-linear data structures include trees and graphs.

Algorithms

  • Algorithms are sequences of steps to solve any given problem.
  • Algorithms have a definite beginning and a definite end.
  • Algorithms contain a finite number of steps.

Characteristics of Data Structures

  • Correctness: Data structure implementation should implement its interface correctly.
  • Time Complexity: Running time of the execution of data structure operations must be as small as possible.
  • Space complexity is also a factor.

Need for Data Structures

  • Facilitates searching data.
  • Necessary for managing processor speed.

Execution Time Cases

  • Worst case
  • Average case
  • Best case

Arrays

  • An array is a finite collection of similar elements stored in adjacent memory locations.
  • Finite means that there are a specific number of elements in an array.
  • Similar means that all the elements in an array are of the same type.
  • Example: an array may contain all integers or all characters but not both.
  • An array consisting of n number of elements is referenced using an index that varies from 0 to (n-1).
  • The elements of A[n] containing n elements are denoted by A[0], A[1], ... A[n-1], where 0 is the lower bound, n-1 is the upper bound.
  • The lowest index of an array is called its lower bound and highest index its upper bound.
  • The number of elements in the array is called its range.
  • An array is a set of pairs of an index and a value.
  • An array is further categorized as a one-dimensional array and multi-dimensional array.
  • A multi-dimensional array can be a 2-D array, 3-D array, 4-D array, etc.
  • An array is judged as 1-D or 2-D by the syntax used to declare the array.
  • A[5]: A 1-D array holding 5 elements.
  • A[2][5]: A 2-D array with 2 rows and 5 columns holding 10 elements.
  • A[2][5][3]: A 3-D array with two 2-D arrays each of which is having 5 rows and 3 columns, holding totally 30 elements.

Operations on Arrays

  • Insertion
  • Deletion
  • Traversal
  • Search

Insertion at End

  • If UB = maximum N-1 then "overflow".
  • Read data or value to be inserted.
  • Set UB = UB+1.
  • A[UB] = data.
  • Stop or Exit.

Insertion at Beginning

  • If UB = maximum N-1 then "overflow".
  • Else Read data
  • K = UB
  • Repeat step
  • While K >= LB
  • A[K+1] = A[K]
  • K = K-1
  • A[LB] = data
  • Stop

Insertion at a Specific Location

  • Shift elements from 7 to 4 shifted to 5, moving UB to loc, from (UB+1) to (loc+1)
  • Initialize a counter I = maximum UB
  • Repeat, while (I > loc)
  • A[I+1] = A[I]
  • I = I-1
  • UB = UB+1, A[loc] = data
  • Exit

Deleting from beginning

  • If UB=-1, then Underflow
  • K = LB
  • Repeat till step 5 while K < UB
  • A[K] = A[K+1]
  • K = K+1
  • A[UB] = NULL
  • UB = UB-1
  • Exit

Deletion of a Particular Element

  • If UB = -1, "Underflow"
  • Read data
  • K = LB
  • Repeat step while A[K] != data
  • K = K+1
  • Repeat step while K =k+1

Representation of 2-D Arrays in Memory

  • A 2-D array is a collection of elements placed in m rows and n columns.
  • Syntax: the syntax used to declare a 2-D array includes two subscripts, of which one specifies the number of rows and the other specifies the number of columns of an array.
  • e.g., A[3][4] is a 2-D array containing 3 rows and 4 columns.
  • A[0][2] is an element placed at 0th row and 2nd column.
  • A 2-D array is also called a matrix.

Row Major and Column Major Arrangement

  • Rows and columns of a matrix are only a matter of imagination.
  • When a matrix gets stored in memory all elements of it are stored linearly.
  • Since computer's memory can only be viewed as consecutive units of memory.
  • This leads to two possible arrangements: row-major arrangements and column-major arrangements.
  • int A[3][4] = { {12, 1, -9, 23}, {14, 7, 11, 121}, {6, 78, 15, 34} }

Row Major Arrangement

  • 12 1 -9 23, 14 7 11 121, 6 78 15 34
  • UB = UB-1

Column-Major Arrangement

  • 12 14 6, 1 7 78, -9 11 15, 23 121 34
  • Since the array elements are stored in adjacent memory locations, we can access any element of the array once we know the base address of the array & no. of rows & columns present in the array.

Base Address Calculation

  • If base address of the array is 502 & we wish to refer the element 121, then the calculation involved
  • Element 121 is present at A[1][3].
  • Location of 121 would be = 502 + 1*4 + 3 = 516
  • In general, for an array A[m][n] the address of element A[i][j] would be
  • Base address + i*n + j
  • For column major arrangement: Base address + j *m + i

Address Calculation in 1-D Array

  • Address of A[i] = B + W * (i - LB)
  • B -> Base address of array
  • W -> Storage size of one element
  • I -> Subscript of element where address is to be found

Question - Address Calculation

  • LB -> Lower Bound of subscript, if not specified assume 0 (zero)
  • Given base address of an array B[1300 ... 1900] as 1020 & size of each element is 2 bytes, find address of B[1700].
  • The given values are: B = 1020, W = 2, LB = 1300, I = 1700

2D array addressing

  • Address of A[i][j] = B + W * [N*(I-LR) + (J-LC)] {Row major order}
  • Address of A[i][j] = B + W * [(I-LR) + M*(J-LC)] {column major order}
  • Where, B -> Base Address, I Row Subscript.
  • J-> Column, W-> Storage Size of one element
  • Lr -> Lower limit / start row index if not given assume zero
  • Lc Lower limit / start column index if not given assume zero
  • M -> No. of rows of given matrix , N -> No. of columns
  • Usually no. of rows & columns of a matrix are given like A [20] [30] etc.
  • but if not given as A [Lr... Ur] [Lc... Uc].
  • then the no. of rows & columns are calculated using:
  • No. of rows (m) = (Ur - Lr) + 1
  • No. of cols (N) = (Uc - Lc) + 1

Stacks

  • Linear data structures as array and a linked list allow us to insert & delete an element at any place in the list.
  • However, sometimes it is required to permit the addition or deletion of elements only at one end that is either beginning or end.
  • Stack and Queues are two types of data structures in which addition or deletion of an element is done at end, rather than middle.
  • A stack is a data structure in which addition of a new element or deletion of an existing element always takes place at the same end.
  • This end is often known as top of stack.
  • A stack of plates in cafeteria on, where every new plate is added to the stack is added at top
  • Every new plate taken off the stack is also from top
  • When an item is added to the stack, the operation is called push and when an item is removed from the stack, the operation is called pop.
  • Stack is also called as last-in-first-out (LIFO) list.
  • Stack is generally implemented with two basic operations: Push and Pop.
  • Stack contain an ordered collection of elements.
  • An array is used to store ordered list of elements, hence it is very easy to manage a stack if we represent it using an array.
  • Each of our Stacks will be maintained by a linear array
  • STACK; a pointer variable TOP, which contains the location of the top element of the stack.
  • A variable MAXSTK which gives the maximum number of elements that can be held by the stack.
  • The condition TOP = 0 or TOP = NULL will indicate that the stack is empty.

Push Algorithm

  • If TOP = MAXSTK then Print Overflow & Return (stack already filled)
  • Set TOP = TOP + 1
  • Set STACK[TOP] = ITEM
  • Return.

Pop Algorithm

  • If TOP=0 then Print UNDERFLOW & Return (Stack has an item to be removed?)
  • Set ITEM = STACK[TOP]. (Assigns Top element to ITEM)
  • Set TOP = TOP - 1
  • Return
  • 2 stack implementation in one array

Push A Algorithm

  • If TOPA + 1 = TOPB OR TOPB - 1 OR TOP A = MAXSTK then Print overflow & Exit.
  • TOPA = TOPA + 1
  • STACK[TOPA] = Item
  • Exit

Stack Applications

  • Reversing a string:
  • Read elements of the string and push onto the stack
  • Pop elements from stack and assign to a new string, yielding a reversed string
  • Factorial Calculation is a stack application
  • Recursion is when function calls itself.

Polish Notation

  • The process of writing the operators of an expression either before or after their operands
  • The fundamental property is that the of operators and operands
  • One never needs parentheses when writing an expression in polish notation. There are classified in three categories.
  • Infix, operators exist between 2 operands
  • Prefix , operators written before operands
  • Postfix, operators come after operands

Tower of Hanoi

  • Tower of Hanoi is a mathematical puzzle which consists of three towers and series of different sized rings, which must adhere to particular rules to solve the puzzle.
  • Objective: The mission is to move all the disks to some tower without violating the sequence of arrangement.
  • Rules: Only one disk can be moved among the towers at any given time; only the top disk can be removed; no large disk can be placed over the small disk.

Recursive Function for Tower of Hanoi

  • Move top (n - 1) disks from S to A
  • Move top disk from S to D
  • Move top (n - 1) disks from A to D
  • If N=1 then print move disk from S to D
  • Else TOWER (N-1, S, D, A)
  • Print move disk from S to D
  • TOWER(N-1, A, S, D)

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Use Quizgecko on...
Browser
Browser