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 a computer?

A device that performs tasks based on a given set of step-by-step instructions (procedures or algorithms).

What are algorithms in the context of computing?

A set of procedures or step-by-step instructions used by a computer to perform tasks.

What is a data structure?

An arrangement of data in computer memory, defining how objects are stored, the operations that can be performed, the algorithms for those operations, and their efficiency.

Which of the following is typically considered a non-linear data structure?

<p>Tree (C)</p>
Signup and view all the answers

What is a data type?

<p>A classification that specifies the type of value a variable holds and the operations that can be performed on it. It determines the amount of memory needed and the kinds of operations applicable.</p>
Signup and view all the answers

In C++, data types are broadly categorized into which two main groups?

<p>Primitive/built-in and Derived/user-defined (B)</p>
Signup and view all the answers

List two examples of built-in data types in C++.

<p><code>int</code>, <code>char</code>, <code>float</code>, <code>double</code>, <code>bool</code> (any two)</p>
Signup and view all the answers

List two examples of user-defined data types in C++.

<p><code>structure</code>, <code>class</code></p>
Signup and view all the answers

What is the key difference between struct and class data types mentioned?

<p>Classes contain data members and also the operations (member functions) that control the data.</p>
Signup and view all the answers

What is an Abstract Data Type (ADT)?

<p>An ADT is a logical description of a data type, defining a set of objects and a set of operations, without specifying the implementation details (how data is stored or how operations work).</p>
Signup and view all the answers

An algorithm must always complete after a finite number of steps.

<p>True (A)</p>
Signup and view all the answers

Each step in an algorithm can have multiple interpretations.

<p>False (B)</p>
Signup and view all the answers

What is algorithm analysis?

<p>The process of determining the amount of computing resources (like time and storage space) required by different algorithms.</p>
Signup and view all the answers

What does T(n) typically represent in algorithm analysis?

<p>T(n) represents the time complexity, describing the amount of time an algorithm takes as a function of the input size 'n'.</p>
Signup and view all the answers

If an algorithm's performance is tested with a very large number of inputs 'n', what case is being evaluated?

<p>Worst-case (C)</p>
Signup and view all the answers

When calculating the order (Big-O) of a polynomial time complexity function like $T(n) = 2n^3 + 4n - 4$, what is the resulting order?

<p>$O(n^3)$</p>
Signup and view all the answers

What is the definition of Big-Oh notation (O)?

<p>$f(n)=O(g(n))$ means that the growth rate of $f(n)$ is less than or equal to the growth rate of $g(n)$, indicating $g(n)$ is an upper bound for $f(n)$ for sufficiently large $n$.</p>
Signup and view all the answers

What is the definition of Big-Omega notation ()?

<p>$f(n)=\Omega(g(n))$ means that the growth rate of $f(n)$ is greater than or equal to the growth rate of $g(n)$, indicating $g(n)$ is a lower bound for $f(n)$ for sufficiently large $n$.</p>
Signup and view all the answers

What is the definition of Theta notation ()?

<p>$f(n)=\Theta(g(n))$ means that the growth rate of $f(n)$ is the same as the growth rate of $g(n)$, indicating $g(n)$ is a tight bound for $f(n)$ for sufficiently large $n$.</p>
Signup and view all the answers

If $f(n) = \Omega(g(n))$, then it is always true that $g(n) = O(f(n))$.

<p>True (A)</p>
Signup and view all the answers

What is sorting?

<p>The process of reordering a list of items into either increasing or decreasing order.</p>
Signup and view all the answers

How is the efficiency of a sorting algorithm typically measured?

<p>By the number of comparisons and the number of data movements (swaps) made by the algorithm.</p>
Signup and view all the answers

Briefly describe the basic idea of Selection Sort.

<p>Repeatedly find the smallest (or largest) element from the unsorted part of the list and swap it with the element at the beginning of the unsorted part.</p>
Signup and view all the answers

What is the typical time complexity of Selection Sort?

<p>$O(n^2)$</p>
Signup and view all the answers

What is another name for Bubble Sort?

<p>Exchange sort</p>
Signup and view all the answers

Briefly describe the basic idea of Bubble Sort.

<p>Repeatedly step through the list, compare adjacent elements, and swap them if they are in the wrong order. Passes continue until no swaps are needed.</p>
Signup and view all the answers

What is the typical time complexity of Bubble Sort?

<p>$O(n^2)$</p>
Signup and view all the answers

Briefly describe the basic idea of Insertion Sort.

<p>Build the final sorted list one item at a time. Take each element from the input data and insert it into its correct position within the already sorted portion of the list.</p>
Signup and view all the answers

What is the typical time complexity of Insertion Sort?

<p>$O(n^2)$</p>
Signup and view all the answers

Among Bubble Sort, Selection Sort, and Insertion Sort, which is often the fastest for small arrays according to the notes?

<p>Insertion Sort</p>
Signup and view all the answers

What is searching in the context of data structures?

<p>The process of looking for a specific element within a list or collection of items.</p>
Signup and view all the answers

What is another name for Linear Search?

<p>Sequential Search</p>
Signup and view all the answers

What is the time complexity of Linear Search?

<p>$O(n)$</p>
Signup and view all the answers

What type of list is required for Binary Search to work correctly?

<p>An ordered (sorted) list.</p>
Signup and view all the answers

What algorithmic principle does Binary Search utilize?

<p>Divide and conquer.</p>
Signup and view all the answers

What is the time complexity of Binary Search?

<p>$O(\log n)$</p>
Signup and view all the answers

What is a pointer?

<p>A variable used for storing the memory address of another object (typically another variable).</p>
Signup and view all the answers

What does the C++ & operator do when applied to a variable?

<p>It returns the memory address of the variable (its operand).</p>
Signup and view all the answers

What does the C++ * operator do when applied to a pointer variable (dereferencing)?

<p>It returns the value located at the memory address stored in the pointer.</p>
Signup and view all the answers

What does the C++ new operator do?

<p>It dynamically allocates memory for an object or array of a specified type during program execution and returns a pointer to the allocated memory.</p>
Signup and view all the answers

What does the C++ delete operator do?

<p>It releases memory that was previously allocated dynamically using the <code>new</code> operator.</p>
Signup and view all the answers

How are members of a structure accessed directly using a structure variable?

<p>Using the dot operator (<code>.</code>).</p>
Signup and view all the answers

How are members of a structure accessed indirectly using a pointer to the structure?

<p>Using the arrow operator (<code>-&gt;</code>).</p>
Signup and view all the answers

What is a linked list?

<p>A linear data structure consisting of a collection of nodes, where each node stores data and a link (pointer) to the next node in the sequence.</p>
Signup and view all the answers

In a singly linked list, what does the link/pointer field of the last node typically point to?

<p>NULL</p>
Signup and view all the answers

What is list traversal?

<p>A procedure that accesses and processes all elements (nodes) of a data structure (like a linked list) in sequence.</p>
Signup and view all the answers

What is a stack?

<p>A linear data structure that follows the Last-In, First-Out (LIFO) principle, where insertions (push) and deletions (pop) occur only at one end, called the top.</p>
Signup and view all the answers

What does the Push operation do on a stack?

<p>Adds an item to the top of the stack.</p>
Signup and view all the answers

What does the Pop operation do on a stack?

<p>Removes and returns the item from the top of the stack.</p>
Signup and view all the answers

What is a queue?

<p>A linear data structure that follows the First-In, First-Out (FIFO) principle, where insertions (enqueue) occur at one end (rear) and deletions (dequeue) occur at the other end (front).</p>
Signup and view all the answers

What does the Enqueue operation do on a queue?

<p>Adds an item to the rear of the queue.</p>
Signup and view all the answers

What does the Dequeue operation do on a queue?

<p>Removes and returns the item from the front of the queue.</p>
Signup and view all the answers

What is a Tree in data structures?

<p>A non-linear, hierarchical data structure consisting of nodes connected by edges. It has a distinguished root node and parent-child relationships.</p>
Signup and view all the answers

What is the root node of a tree?

<p>The topmost node in the tree hierarchy, which has no parent.</p>
Signup and view all the answers

What are leaf nodes (or external nodes) in a tree?

<p>Nodes in a tree that do not have any children.</p>
Signup and view all the answers

What is a Binary Tree?

<p>A tree data structure in which each node has at most two children, referred to as the left child and the right child.</p>
Signup and view all the answers

What is a Binary Search Tree (BST)?

<p>A binary tree where for each node, all keys in its left subtree are less than the node's key, and all keys in its right subtree are greater than the node's key.</p>
Signup and view all the answers

Describe Pre-order traversal of a binary tree.

<p>Visit the parent node first, then recursively traverse the left subtree, and finally recursively traverse the right subtree (Parent-Left-Right).</p>
Signup and view all the answers

Describe In-order traversal of a binary tree.

<p>Recursively traverse the left subtree first, then visit the parent node, and finally recursively traverse the right subtree (Left-Parent-Right).</p>
Signup and view all the answers

Describe Post-order traversal of a binary tree.

<p>Recursively traverse the left subtree first, then recursively traverse the right subtree, and finally visit the parent node (Left-Right-Parent).</p>
Signup and view all the answers

What is a Heap data structure (max heap)?

<p>A specialized tree-based data structure that satisfies the heap property: in a max heap, for any given node, the value of the node is greater than or equal to the values of its children. It is typically implemented as a complete binary tree.</p>
Signup and view all the answers

In an array representation of a heap, if a node is at index i, what are the indices of its left and right children?

<p>Left child: $2i + 1$, Right child: $2i + 2$.</p>
Signup and view all the answers

What is the expected time complexity of Quick Sort?

<p>$O(n \log n)$</p>
Signup and view all the answers

What is the time complexity of Merge Sort?

<p>$O(n \log n)$</p>
Signup and view all the answers

Shell sort is considered an improvement over which sorting algorithm?

<p>Insertion sort.</p>
Signup and view all the answers

What is Hashing?

<p>A technique used to map keys (e.g., employee IDs, words) to indices (hash codes) in an array (hash table) for efficient lookup, insertion, and deletion.</p>
Signup and view all the answers

What is a Hash Collision?

<p>The situation where two different keys produce the same hash index when processed by a hash function.</p>
Signup and view all the answers

Describe the Division Method hash function.

<p>The hash index is calculated as the remainder of the key divided by the size of the hash table (m): $H(k) = k \pmod m$.</p>
Signup and view all the answers

Describe the Chaining technique for hash collision resolution.

<p>Each slot in the hash table acts as the head of a linked list. All keys that hash to the same slot are stored in that slot's linked list.</p>
Signup and view all the answers

According to the summary table, Merge Sort is a stable sorting algorithm.

<p>True (A)</p>
Signup and view all the answers

According to the summary table, Heapsort requires O(n log n) extra memory space.

<p>False (B)</p>
Signup and view all the answers

Flashcards

What is a Computer?

A device that performs tasks based on step-by-step instructions.

What are Algorithms?

Set of procedures or step-by-step instructions.

What is a Data Structure?

Arrangement of data in computer memory, agreeing how to store and operate on data.

Two ways data stored?

Linear and Non-linear.

Signup and view all the flashcards

What is Data Type?

Item type to be stored in memory, identifying required memory and operations.

Signup and view all the flashcards

Example of Built-in data types?

int, char, float, double, bool.

Signup and view all the flashcards

Example of User-defined data types?

structure, class.

Signup and view all the flashcards

What is Abstract Data Type (ADT)?

Logical view of data, specifying operations without implementation details.

Signup and view all the flashcards

What is a Data structure?

Computer's memory is organized

Signup and view all the flashcards

What is Algorithm?

A clearly specified set of simple instructions to solve a problem

Signup and view all the flashcards

What is Finiteness?

Algorithm must complete after a finite number of steps.

Signup and view all the flashcards

What is Definiteness?

Each step must be clearly defined, having one and only one interpretation.

Signup and view all the flashcards

What is Feasibility?

It must be possible to perform each instruction using resources at hand.

Signup and view all the flashcards

What is Correctness?

It must compute correct answer for all possible legal inputs.

Signup and view all the flashcards

What is Completeness?

It must solve the problem completely.

Signup and view all the flashcards

What is Input/output?

There must be a specified number of input values, and one or more result values.

Signup and view all the flashcards

What is Sequence?

Each step must have a unique defined preceding and succeeding step.

Signup and view all the flashcards

What is Language Independence?

It must not depend on any one programming language.

Signup and view all the flashcards

What is Effectiveness?

It must be possible to perform each step exactly and in a finite amount of time.

Signup and view all the flashcards

What is Efficiency?

It must solve with the least amount of computational resources such as time and space.

Signup and view all the flashcards

What is Generality?

Algorithm should be valid on all possible inputs.

Signup and view all the flashcards

Algorithm Analysis

Determining computing time/storage space required by algorithms.

Signup and view all the flashcards

Complexity of Algorithm

The function T(n) or S(n) describing efficiency of an algorithm.

Signup and view all the flashcards

What is T(n)?

Function describing time algorithm takes in terms of input.

Signup and view all the flashcards

What is S(n)?

Function describing memory space algorithm uses for input.

Signup and view all the flashcards

What are the the testing conditions?

Best, average, worst.

Signup and view all the flashcards

Order of an algorithm

Category of complexity function in terms of resource usage.

Signup and view all the flashcards

Asymptotic Notation

Algorithm has complexity for number of inputs.

Signup and view all the flashcards

What is Big-Oh Notation?

If growth rate of T(n) is less than or equal to f(n)

Signup and view all the flashcards

Big- omega notation

If growth rate of T(n) is greater than or equal to f(n)

Signup and view all the flashcards

Theta notation

Growth rate of f(n) is the same as g(n)

Signup and view all the flashcards

What is little-oh of f(n)?

T(n)= o(f(n)). This is read as “T(n) is little-oh of f(n)

Signup and view all the flashcards

What is little-omega of f(n)?

T(n)= (f(n)). This is read as “T(n) is little-omega of f(n)

Signup and view all the flashcards

Classes Data Type (structures)

Data members that a class contains and the operations that control the data.

Signup and view all the flashcards

Sorting Algorithms

Process of reordering a list of items in either increasing or decreasing order.

Signup and view all the flashcards

Bubble Sort

Simple algorithm to implement and the slowest algorithm on very large inputs.

Signup and view all the flashcards

Insertion Sort

It inserts each item into its proper place in the final list.

Signup and view all the flashcards

Linear Searching

Easy to implement, not practical for searching large collections.

Signup and view all the flashcards

Binary Searching

Works only on an ordered list, uses principle of divide and conquer.

Signup and view all the flashcards

What is a Pointer?

Variable used for storing the address of a memory cell.

Signup and view all the flashcards

Study Notes

  • Data structures organize data within a computer's memory
  • Algorithms provide step-by-step instructions to solve problems

Computer Basics

  • A computer executes tasks based on step-by-step procedures
  • These procedures, called algorithms, enable computers to perform calculations and store/retrieve data efficiently
  • The primary function of a computer involves efficient data storage and retrieval

Data Storage in Computers

  • Computer memory is split into addressable units, but searching for free units is costly
  • Studying data storage and retrieval methods is crucial for efficiency

Understanding Data Structures

  • A data structure defines how data is arranged in computer memory
  • Includes: how to store object collections, allowed operations, related algorithms, and optimization

Data Storage Methods

  • Data in a computer is stored linearly or non-linearly
  • Linear structures include Linked lists, Queues, and Stacks
  • Non-linear structures include Trees and Graphs

Steps for Selecting a Data Structure

  • Analyze the problem to determine the basic operations that must be supported
  • Quantify the resource constraints for each operation
  • Select the data structure that best meets these requirements

Applications of Data Structures

  • Google uses data structures to quickly find pages containing search terms
  • Data structures help find the quickest way to broadcast a message across a network
  • Operating systems track memory (disk/RAM) availability using data structures

Data Types Overview

  • Different data types occupy varying memory and support unique operations
  • A mechanism known as the "data type" system addresses these issues
  • A data type defines the type of data to be stored in memory

Data Type Purpose

  • They establish size for data/items and possible operations
  • Data types are categorized as primitive/built-in and derived/user-defined

Data Type Categories

  • Primitive types consist of numeric and non-numeric types
  • Numeric types are integer and floating-point
  • Non-numeric types are character, string, and Boolean data types
  • Derived types are classes, structures, interfaces, and arrays

Built-in and User-Defined Data Types

  • Built-in data types include int, char, float, double, bool, etc
  • User-defined data types may include structures and classes

Structures vs. Classes

  • Classes feature data members and operations controlling the data

Abstract Data Types (ADTs)

  • User-defined data types create ADTs
  • It's a logical view of data in computer memory, modeling items with characteristics and operations

Data Abstraction

  • This involves modeling or constructing a logical view/picture of an item
  • User-defined data types like class types then map this abstract view into a computer program

ADT Definition

  • It's a set of objects with operations or the externally defined data type holding data and related operations
  • It's called abstract because the internal data storage and operation implementation are not specified

Algorithm Definition

  • An algorithm is a series of computational steps for solving a problem
  • A program/software combines data structures with algorithms

Algorithm Properties

  • Finiteness: Must complete after a finite number of steps
  • Definiteness: Each step is clear, leaving no room for interpretation
  • Feasibility: Must be possible with available resources
  • Correctness: Must compute the correct results for all legal inputs
  • Completeness: Must solve the problem completely
  • Input/output: Must have a specified number of input and output values.

Algorithm Properties

  • Sequence: Each step has unique preceding and succeeding steps, with clearly noted start and halt indicators
  • Language Independence: Must not rely on a specific programming language
  • Effectiveness: Each step must be performed precisely and within a finite timeframe
  • Efficiency: Must solve with minimal time and space requirements.
  • Generality: Should be valid for all possible inputs

Algorithm Analysis Overview

  • Algorithm analysis assesses computing time and space for different algorithms
  • It is the study of program performance and resource usage
  • The process involves establishing the function T(n) or S(n) for a given algorithm

Computer Resources

  • Running Time
  • Memory Usage
  • Communication Bandwidth

Algorithm Performance Analysis

  • Performance involves measuring best, average, and worst-case scenarios for a given algorithm
  • These scenarios utilize notations like (Ο,Ω,Θ,0,0) employing both formal and informal operation-counting methods

Complexity of Algorithms

  • Complexity reflects an algorithm's efficiency relative to the amount of data it processes
  • This is described by the functions T(n) for time or S(n) for space

Time and Space Functions

  • T(n): Indicates the time complexity of an algorithm for 'n' inputs
  • S(n): Indicates the memory space complexity of an algorithm for 'n' inputs

Measuring Algorithm Complexity

  • Time complexity, T(n), is the most common measure
  • The testing conditions depend on the number of inputs

Testing Conditions

  • Best-case: Few inputs
  • Average-case: Normal inputs
  • Worst-case: Large number of inputs

Complexity Function

  • The complexity function T(n) is determined by counting the number of operations in the algorithm

Algorithm Order

  • It's the categorization of a given complexity function related to resource usage
  • This order is determined by the established function T(n) or S(n)

Growth Rate

  • It is determined by how storage or time grows with the size of inputs like O(n), O(n!), O(logn), etc

Algorithm Analysis Steps

Drive the function T(n) for the algorithm

  • Determine the order/category of T(n)

Order Notation

  • A given algorithm order or a function T(n) is written as O(T(n))
  • O(T(n)) means "category/order of the function T(n)"

Example of Algorithm Order

  • If the time complexity function T(n) is categorized in logarithmic time, it's written as O(T(n)) = logn

Calculating Order of T(n)

  • Constant coefficients, logarithm bases, and powers are omitted
  • The highest degree is taken from a polynomial

Rules for Computing Complexity

  • The complexity function T(n) is determined by counting the number of operations of the algorithm
  • There are two types of counting: informal and formal

Complexity Analysis Methods

  • The informal method counts all operations in the algorithm
  • The formal method counts operations ignoring variable initializations and loop controls

Running Time of Selection Statements

  • The running time is {time for condition evaluation} + {the maximum running time for individual clauses}

Loop Running Time

  • The loop running time equals the statements' time inside the loop multiplied by the number of iterations

Nested Loops Timings

  • The total running time of nested loops is multiplied by product of the sizes of all the loops

Blocks or Sequences of Statements

  • Using the order arithmetic addition rule O( f(n)+g(n)) = max( f(n), g(n)), add the time complexities of each statement

Formal vs. Informal Analysis

  • Formal analysis often simplifies complexity and can be more convenient for estimating general efficiency

Complexity Orders

  • O(n) – Linear Time Complexity
  • O(log n) – Logarithmic Time Complexity
  • O(n^2) – Quadratic Time Complexity
  • O(2^n) – Exponential Time Complexity
  • O(1) – Constant Time Complexity

Time-Space Tradeoff

  • Choosing an algorithm often involves balancing time efficiency against memory usage

Exercises

  • Computing resource requirements and determining complexity for different algorithms is important

Asymptotic Notation

  • If there's a complexity function T(n) of an algorithm, the order of complexity in terms of asymptotic notation can be as follows

Types of Asymptotic Notation

  • Big-Oh Notation (O)
  • Big-Omega Notation (Ω)
  • Theta Notation (Θ)
  • Little-o Notation (o)
  • Little-Omega Notation (ω)

Representing Growth Rate

  • These notations are used to represent and explain the growth rate of the function, T(n), of algorithms

Big-Oh Notation (O)

  • If f is a given function, and if some function g is an upper bound for f, then we express this with big O
  • In simple terms, f(n) = O(g(n)) means the growth rate of f(n) is less than or equal to g(n), so g(n) is the upper bound of f(n)

Big Omega notation (Ω)

  • If f is a given function, and if some function g is an lower bound for f, then we express this with big Ω
  • In simple terms, f(n) = Ω(g(n)) means the growth rate of f(n) is greater than or equal to g(n), so g(n) is the lower bound of f(n)

Theta Notation

  • If f is a given function, and if some function g is a tight bound for f, then we say that f is Θ of g
  • In simple terms, f(n) = Θ(g(n)) means the growth rate of f(n) is the same as g(n), so g(n) is a tight bound of f(n)

Algorithm Analysis Focus

  • Asymptotic analysis is used most often
  • Algorithm growth rate increases as size of input increases without bound
  • Focus lies on running time given worst-case inputs

Simple Sorting Algorithms

  • Selection sort
  • Bubble sort
  • Insertion sort

Simple Searching Algorithms

  • Linear/sequential searching
  • Binary searching

Sorting Basics

  • Sorting reorders lists by increasing or decreasing value
  • Algorithm efficiency measured by # of comparisons and data movements

Sorting Algorithms: Simple vs Advanced.

  • Simple algorithms more fit for smaller datasets

Selection Sort Process

  • Scan list, swapping minimum value with current position in the list. Iterate one position at a time until complete

Selection Sort Steps:

  1. Go through the array from i=0 to n-1
  2. Select the smallest element from i to n
  3. Swap this value with position i

Selection Sort

  • This algorithm repeatedly finds the next smallest element in an unsorted array and moves it to its final sorted position

List Division in Selection Sort

  • Maintains a sub-list of sorted items and a sub-list of remaining items

Selection Sort Analysis

  • The outer loop executes n-1 times
  • Inner loop executions are about n(n-1)/2 on average: O(n²)
  • Work done in inner loop is constant

Efficiency of Selection Sort

  • Comparisons: O(n^2)
  • Swaps: O(n)

Bubble Sort

  • Called the Exchange Sort

Bubble Sort Details

  • Simplest to implement but least efficient on large datasets
  • Array is processed, swapping adjacent elements if out of order

Bubble Sort Process

  • Each pass compares adjacent, unsorted elements
  • Larger elements bubble toward their final sorted positions
  • Continue sorting from the first to second largest elements, and so on

Bubble Sort Analysis

  • n = a.length = size of the array
  • The outer loop executes n-1 times
  • Each time the outer loop is executed, the inner loop is executed

Inner Loop Analysis

  • Inner loop executes n-1 times at first, linearly dropping to just once
  • On average, inner loop executes about n(n-1)/2 times for each execution of the outer loop
  • In the inner loop, the comparison is always done (constant time), the swap might be done (also constant time)

Bubble Sort Result

Result is (n-1) * [ n(n-1)/2 ] + k, that is, O(n²)

Bubble Sort Efficiency

  • Comparisons: O(n²)
  • Swaps: O(n²)
  • Simplicity comes at cost of speed in large datasets

Insertion Sort Goal

  • Each item is inserted in its proper place in the final list

Insertion Sort Steps

  • The left most value can be said to be sorted relative to itself, so there is not an action required
  • Check to see if the second value is smaller than the first one; if it is, swap values. The first two values are now relatively sorted

Insertion Sort Details

  • Remove the third value first and slide the second value to make room for insertion. Insert the value in the proper position, so now the first three are relatively sorted
  • Do same for remaining items in the list

Insertion Sort Algorithm

Algorithm checks and places each value in sorted portion of list

Insertion Sort Analysis

  • Outer loop runs once through each n element
  • Inner loop looks at 1/2 of 'n' elements then this gives a second factor of n/4 In Insertion sort the time required is proportional to n²/4 so insertion sort is O(n^2)

Insertion Sort Efficiency

  • Comparisons: O(n²)
  • Swaps: O(n²)

Summary of sorting algorithms

Bubble Sort, Selection Sort, and Insertion Sort are all O(n²) Within O(n²),

  • Bubble Sort is very slow, and should probably never be used for anything
  • Selection Sort is intermediate in speed
  • Insertion Sort is usually the fastest–-in fact, for small arrays (like 10 or 15 elements), insertion sort is faster than more complicated sorting algorithms

Comparison of "Simple" Sorts

  • While easy to code, these sorts perform poorly on larger datasets

Searching Algorithms

  • Searching identifies a specific element or determines its absence

Data Set

  • Sequential search and Binary Search are used
  • Called sequential, but easy to understand and implement

Linear Search Procedure

  • Scan list, comparing elements to identified variable
  • End once match or last list item reached Return -1 if identified variable is not found

Linear Search Analysis

  • Loop is proportional to input (n)

Binary Search Basis

  • Binary Search is efficient for ordered lists This is on the basis of divide and conquer

Binary Search Steps

The basic idea is locates middle of array

  • Determine if target is in lower or upper half of an array Loop until end occurs or negative is found

Binary Search Analysis

  • Loop is proportional to log based of length of the array Therefore the time complexity is O(log n)

CHAPTER 3 – LINKED LISTS

  • Pointer, Dynamic Memory allocation and De-allocation Pointer A pointer is a variable used for storing the address of a memory cell. This address is the location of another object (typically another variable) in memory.

Syntax:

type *name; type is the base type of the pointer and may be any valid type. The name of the pointer variable is specified by name.

Pointer Operators:

& - is a unary operator that returns the memory address of its operand.

  • -is an unary operator that returns the value located at the address that follows.

Arrays of Pointers

The declaration for an int pointer array of size 10 is: int *x[10]; To assign the address of an integer variable called var to the third element of the pointer array, write: x[2] = &var; To find the value of var, write: *x[2];

Operators for Allocation and Deallocation

  • Memory that has been allocated by a call to new can be released by delete operator

Structure is defined as:

struct struct-tag { Typel member variable1; Type2 member variable2; Type n member variablen; } variable-namel, variable-name2...;

  • arrow operator (- ->) point it toward data members. Like pointer to an object calls variable or operator

Self Referenced using pointer

struct Node { Data typel data; Data type2 data; Node * next; // Name given to pointer which point to Node data type }

Singly Linked Lists

  • collection of nodes storing data and links to other nodes. A node contains some information useful for a specific application and a pointer to the next node. The most flexible implementation is by using pointers
  • A node of linked list structure has a link only to its successor in the sequence of node, so the list is called a single linked list

Linked List Rules

  • Linked list structure which can change during execution
  • Successive elements are connected by pointers, and Last element points to NULL. If the list currently contains 0 nodes, the head points to NULL

Singly Linked Lists

  • List can grow and shrink with execution
  • List made as long as needed and that do not waste memory

Process to Create a new node process

  1. Allocate memory for the new node with 'new' assignment
  2. Initialize the contents of the node and copy data into struct
  3. Set the pointer field to NULL value

Doubly LinkedList

  • The pointer in the first node will be default as NULL in head pointer, node and all next nodes

New Node Insertion Location

  • The simplest strategy for adding is at the beginning of a list. (see the bullet points)

Step to Insert to Beginning

  1. Allocate space for a new node and copying the item into it.
  2. Making pointer from new node to current head,
  3. Make the head of the list point to the newly-allocated node, (see visual aides for context)
  • Addition at the beginning of the list is fast and efficient

Linked List Insertion Strategies

Insertion at the end or to create the list.

  • Allocate a space for new node and copying in data

Steps to new nodes to the end of a linked list

The steps to add a new node to the end of the list Allocate the space for a new node Copy the item in to it and initialize the new code

To append a new node to an empty List

Include the node in the list by making the next member of the last node point to the newly created node (see visual guides for context) assigning address to tail The steps to add node in a specified location: Allocation made and info copied Keep track of 2 node to find the right place in the list

Deleting from LinkdList

A node can be at head, end, or middle, and here release from memory. to do a delete from start of the list *Make temp pointer first

  • Point it to head=temp- head= head next and move this location
  • second code set then *delete temp

Operations for LinkedListDeletion Methods

Unlike the previously stored lists, the LinkedList have to be modified to avoid null points and ensure nodes link correctly

  • Deleting a specific node from a list is to Unlink the node and connect to the next available node, and Delete the unlinked node* The method then iterates to search all the nodes related to a number sequence in list (see example)

Traversing a Linked List is a procedure that accesses and processes elements linked from to data structure in sequention to display note

To display a list nodes:

  • Visit each node in a linked list and then perform basic processes
    • Basic process to Traverse to Display Nodes ***
  • Set pointer to where the contents are contained- While there is not a NULL pointer:
  • display data go to next node by setting Pointer field. the list-end while

** CHAPTER 4 – STACKS AND QUEUES **

Stack Overview

– data structure gives temp storage so element set first will be retrieved last is (LIFO) “last in first out.

  • items added "pushed to list“ is removed using the stack. top. Stack temp storage. nesting

Operations:

  • operations
  • Push s- push #k as stack s. then Popt s deleting s Peeks returning s the value
  • -IsEmptyts

IsFulls returns code if FULL

    • Array Implementation of Stacks: The PUSH operation ** -increment the stock up is alway stop +check low step + go

Step2 PUSH element in with value to be added

-Array implementation that Pops Operation

step give up "or empty give a value (see description step a and hold element point up hold value value and and change one

The Queues is used for datastructures

queues has that has the access for it for front and back. and of of the operations

Tree Categories

  • Three kinds:Normal Priority Doublly-Ended (Dequeue) queuess-

Queue Details

  • Operates via First in First Out or FIFO method -Using 2 or more indexes to track and basic operations use
  • Enqueue-enter in data in rear of of the and and Queues
  • Dequeue remove data and point in of of these questions in see see operations

LinkedList Details

To build basic arrays, to add variables, elements that index that and how the Qsize add to Q. check notes and look at sample code

CHAPTER 5 - TREE STRUCTURES

Tree Overview

A tree consists of nodes and edges, illustrating a hierarchical structure and includes these traits:

  • Single root node, with connections as descendants
  • Each except root connects from P as parent with the descendants

Tree properties

  • Paths and length noted from root Root
  • Parent less, Top most
  • Node with children Leaf Child-less, Last row
  • Relationships
  • Ancestors the root path
  • Descendants all relations from above (see graph

Tree Terms- Depth

  • Number of level from root

  • Tree Types, and Binary Tree are:

  • A Tree is root and descendants

  • Binary has most 2 children

Full, Full Binary, Balanced Types

  • Full BT has all zeros or children
  • Balance BT has node and children

Binary Search BST(ordered

  • Search tree with the following: Each node <, node, no repeat number. Then *Right keys go larger left smaller

DATA STRUCTURE

  • Structure that declares value and right and left, with right link
  • Operation and BinaryTree =Root

Tree Operations

  • *Insertion: Preserves the binary structure There = Null; code set that in by. 2 ptrs. insertBST for no data, should and for the set value ( see steps) CASE DATA= insert at position. See search algorithms. insert that node that can

Tree Binary Search Methods

  • Binary searches in tree can be traversered in 3 ways: pre, post, in order
    • a Pre order =Traverse to see parent side left and right - post order - traverse and show right and parent
  • inO. show to to list ( read ascending)
  • post. do to set to read for the

DELETION

  • To remove known. Note value from Bst And. four should all the (see graph/image. read and steps)

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser