0% found this document useful (0 votes)
18 views12 pages

Unit 1

This document provides an overview of linear data structures, including definitions, classifications, and common types such as arrays, linked lists, stacks, and queues. It discusses searching techniques like linear and binary search, as well as sorting algorithms including bubble sort, selection sort, and insertion sort. Additionally, it covers abstract data types, algorithms, and the concepts of time and space complexity in relation to data structures.

Uploaded by

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

Unit 1

This document provides an overview of linear data structures, including definitions, classifications, and common types such as arrays, linked lists, stacks, and queues. It discusses searching techniques like linear and binary search, as well as sorting algorithms including bubble sort, selection sort, and insertion sort. Additionally, it covers abstract data types, algorithms, and the concepts of time and space complexity in relation to data structures.

Uploaded by

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

Unit-1

Introduction to Linear Data Structures :- Definition and importance of Linear Data Structures,
Abstract Data Types (ADT’s) and their Implementation, Time and Space Complexity. Searching
Techniques - Linear search & Binary search. Sorting Techniques - Bubble sort , Selection sort,
Insertion sort.

---------------------------------------------------------------------------------------------------------

Data Structures
A data structure is a particular way of storing and organizing data in
a computer so that it can be used efficiently

A data structure is not only used for organizing the data. It is also
used for processing, retrieving, and storing data.

There are different basic and advanced types of data structures that
are used in almost every program or software system that has been
developed.

Some common examples of data structures are arrays, linked lists,


queues, stacks, binary trees, and hash tables

Classification of Data Structures

Data structures are generally categorized into two classes: primitive and non-
primitive data structures .

Primitive Data Structures

Primitive data structures are the fundamental data types which are supported by a
programming language. Some basic data types are integer, real, character, and
boolean. The terms ‘data type, basic data type’, and ‘primitive data type’ are
often used interchangeably.

Non Primitive Data Structures


Non-primitive data structures can further be classified into two categories:
linear and non-linear data structures

Non-primitive data structures are those data structures which are created using
primitive data structures. Examples of such data structures include linked lists,
stacks, queues, trees, and graphs

Arrays: An array is a collection of similar data elements. These data elements have
the same data type. The elements of the array are stored in consecutive memory
locations and are referenced by an index (also known as the subscript).

Linked List: linked list is a dynamic data structure in which elements (called nodes)
form a sequential list

Stack: A stack is a linear data structure in which insertion and deletion of


elements are done at only one end, which is known as the top of the stack.

Queue: A Queue is a linear data structure in which insertion can be done at rear end
and deletion of elements can be dome at front end.

Tree: A tree is a non-linear data structure which consists of a collection of nodes


arranged in a hierarchical order
Graph: A graph is a non-linear data structure which is a collection of vertices (also
called nodes) and edges that connect these vertices

Importance of Data Structures

1. Data Structures help in the organization of data in a computer's memory.


2. Data Structures also help in representing the information in databases.
3. Data Structures allows the implementation of algorithms to search through data
(For example, search engine).
4. We can use the Data Structures to implement the algorithms to manipulate data
(For example, word processors).
5. We can also implement the algorithms to analyse data using Data Structures (For
example, data miners).
6. Data Structures support algorithms to generate the data (For example, a random
number generator).
7. Data Structures also support algorithms to compress and decompress the data
(For example, a zip utility).
8. We can also use Data Structures to implement algorithms to encrypt and decrypt
the data (For example, a security system).

Abstract Data Types (ADT’s) and their Implementation

An abstract data type (ADT) is a data structure that ignores the internal
representation but focus on how it performs it job. (or)

Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined
by a set of value and a set of operations.
Ex: stacks ADT and queues ADT. the user is concerned only with the type of data and
the operations that can be performed on it

We can implement both these ADTs using an array or a linked list

Advantages:
•Modification of a program is simple, For example, if you want to add a new field to
a student’s record to keep track of more information about each student, then it
will be better to replace an array with a linked structure to improve the program’s
efficiency.

•In such a scenario, rewriting every procedure that uses the changed structure is
not desirable. Therefore, a better alternative is to separate the use of a data
structure from the details of its implementation.

ALGORITHM
Algorithm is step by step logical procedure for solving a problem.

In Algorithm each step is called Instruction.

An Algorithm is any well-defined computational procedure that take some values as


inputs and produce some values as output.

An Algorithm is a sequence of computational steps that transform input into output.

An Algorithm has 5 basic properties:

1. Input: An Algorithm has take ‘0’ or more number of inputs that can be supplied
as externally.
2. Output: An Algorithm must produce at least one output.
3. Definiteness: Each instruction in the algorithm must be clear.
4. Finiteness: An algorithm must terminate after a finite number of steps .
5. Effectiveness: Each operation should be effective. i.e the operations must be
terminate after finite amount of time.

Efficiency of Algorithm

The efficiency of an algorithm can be computed by measuring the performance of an


algorithm. We can measure the performance of an algorithm in Two(2) ways.

Time Complexity

Space Complexity

Time Complexity

The time complexity of an algorithm is the amount of computing time required by an


algorithm to run its completion.

There are 2 types of computing time 1. Compile time 2. Run time


The time complexity generally computed at run time (or) execution time.

The time complexity can be calculated in terms of frequency count.

Frequency count is a count denoting the number of times the statement should be
executed.

The time complexity can be calculated as

Comments – 0

Assignment / return statement – 1

Conditional (or) Selection Constructs – 1

Example 1: Sum of the elements in an Array

Statements Step count/ Execution Frequency Total Steps


Algorithm Addition (A,n) 0 - 0
{ 0 - 0
//A is an array of size ‘n’ 0 - 0
Sum :=0; 1 1 1
for i:=1 to n do 1 n+1 n+1
Sum:=Sum+A[i]; 1 n n
return Sum; 1 1 1
} 0 - 0
Total 2n+3

Example 2: Subtraction of two matrices

Statements Step count/ Execution Frequency Total Steps


Algorithm Subtract (A,B,C,m,n) 0 - 0
{ 0 - 0
for i:=1 to m do 1 m+1 m+1
for j:=1 to n 1 m(n+1) mn+m
do 1 mn mn
C[i,j] := A[i,j] – B[i,j]; 0 - 0
}
Total 2mn+2m+1

The Notations of Time complexities are

Worst Case: the longest running time performed by an algorithm for the given input for

solving a problem is called worst case. It is denotes by Big–oh ( O)


Average Case: the average running time performed by an algorithm for the given input
for solving a problem is called avergae case. It is denotes by omega ( )

Best Case: the shortest running time performed by an algorithm for the given input for
solving a problem is called best case. It is denotes by omega ( )

Space Complexity

• Space Complexity can be defined as amount of memory (or) space required by an


Algorithm to run.

• To compute the space complexity we use 2 factors i. Constant ii. Instance


characteristics.

• The space requirement S(p) can be given as S(p) = C+Sp

Where C- Constant, it denotes the space taken for input and output.

Sp – Amount of space taken by an instruction, variable and identifiers

Sorting
It is a technique used to arrange a given array or list of unordered elements into an
order (i.e Ascending or Descending)

These are the main types of sorting in data structures

 Bubble sort
 Selection sort
 Insertion sort
 Merge sort
 Quick sort
 Heap sort
 Radix sort
 Bucket sort

Insertion Sort: Insertion sort is a simple sorting algorithm that works by iteratively
inserting each element of an unsorted list into its correct position in a sorted
portion of the list. It is like sorting playing cards in your hands

You split the cards into two groups: the sorted cards and the unsorted cards. Then,
you pick a card from the unsorted group and put it in the right place in the sorted
group.

 We start with second element of the array as first element in the array is
assumed to be sorted.

 Compare second element with the first element and check if the second element
is smaller then swap them.
 Move to the third element and compare it with the first two elements and put
at its correct position

 Repeat until the entire array is sorted.

Example

void insertionSort(int arr[], int n)


{
for (int i = 1; i < n; ++i)
{
int key = arr[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are greater than key, to one
position ahead of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

Time Complexities of Insertion Sort are

Best case: O(n),If the list is already sorted, where n is the number of elements in
the list.

Average case: O(n2), If the list is randomly ordered

Worst case: O(n2), If the list is in reverse order

Bubble Sort: It is the simplest sorting algorithm that works by repeatedly


swapping the adjacent elements if they are in the wrong order.
We sort the array using multiple passes. After the first pass, the maximum element
goes to end (its correct position). Same way, after second pass, the second largest
element goes to second last position and so on.

 In every pass, we process only those elements that have already not moved to
correct position. After k passes, the largest k elements must have been moved
to the last k positions.

 In a pass, we consider remaining elements and compare all adjacent and swap
if larger element is before a smaller element. If we keep doing this, we get
the largest (among the remaining elements) at its correct position.

Example

void bubbleSort(int arr[], int n)


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

Note: Time Complexity In all cases worst, best and average is O(n2)

Selection Sort: It is a comparison-based sorting algorithm. It sorts an array by


repeatedly selecting the smallest (or largest) element from the unsorted portion
and swapping it with the first unsorted element. This process continues until the
entire array is sorted

 First we find the smallest element and swap it with the first element. This
way we get the smallest element at its correct position.

 Then we find the smallest among remaining elements (or second smallest) and
swap it with the second element.

 We keep doing this until we get all elements moved to correct position.

Example
void selectionSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
// Assume the current position holds the minimum element
int min_idx = i;
//Iterate through the unsorted portion to find the actual minimum
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{

// Update min_idx if a smaller element is found


min_idx = j;
}
}
// Move minimum element to its correct position
int temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}

Note: The time complexity of Selection sort is: O(n2) ,as there are two nested loops

Searching
Searching is the process of finding a specific element or item within a
collection of data. This collection of data can take various forms, such as
arrays, lists, trees, or other structured representations.

Searching Algorithms are designed to check for an element or retrieve an element


from any data structure where it is stored

some searching algorithms are


Linear Search
Binary Search
Linear Search
Linear Search, also known as Sequential Search, is one of the simplest and most
straightforward searching algorithms. It works by sequentially examining each element
in a collection of data(array or list) until a match is found or the entire collection
has been traversed.
Time Complexity:

Best Case: In the best case, the key might be present at the first index. So the best
case complexity is O(1)

Worst Case: In the worst case, the key might be present at the last index i.e.,
opposite to the end from which the search has started in the list. So the worst-case
complexity is O(N) where N is the size of the list.

Average Case: O(N)

Binary Search
Binary Search is defined as a searching algorithm used in a sorted array by repeatedly
dividing the search interval in half. The idea of binary search is to use the
information that the array is sorted and reduce the time complexity

Time Complexity:

Best Case: O(1) - When the key is found at the middle element.

Worst Case: O(log N) - When the key is not present, and the search space is
continuously halved.

Average Case: O(log N)

You might also like