0% found this document useful (0 votes)
29 views73 pages

Dsa Week 2 Updates

Uploaded by

riyabhart02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views73 pages

Dsa Week 2 Updates

Uploaded by

riyabhart02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Week#02

Data Structures
CS2001
Email: [email protected]
Array
Contiguous area of memory consisting of equal-size elements
indexed by contiguous integers.
What’s Special About Arrays?
Random access memory.
Constant time access (O(1)) to any particular element in an array.
Constant time to access read and write
memory address = ArrayName+index*sizeof(datatype)
Creating
two
arrays
using
pointers
Copying the elements of array
Multidimensio
nal Array
Multidimensional Array
Multidi
mension
al Array
Multidimensional Array
Times for common operations
Summary
• Array: contiguous area of memory consisting of equal-size
elements indexed by contiguous integers.
• Constant-time access to any element.
• Constant-time to add/remove at the end.
• Linear time to add/remove at an arbitrary location.
Please get the concepts using visuals
https://visualgo.net/en/array
Linked List
An array is a very useful data structure provided in programming
languages. However, it has at least two limitations:
(1) its size has to be known at compilation time and
(2) the data in the array are separated in computer memory by
the same distance, which means that inserting an item inside the
array requires shifting other data in this array.

This limitation can be overcome by using linked structures.


Linked List
• A linked structure is a collection of nodes storing data and
links to other nodes.
• The most flexible implementation of linked list is by using
pointers.
Linked List

Node contains:
• Keys/Data/Content
• next pointer
Observation
• For fixed-size collections with known max limit of number of
items that will ever be needed, i.e., the max size of M, then
array is already a reasonably good data structure for List ADT
implementation.

• For variable-size collections with unknown size M and where


dynamic operations such as insert/remove are common, a
simple array is actually a poor choice of data structure.
Types of Linked List
1. Single/Linear Linked
List:

2. Doubly Linked List:

3. Circular List:

4. Doubly Circular List:


Singly Linked
List
The linked list is a linear
data structure where each
node has two parts.
1. Data
2. Reference to the next
node

Data - it can be any data


type. int,char,float,double
etc.
Reference Part - It will hold
the address of the next
node. So, it is a type pointer.
Operations on linked list
• Add to front • PushFront(Key)
• return front item • Key TopFront()
• remove front item • PopFront()
• add to back • PushBack(Key)
• return back item • Key TopBack()
• remove back item • PopBack()
• is key in list? • Boolean Find(Key)
• remove key front list • Erase(Key)
• empty list? • Boolean Empty()
• adds key before node • AddBefore(Node, Key)
• adds key after node • AddAfter(Node, Key)
Singly-linked list - Add to front operation
Singly-linked List
Add a new node to
front operation
• Time complexity O(1)
Singly-linked list;
Return front item
operation

• Delete/remove first
node
• Time complexity O(1)
Singly-linked list;
Return front item
operation

• Delete/remove first
node
• Time complexity O(1)
Add new node to tail
• add to back
(no tail pointer)

• Time complexity O(n)


add to back
operations
• Time complexity O(1)
(with tail pointer)
PopBack()
operations
• return back item/PopBack()
• Time complexity O(n)
(no tail pointer)
PopBack
operations
• return back item /PopBack()
• Time Complexity O(n)
(with tail pointer)
Operation;
Add new
node after a
node

new node
Doubly-Linked List
There is a way to make popping the back and adding before cheap.
Our problem was that although we had a way to get from a previous
element to the next element, we had no way to get back. And what a
doubly-linked list says is, well, let's go ahead and add a way to get
back. So we'll have two pointers, forward and back pointers.
Doubly-Linked List
Node contains:
• key
• next pointer
• previous pointer
PopBack()

Time complexity O(1)


PopBack()
Summary
• Constant time to insert at front or remove from the front.
• With tail and doubly-linked, constant time to insert at or
remove from the end.
• O(n) time to find arbitrary element.
• List elements need not be contiguous.
• With doubly-linked list, constant time to insert between nodes
or remove a node.
Circular Linked List
Try implementing all the functions (we discussed in the previous
slides) on a circular list.
Circular doubly linked list
Try implementing all the functions (we discussed in the previous
slides) on a circular doubly linked list.
Linear Search
Time Complexity: O(n)
Linear Search code - Array
#include <iostream> int main()
using namespace std; {
int array[] = { 12, 114, 0, 4, 9 };
int search(int array[], int n, int x) int x = 4;
{ int n = sizeof(array) / sizeof(array[0]);

// Going through array sequencially int result = search(array, n, x);


for (int i = 0; i < n; i++)
if (array[i] == x) (result == -1)
return i; ? cout << "Element not found"
return -1; : cout << "Element found at index: " <<
result;
}
}
Linear Search Code - Linked List
// Iterative C++ program to search
#include <iostream>
using namespace std;
int main() {
class Node { // Create a hard-coded linked list:
public: // 14 -> 21 -> 13 -> 30 -> 10
int data; Node* head = new Node(14);
Node* next; head->next = new Node(21);
Node(int new_data){ head->next->next = new Node(13);
data = new_data; head->next->next->next = new Node(30);
next = nullptr; } }; head->next->next->next->next = new
// Checks whether key is present in linked list Node(10);
bool searchKey(Node* head, int key) { int key = 14;
Node* curr = head; if (searchKey(head, key))
while (curr != NULL) { cout << "Yes";
if (curr->data == key) else
return true;
cout << "No";
curr = curr->next;}
return 0;}
return false;}
// Recursive C++ program to search
#include <iostream>
using namespace std;
struct Node { int main() {
int data; // Create a hard-coded linked list:
Node* next; // 14 -> 21 -> 13 -> 30 -> 10
struct Node* head = new Node(14);
Node(int new_data) {
head->next = new Node(21);
data = new_data;
head->next->next = new Node(13);
next = nullptr;}};
head->next->next->next = new Node(30);
// Checks whether the key is present in linked list
head->next->next->next->next = new Node(10);
bool searchKey(struct Node* head, int key) {
// Base case
// Key to search in the linked list
if (head == NULL) int key = 14;
return false;
// If key is present in current node, return true if (searchKey(head, key))
if (head->data == key) printf("Yes");
return true; else
// Recur for remaining list printf("No");
return searchKey(head->next, key);
} return 0;
}
Binary Search
Binary Search is a divide and conquer algorithm, so we have to
divide the list in half and keep doing until we find our required
element.
Time Complexity: O(log n)
Binary Search Code - Array
Binary Search Code - Array
On the next slide
#include <iostream> int main() {
using namespace std; // Initialize raw array
// Binary search for raw array int arr[] = {2, 4, 5, 7, 14, 17, 19, 22};
int binarySearch(const int arr[], int x, int x = 22;
int low, int high) {
while (low <= high) { // Perform binary search
int mid = low + (high - low) / 2; int result = binarySearch(arr, x, 0,
sizeof(arr) / sizeof(arr[0]) - 1);
if (arr[mid] == x)
return mid;
// Print result
else if (arr[mid] < x) if (result != -1)
low = mid + 1; cout << "Index: " << result << endl;
else else
high = mid - 1; cout << "Not found" << endl;
}
return -1; return 0;
} }
Binary Search - Linked List
Dry Run:

To find the middle node of the linked list


We will use two pointers approach in order to find middle of the
linked list. The two pointers will be slow and fast, slow will jump a
step( slow=slow→next) whereas fast will jump 2 steps( (fast =
fast→next→next). So, when fast reaches end, slow reaches
middle.
fast!=last (false)
so we will return slow as middle node
Binary Search - Linked List
Code:

I will attached the complete code cpp file on GCR along with the
this slide lecture.

You can check the snapshots of the function code on next slide
Interpolation Search
● Interpolation search is an improvement over binary search.
● Binary Search always checks the value at middle index. But,
interpolation search may check at different locations based on
the value of element being searched.
● For interpolation search to work efficiently the array
elements/data should be sorted and uniformly distributed.
Example if we are
looking for element 4 in
the given array
Code Example
I will attached the complete code cpp file on GCR along with the
this slide lecture.
Reference
https://www.cs.emory.edu/~cheung/Courses/170/Syllabus/09/cop
y-array.html
Book - “Data Structures and Algorithms in C++ by Adam
Drozdek”
All you need to have to achieve a
dream/goal/task!!
Three promises you have to make to yourself!
Watch full video!!
https://www.youtube.com/watch?v=uxoCnxlxpIk

You might also like