Basic Data Structures, Summaries of Data Structures and Algorithms

An introduction to various data structures such as arrays, strings, stacks, queues, and basic math operations. It also covers topics such as asymptotic analysis, Euclid’s GCD Algorithm, basic recursion, greedy algorithms, sqrt(n) primality testing, naive string searching, O(n log n) sorting, and basic dynamic programming. the operations of stack and queue with examples. It also provides a solution to a problem related to stack. The document can be useful as study notes, lecture notes, and summaries for computer science students.

Typology: Summaries

2022/2023

Available from 03/21/2023

bhanu-prakash-2409
bhanu-prakash-2409 🇮🇳

1 document

1 / 53

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures Index
Data structure
LInear data structure
Static
Array
String
Dynamic
Queue
Stack
Non linear data structure
Array
Introduction to Array
Access the Elements of an Array
Modifying the elements of an array
Length of an Array
Looping Array Elements
Adding Array Elements
Removing Array Elements
Sort Array Elements
Reverse Array Elements
String
String operations
String methods,String Built-In Functions.
Stack
Operations of stack
Examples for stack operations
Queue
Operations of Queue
Examples for Queue operations
Asymptotic analysis
O(1)
O(log n)
O(n)
O(n log n)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35

Partial preview of the text

Download Basic Data Structures and more Summaries Data Structures and Algorithms in PDF only on Docsity!

Data Structures Index

Data structure ○ LInear data structure ■ Static ● Array ● String ■ Dynamic ● Queue ● Stack ○ Non linear data structure ★ Array ○ Introduction to Array ○ Access the Elements of an Array ○ Modifying the elements of an array ○ Length of an Array ○ Looping Array Elements ○ Adding Array Elements ○ Removing Array Elements ○ Sort Array Elements ○ Reverse Array Elements ★ String ○ String operations ○ String methods,String Built-In Functions. ★ Stack ○ Operations of stack ○ Examples for stack operations ★ Queue ○ Operations of Queue ○ Examples for Queue operations ★ Asymptotic analysis ○ O(1) ○ O(log n) ○ O(n) ○ O(n log n)

○ O(n^2) ○ O(2^n) ★ Basic Math Operations ○ Addition ○ Subtraction ○ Multiplication ○ Division ○ Exponentiation ★ Euclid’s GCD Algorithm ○ Introduction and Algorithm Implementation in Python ★ Basic Recursion ○ Recursion Intro ○ Example problem ★ Greedy Algorithms ○ Introduction ○ Properties of Greedy Algorithm ○ Applications of Greedy Algorithms ○ Example problem ★ sqrt(n) Primality Testing ○ Introduction and Algorithm Implementation in Python ★ Naive String Searching ○ Introduction ○ Basic steps of the naive string searching algorithm Implementation in Python ★ O(n log n) sorting ○ Merge sort ○ Quicksort ○ Heapsort ★ Binary SearchingBasic Dynamic Programming ○ Basic Introduction ○ Steps of dynamic programming ○ Example problem on dynamic programming

Arrays

Array is a collection of elements of the same data type that are stored in contiguous memory locations. The elements of an array can be accessed using an index, which is an integer value that represents the position of an element in the array. These are commonly used in programming for various purposes, such as storing a collection of data, organizing data into tables or matrices, and implementing algorithms that require efficient access to a large number of elements. ➢ Each element in the array has its own index which starts from 0 to n. 2 5 7 6 3 3 2 8 16 - Elements 0 1 2 3 4 5 6 7 8 - index ➢ The elements of an array can be accessed using an index that represents the position of the element in the array ➢ The index for each element has different values and does not have the same for any element in an array. ➢ Arrays are useful when we need to store a large amount of data, and we want to access it quickly.

➢ We can run the python in our terminal by giving the command python3 and run it then we enter into the python compiler. ➢ Giving an array and printing the elements present in an array in python.

Access the Elements of an Array:-

We access the elements of an array by using the array index and print the elements. Here we print the 0th element in an array by giving the index of that element.

Modifying the elements of an array:-

We can modify the array elements by using the index of the elements,for example there are four elements in an array like 4,6,3, Here we should change the 4 element to 9 by using below method,

We can remove an element in an array by using the pop() method and array index. We can also use remove() method to remove an element.

Sort Array Elements:-

We can sort the array elements using the sort() method.

Reverse Array Elements:-

We can reverse the array element by using the reverse() method.

String

String is a data structure that represents a sequence of characters. A string can be defined as a collection or sequence of characters, represented either as an array of characters or as an object of a string class. In most programming languages, including Python, strings are immutable, meaning they cannot be changed once they are created.

String Operations

Concatenation - combining two or more strings into one. ★ Substring - extracting a portion of a string. ★ Length - determining the number of characters in a string. ★ Comparison - comparing two strings to see if they are equal.

String Built-In Functions

str() - Converts the specified value into a string. ➢ len() - Returns the length of a string. ➢ lower() - Converts all characters in a string to lowercase. ➢ upper() - Converts all characters in a string to uppercase. ➢ capitalize() - Converts the first character of a string to uppercase. ➢ title() - Converts the first character of each word in a string to uppercase.

strip() - Removes whitespace characters from the beginning and end of a string. ➢ replace() - Replaces all occurrences of a specified substring with another substring. ➢ split() - Splits a string into a list of substrings based on a specified delimiter. ➢ join() - Joins a list of substrings into a single string using a specified separator. ➢ startswith(prefix) - Returns True if the string starts with the specified prefix, otherwise False. ➢ endswith(suffix) - Returns True if the string ends with the specified suffix, otherwise False.

islower() - Returns True if all characters in the string are lowercase, otherwise False. ➢ isupper() - Returns True if all characters in the string are uppercase, otherwise False. ➢ isspace() - Returns True if all characters in the string are whitespace (spaces, tabs, newlines), otherwise False.

Stack

Stack is a linear data structure that follows the principle of Last In First Out (LIFO). This means the last element inserted inside the stack is removed first. ➢ Stacks are used in many applications, such as in computer memory, web browsing, and compiler implementations. ➢ In Python, we can implement stacks using either lists or deque from the collections module.

Stack push and pop operations
Operations of Stack:-

Push : Add an element to the top of a stack. ➢ Pop : Remove an element from the top of a stack. ➢ IsEmpty : Check if the stack is empty. ➢ IsFull : Check if the stack is full. ➢ Peek : Get the value of the top element without removing it.

Queues

➢ Queue is a linear data structure based on the First-In-First-Out (FIFO) principle. ➢ In other words, the first element that is added to the queue is the first one to be removed. ➢ Queue is a useful data structure in programming. It is similar to the ticket queue outside a cinema hall, where the first person entering the queue is the first person who gets the ticket. ➢ Queues are used in many applications, such as in scheduling algorithms, network traffic management, and task processing. ➢ In Python we can implement queues using the queue module or using lists. FIFO Representation of Queue

Operations of Queue:-

Enqueue : Add an element to the end of the queue.adding of the element to the queue is done by the append() method. ➢ Dequeue : Remove an element from the front of the queue.removal of the elements in the queue is done by the pop() method. ➢ IsEmpty : Check if the queue is empty. ➢ IsFull : Check if the queue is full. ➢ Peek : Get the value of the front of the queue without removing it.

Example for Queue operations:- queue = [] # Add elements to the queue queue.append('a') queue.append('b') queue.append('c') queue.append('d') # Print the queue print("Queue: ", queue) # Remove an element from the queue queue.pop(0) # Print the queue print("Queue: ", queue) # Get the front element from the queue without removing it front= queue[0] print("Front element: ", front)

Problem 2

Problem Statement:- A letter means push and an asterisk means pop in the following sequence. Give the contents of s[0], ..., s[4] after the execution of Sedgewick Program 4.4 (or the array implementation of a stack in theADT Lecture). L A * S T I * N * F I R * S T * * O U * T * * * * * * Solution:- stack = [] ops = 'L A * S T I * N * F I R * S T * * O U * T * * * * * ' for op in ops.split(): if op == '': value = stack.pop() else: stack.append(op) print(stack[:5])

Problem 3

Problem Statement:- Modify the stack program in the lecture notes to issue a warning and exit if the client program attempts to pop an empty stack or push onto a full one. Solution:- class Stack: def init(self, capacity): self.capacity = capacity self.size = 0 self.items = [] def is_empty(self): return self.size == 0 def is_full(self): return self.size == self.capacity def push(self, item): if self.is_full(): print("Stack is full. Cannot push any more elements.") exit() self.items.append(item) self.size += 1 def pop(self): if self.is_empty(): print("Stack is empty. Cannot pop any more elements.") exit() self.size -= 1 return self.items.pop() # Create a stack with capacity of 5 stack = Stack(5) # Push 5 elements to the stack stack.push(1)