0% found this document useful (0 votes)
15 views16 pages

Ds Unit-1

The document provides an introduction to linear data structures, explaining their importance, classification, and key concepts such as Abstract Data Types (ADTs). It covers various linear data structures like arrays, linked lists, stacks, and queues, along with their time and space complexities, searching techniques, and sorting algorithms. Additionally, it includes examples of linear and binary search algorithms, as well as bubble sort implementation.

Uploaded by

amaramarnadh007
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)
15 views16 pages

Ds Unit-1

The document provides an introduction to linear data structures, explaining their importance, classification, and key concepts such as Abstract Data Types (ADTs). It covers various linear data structures like arrays, linked lists, stacks, and queues, along with their time and space complexities, searching techniques, and sorting algorithms. Additionally, it includes examples of linear and binary search algorithms, as well as bubble sort implementation.

Uploaded by

amaramarnadh007
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/ 16

INTRODUCTION TO LINEAR DATA STRUCTURES

Data Structure:
A data structure is a specialized format for organizing, storing, and managing data in a computer so that it
can be accessed and modified efficiently. It defines the way data is arranged and the operations that can be
performed on it.
Importance:
Data structures are essential because they:
• Help in organizing data efficiently.
• Allow quick access and modification of data.
• Improve the performance of algorithms.
• Play a crucial role in solving complex computational problems.
Classification of Data Structures
Data structures are broadly classified into two types:
1. Linear Data Structures – Elements are arranged sequentially (e.g., Arrays, Linked Lists, Stacks,
Queues).
2. Non-Linear Data Structures – Elements are arranged in a hierarchical manner (e.g., Trees, Graphs).

RC1
Linear Data Structures
A data structure is considered linear if its elements form a sequence. The elements of a linear data structure
can be stored in:
• Contiguous memory locations (using arrays)
• Non-contiguous memory locations (using linked lists, where each element contains a pointer to the
next element)
Importance of Linear Data Structures
1. Simplicity and Easy Access – Easier to understand and manipulate.
2. Efficient Traversal – Simple to iterate through elements.
3. Data Integrity – Maintains ordered relationships among elements.
4. Memory Efficiency – Optimized memory usage in certain implementations.
5. Real-World Applications – Used in software applications, databases, and operating systems.
6. Ease of Algorithm Implementation – Fundamental to algorithm design.
7. Flexibility – Can be implemented dynamically (e.g., linked lists).

Abstract Data Type (ADT)


An Abstract Data Type (ADT) is a conceptual model that defines a set of operations and behaviors for a
data structure without specifying its implementation details. ADTs focus on what a data structure does,
rather than how it does it.
For example, a Stack ADT defines operations like push(), pop(), and peek() but does not specify whether the
stack is implemented using an array or a linked list.
Key Concepts of ADT
1. Abstraction
o ADTs hide implementation details and only expose necessary operations.
o Users can use ADTs without knowing how they are implemented.
2. Encapsulation
o ADTs bundle data and operations into a single unit.
o Internal details are hidden, and only the defined operations can be used.
3. Modularity
o ADTs help in designing reusable and maintainable code.
4. Data Structure Independence
o The same ADT can be implemented using different data structures, such as arrays or linked
lists.
RC2
Examples of ADTs
1. List ADT
o A collection of elements in a sequence.
o Operations: insert(), remove(), get(), size(), isEmpty(), etc.
2. Stack ADT (LIFO - Last In, First Out)
o Allows insertion and deletion only from the top.
o Operations: push(), pop(), peek(), isEmpty(), etc.
3. Queue ADT (FIFO - First In, First Out)
o Elements are added at the rear and removed from the front.
o Operations: enqueue(), dequeue(), peek(), isEmpty(), etc.

Advantages of ADTs
Encapsulation – Keeps data secure by hiding implementation details.

Abstraction – Users work with data without worrying about implementation.

Reusability – The same ADT can be used in multiple programs.

Modularity – Code becomes well-structured and easy to debug.


Disadvantages of ADTs
Overhead – Additional memory and processing may be required.

Complexity – Implementing ADTs can be tricky for large systems.

Limited Flexibility – Some ADTs may not fit all scenarios.

RC3
Time and Space Complexity Analysis for Linear Data Structures
Linear data structures, such as arrays, linked lists, stacks, and queues, require analyzing their time
complexity (how execution time increases with input size) and space complexity (how memory usage grows
with input size).

Time Complexity Table for Linear Data Structures

Operation Array Singly Linked Doubly Linked Stack Queue


List List
Access O(1) (Direct index O(n) O(n) O(n) O(n)
(Read/Write) access) (Sequential (Sequential
access) access)
Search (Linear O(n) O(n) O(n) O(n) O(n)
Search)
Insertion O(n) (Shifting O(1) O(1) O(1) (Push O(1) (Enqueue
(Beginning) needed) operation) operation)
Insertion O(n) (Shifting O(n) O(n) N/A N/A
(Middle) needed)
Insertion (End) O(1) (If capacity O(n) (Traversal O(1) (Tail O(1) O(1)
allows) / O(n) (If required) pointer
resizing needed) available)
Deletion O(n) (Shifting O(1) O(1) O(1) (Pop O(1) (Dequeue
(Beginning) needed) operation) operation)
Deletion O(n) (Shifting O(n) O(n) N/A N/A
(Middle) needed)
Deletion (End) O(1) O(n) O(1) (Tail O(1) O(1)
pointer
available)

RC4
Space Complexity Table for Linear Data Structures

Data Structure Space Complexity


Array O(n)
Singly Linked List O(n)
Doubly Linked List O(n)
Stack (Using Array) O(n)
Stack (Using Linked List) O(n)
Queue (Using Array) O(n)
Queue (Using Linked List) O(n)

Searching Techniques: Linear and Binary Search

Linear Search
Linear search is a method of finding a particular value in a list by checking each element one by one
sequentially until the desired element is found or the list is exhausted.
Algorithm:
1. Start
2. Take the input list/array and the search element.
3. Iterate through each element in the list:
o Compare the current element with the search element.
o If they match, return the index (position) of the element.
o If not, continue to the next element.
4. If the element is not found after checking all elements, return "Element not found."
5. End

Example
Consider the following list of elements and the search element:
Search Element: 12

RC5
Index 0 1 2 3 4 5 6 7

Values 65 20 10 55 32 12 50 99

Steps of Linear Search


Step 1: Compare search element 12 with the first element (index 0).
12 == 65 → Not Matched → Move to next element.
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99

Step 2: Compare search element 12 with the second element (index 1).
12 == 20 → Not Matched → Move to next element.
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99
Step 3: Compare search element 12 with the third element (index 2).
12 == 10 → Not Matched → Move to next element.
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99

Step 4: Compare search element 12 with the fourth element (index 3).
12 == 55 → Not Matched → Move to next element.
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99

Step 5: Compare search element 12 with the fifth element (index 4).
12 == 32 → Not Matched → Move to next element.
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99

Step 6: Compare search element 12 with the sixth element (index 5).
12 == 12 → Matched! → Stop searching and display the result.

RC6
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99

Element 12 found at index 5.

Code:
#include <stdio.h>

void linearSearch(int arr[], int size, int key) {


int found = 0;
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
printf("Element found at index %d\n", i);
found = 1;
}
}
if (!found) {
printf("Element not found\n");
}
}

void main() {
int size, key;

printf("Enter the size of the array: ");


scanf("%d", &size);

int arr[size];

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the element to search: ");


scanf("%d", &key);

linearSearch(arr, size, key);


}

Binary Search
Binary search is an efficient searching technique used to check whether an element is present in a sorted
list. It repeatedly divides the search interval in half to locate the element.

RC7
Algorithm:
1. Start
2. Take the input sorted list/array and the search element.
3. Set low = 0 and high = size of array - 1.
4. Repeat until low ≤ high:
o Find the mid index: mid=(low+high)/2
o Compare arr[mid] with the search element:
▪ If arr[mid] == search element, return mid (element found).
▪ If arr[mid] < search element, search the right half (low = mid + 1).
▪ If arr[mid] > search element, search the left half (high = mid - 1).
5. If the element is not found, return "Element not found."
6. End

Example
Consider the sorted list:
Index 0 1 2 3 4 5 6 7 8
Values 3 5 7 9 10 16 21 35 36

Search Element: 35

Steps of Binary Search


Step 1: Find the Midpoint
• Low index = 0
• High index = 8
• Mid index = (0 + 8) / 2 = 4
• Mid Value = 10
Index 0 1 2 3 4 5 6 7 8
Values 3 5 7 9 10 16 21 35 36

Comparison: 35 > 10 → Search in the right sublist {16, 21, 35, 36}.

Step 2: Find the New Midpoint


• Low index = 5
• High index = 8
• Mid index = (5 + 8) / 2 = 6
RC8
• Mid Value = 21
Index 0 1 2 3 4 5 6 7 8
Values 3 5 7 9 10 16 21 35 36

Comparison: 35 > 21 → Search in the right sublist {35, 36}.

Step 3: Find the New Midpoint


• Low index = 7
• High index = 8
• Mid index = (7 + 8) / 2 = 7
• Mid Value = 35
Index 0 1 2 3 4 5 6 7 8
Values 3 5 7 9 10 16 21 35 36

Comparison: 35 == 35 → Match found at index 7!

Code:
#include <stdio.h>

void binarySearch(int arr[], int size, int key) {


int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
printf("Element found at index %d\n", mid);
return;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
printf("Element not found\n");
}

void main() {
int size, key;

printf("Enter the size of the array: ");


scanf("%d", &size);

int arr[size];

printf("Enter %d sorted elements: ", size);


for (int i = 0; i < size; i++) {

RC9
scanf("%d", &arr[i]);
}

printf("Enter the element to search: ");


scanf("%d", &key);

binarySearch(arr, size, key);


}

Sorting Techniques
Sorting is the process of arranging elements in a specific order (ascending or descending). Below are three
fundamental sorting techniques:

Bubble Sort
• Bubble Sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps
them if they are in the wrong order.
• It "bubbles" the largest element to the end in each pass.
• The process repeats until the array is completely sorted.
Algorithm:
1. Start from the first element.
2. Compare it with the next element.
3. Swap if the first element is greater than the second.
4. Repeat this process for the entire array.
5. Reduce the range of comparison in each pass.
6. Stop when no swaps occur in a pass.
Time Complexity:
• Best Case: O(n)(already sorted)
• Worst/Average Case: O(n2)

Example: Sorting [8, 12, 2, 4, 11]


Initial Array:
8 12 2 4 11

Pass 1:
Compare 8 & 12 → No swap

RC10
8 12 2 4 11

Compare 12 & 2 → Swap


8 2 12 4 11

Compare 12 & 4 → Swap


8 2 4 12 11

Compare 12 & 11 → Swap


8 2 4 11 12

Pass 2:
Compare 8 & 2 → Swap
2 8 4 11 12

Compare 8 & 4 → Swap


2 4 8 11 12

Compare 8 & 11 → No swap


2 4 8 11 12

Pass 3:
Compare 2 & 4 → No swap
2 4 8 11 12

Compare 4 & 8 → No swap


2 4 8 11 12
Since no swaps were made in this pass, the sorting process stops.

Final Sorted Array:


Sorted Elements: [2, 4, 8, 11, 12]

Code:
#include <stdio.h>

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
RC11
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);


for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

bubbleSort(arr, n);

printf("Sorted array using Bubble Sort: ");


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}

Selection Sort
Selection Sort is a simple sorting algorithm that repeatedly selects the smallest element from the unsorted
part and moves it to the correct position.
• Finds the minimum element and places it at the beginning.
• Repeats the process for the remaining unsorted part.

Algorithm:
1. Find the smallest element in the unsorted part.
2. Swap it with the first element of the unsorted part.
3. Repeat for the remaining elements until the array is sorted.

Time Complexity:
• Best Case: O(n2)
• Worst Case: O(n2)
• Average Case: O(n2)
RC12
Example: Sorting [8, 12, 2, 4, 11]

Initial Array:
8 12 2 4 11

Pass 1:
Find the minimum element in [8, 12, 2, 4, 11] → 2

Swap 2 with 8
2 12 8 4 11

Pass 2:
Find the minimum element in [12, 8, 4, 11] → 4

Swap 4 with 12
2 4 8 12 11

Pass 3:
Find the minimum element in [8, 12, 11] → 8

8 is already in the correct position (no swap needed)


2 4 8 12 11

Pass 4:
Find the minimum element in [12, 11] → 11

Swap 11 with 12
2 4 8 11 12

Final Sorted Array:


Sorted Elements: [2, 4, 8, 11, 12]

Code:
#include <stdio.h>

void selectionSort(int arr[], int n) {


RC13
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

void main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);


for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

selectionSort(arr, n);

printf("Sorted array using Selection Sort: ");


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}

Insertion Sort
Insertion Sort is a simple sorting algorithm that builds the final sorted array one element at a time. It works
similarly to how we sort playing cards in our hands:
• Takes one element at a time and inserts it into its correct position.
• Moves larger elements one position to the right to make space for the new element.

Algorithm:
1. Start from the second element (index 1).
2. Compare it with previous elements.
3. Shift larger elements to the right.
4. Insert the current element at the correct position.
5. Repeat for all elements.

RC14
Time Complexity:
• Best Case: O(n) (if already sorted)
• Worst/Average Case: O(n2)

Example: Sorting [8, 12, 2, 4, 11]


Initial Array:
8 12 2 4 11

Pass 1:
Take the second element (12), compare with 8

12 > 8 → No change
8 12 2 4 11

Pass 2:
Take the third element (2), compare with 8 and 12

Insert 2 at index 0
2 8 12 4 11

Pass 3:
Take the fourth element (4), compare with 2,8 and 12

Insert 4 at index 1
2 4 8 12 11

Pass 4:
Take the fifth element (11), compare with 2,4,8 and 12

Insert 11 at index 3
2 4 8 11 12

Final Sorted Array:


Sorted Elements: [2, 4, 8, 11, 12]

RC15
Code:
#include <stdio.h>

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

void main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);


for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

insertionSort(arr, n);

printf("Sorted array using Insertion Sort: ");


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}

RC16

You might also like