0% found this document useful (0 votes)
74 views43 pages

Understanding Data Structures and Algorithms

The document provides an overview of computer science, focusing on the importance of algorithms and data structures in programming. It explains the types of data structures, including primitive and non-primitive, and discusses concepts such as abstract data types, time and space complexity, and various searching and sorting algorithms. The document emphasizes the significance of efficient coding and the relationship between data structures and algorithms in creating effective programs.
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)
74 views43 pages

Understanding Data Structures and Algorithms

The document provides an overview of computer science, focusing on the importance of algorithms and data structures in programming. It explains the types of data structures, including primitive and non-primitive, and discusses concepts such as abstract data types, time and space complexity, and various searching and sorting algorithms. The document emphasizes the significance of efficient coding and the relationship between data structures and algorithms in creating effective programs.
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

Idea of cMomputer science

• Computer science deals with solving a


problem correctly in the form of
Algorithm which then can be converted
into a program, in most efficient time and
memory.

• Problem --> Solution(Algorithm) Program (Efficient)


• To write an efficient program we need knowledge of both Data Structures and
Algorithms.

• DATA STRUCTURE + ALGORITHM = PROGRAM’


Why we study data structure and algorithms
• Course objective is to teach you how to code efficiently.
• What is the meaning of efficiency (time, space, battery, system
buses, register etc) time is considered as most important.
• Better running time is obtained from the use of most appropriate data
structure and algorithms, rather than through removing a few
statements by clever coding.
What is data structure
• Data structure is a particular way of organizing data in a computer memory
(cache, main, secondary) so that Memory can be used efficiently both in terms
of time and space.
• It is a logical relationship existing between individual elements of data, it
considers elements stored and also their relationship to each other.
Data structure mainly specifies the following four things:

• Organization of data

• Accessing methods
Array
• Degree of association

• Processing methods

Link List
Array

Tree
Stack
Primitive data structure
• Primitive data structures are those which have predefined way of storing data by
the system. And the set of operations that can be performed on these data are
also predefined. They are directly operated upon by the machine instruction.
• Primitive data structures are char, int, float, double. The predefined operations
are addition, subtraction, etc.
Non-Primitive data structure
• Derived data structures are also provided by the system but are made using
primitives like an array. It can be array of chars, array of int, etc. The set of
operations that can be performed on derived data structures are also
predefined.

• Finally, there are user defined data types which the user defines using the
primitive and derived data types using language constructs like structure or class
and uses according to their needs. And the user has to define the set or
operations that we can perform on them. User defines data types are Linked
Lists, Trees, etc.
Array Link List

Stack Tree

Queue
Abstract Data Types
ADT set of all operations.
ADT specifies what is to be done but how it is to done is not specified i.e the
implementation details are hidden. Ex: stack ADT
Stack consisting of elements of same type of data, arranged in a sequential
order
Operations:
initialize()
push()
pop()
isEmpty()
isFull()
ADT is like blackbox which hides the inner structure and design of the data
type from the user.
Ex: stack ADT can be implemented using arrays and linked list
Why ADT

 The program which uses data structures is client program to run that program
we need interface to access it.

 The program which implements the data structure is know as the


implementation
Advantages
 If some wants to use stack in the program then he can simply use push and pop
operations without knowing its implementation.
 Previously the stack is implemented using linked list ,later the developer
changed to arrays then the client program will work in the same way without
being affected.
 ADT hides the information from the user means the user doesn’t bother of the
implementation they need only separation.
LINEAR DATA STRUCTURE NON-LINEAR DATA STRUCTURE
• In a linear data structure, data elements In a non-linear data structure, data
are arranged in a linear order where elements are attached in hierarchical
each and every element are attached to manner.
its previous and next adjacent.
• In linear data structure, single level is • Whereas in non-linear data structure,
involved. multiple levels are involved.

• Its implementation is easy in comparison


• While its implementation is complex in
to non- linear data structure.
comparison to linear data structure.
• In linear data structure, data
• While in non-linear data structure, data
elements can be traversed in a single
elements can’t be traversed in a single
run only.
run only.
• Its examples are :Trees and graphs.
• Its examples are: array,stack, queues.
• Homogeneous data: Homogeneous data structures are those data structures
that contain only similar type of data e.g. like a data structure containing only
integer or float values. The simplest example of such type of data structures is an
Array.

• Heterogeneous Data: Heterogeneous Data Structures are those data structures


that contains a variety or dissimilar type of data, for e.g. a data structure that
can contain various data of different data types like integer, float and character.
The examples of such data structures include structures, union etc.
Time And Space Complexity Analysis
Basing on the time and space complexity we can analyse the performance of the
algorithm.

Time complexity:
Amount of time that an algorithm requires for its execution

Space complexity:
Amount of memory space used by an algorithm/program for execution.
Time And Space Complexity Analysis

Methods to find time complexity:


[Link] based
[Link] notations

Frequency /count based:


• The step count method is one of the methods to analyze the
Time complexity of an algorithm. In this method, we count the
number of times each instruction is executed. Based on that we
will calculate the Time Complexity.
Asymptotic notations based:
Asymptotic Notations are mathematical tools that allow you to
analyze an algorithm's running time by identifying its behavior as
its input size grows.
Frequency /count based
• For Comments and declarations time count is 0.
• For Return and assignments the time count is 1.
• Ignore the lower order exponents when upper order exponents are
existing.
• Ignore constants.

• s=0;
• for(i=0;i<n; i++)
• {
• s= s+ a[i];
• }
• return s;
Asymptotic Notations

Asymptotic Notations are mathematical tools that allow you to analyze an


algorithm’s running time by identifying its behavior as its input size grows.

There are mainly three asymptotic notations:


[Link]-O Notation (O-notation): worst case
upper bound of algorithms running time(means maximum amount of
time ).
[Link] Notation (Ω-notation): best case
lower bound of algorithms running time(means minimum amount of
time) .
[Link] Notation (Θ-notation): average case
average bound of algorithms running time(average time).
Space Complexity
Space complexity refers to the total amount of memory space used by an
algorithm/program, including the space of input values for execution.

S(P)=C+SP

Problem dependent variable


constant
(independent variable) Ex: sum( int a, int n)
{
Ex: sum( int a, int b, int c) total=0;
{ for(i=0;i< n; i++)
a=10,b=20; {
c = a + b; total=total +a [i];
} }
}
Array
• An array is a is a data structure that stores collection of elements of
same type stored at contiguous memory locations and can be
accessed using an index.

int num[5];
How to declare an array in C

• datatype arrayName[array Size];


• int myarray[5];
How to initialize an array in C

• dataType arrayName[arraySize] = {value1, value2, ..., valueN};

• int myarray[5] = {1, 2, 3, 4, 5};

• You can also initialize an array like this.


• int myarray[] = {1, 2, 3, 4, 5};

• Here, we haven't specified the size. However, the compiler knows it’s size is 5 as
we are initializing it with 5 elements.
Change Value of Array elements

• int myarray[5] = {1, 2, 3, 4, 5};

• make the value of the third element to -1


• myarray[2] = -1;

• make the value of the fifth element to 0


• myarray[4] = 0;
• Arrays have several advantages, like:
• Efficient storage and retrieval: Arrays store elements in contiguous memory
locations, which makes it easy to retrieve elements using their index. So very
efficient with large amounts of data.
• Random access(fast access): Arrays allow access to individual elements using their
index, which means that accessing any element of the array takes the same amount
of time.
• Easy to sort and search: Arrays can be easily sorted and searched using algorithms
like binary search, which can be more efficient than searching through unsorted
data.
• Flexibility: Arrays can be used to represent a wide variety of data structures,
including stacks, queues etc.
• Easy to use: Arrays are a simple and easy-to-use data structure that can be easily
understood by programmers of all skill levels.
• Arrays also have some disadvantages, likes:
• Fixed size: In most programming languages, arrays have a fixed size that
cannot be changed once they are created. This can make it difficult to work
with data structures that need to grow or shrink dynamically(Internal
Fragmentation) (External Fragmentation).
• No built-in support for insertion or deletion: Inserting or deleting an
element in an array can be time-consuming and require shifting all the
elements after the insertion or deletion point.
• Homogeneous elements: Arrays can only store elements of the same type,
which can be limiting for many requirements.
• Poor performance for some operations: Some operations, such as searching
or inserting elements in a sorted array, can have poor performance compared
to other data structures like hash tables or binary search trees.
Searching Algorithms

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


element from any data structure where it is stored.
[Link] Search: In this, the list or array is traversed sequentially and
every element is checked.
For example: Linear Search.

[Link] Search: These algorithms are specifically designed for searching in


sorted data-structures. These type of searching algorithms are much more
efficient than Linear Search as they repeatedly target the center of the search
structure and divide the search space in half.
For Example: Binary Search.
Linear Search
Algorithm:

Step 1: start
Step 2: Start from the first element (index 0) and compare key with each
element (arr[i]).
Step 3: comparing key with first element arr[0]. Since not equal, the iterator
moves to the next element as a potential match.
Step 4: Now when comparing arr[2] with key, the value matches. So the
Linear Search Algorithm will yield a successful message and return the index
of the element when key is found.
Step 5: stop
int array[100], se, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter a number to search\n");
scanf("%d", &se);
for (i = 0; i < n; i++)
{
if (array[i] == se) /* If required element is found */
{
printf("%d is present at location %d.\n", se, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", search);
Binary Search
Algorithm:
Step 1: start
Step 2: Divide the search space into two halves by finding the middle
index “mid”.
Step 3: Compare the middle element of the search space with the key.
Step 4: If the key is found at middle element, the process is terminated.
Step 5: If the key is not found at middle element, choose which half will be
used as the next search space.
1. If the key is smaller than the middle element, then the left side is
used for next search.
2. If the key is larger than the middle element, then the right side is
used for next search.
Step 6: This process is continued until the key is found or the total search
space is exhausted.
Step 7: stop
int i, low, high, mid, n, s, arr[100],c=0;
printf("Enter array size (or) length ");
scanf("%d",&n);
printf("Enter %d elements ", n);
for(i =0;i<=n-1;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter searching element");
scanf("%d", &s);
low = 0; high = n-1;
while (first <= last)
{
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search)
{
printf("%d found at location %d.\n",search,middle+1);
c=1;
break;
}
else
last = middle - 1;
}
if (c==0)
printf("Not found! %d isn't present list.\n", search);
Sorting
A Sorting Algorithm is used to rearrange a given array or list of
elements according to a comparison operator on the elements. The
comparison operator is used to decide the new order of elements in
the respective data structure.
Characters is sorted in increasing order of their ASCII values. That is,
the character with a lesser ASCII value will be placed first than the
character with a higher ASCII value.
Types of sorting algorithms:
• Bubble sort
• Insertion sort
• Selection sort
• Quick sort
• Merge sort
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping
the adjacent elements if they are in the wrong order. This algorithm is not suitable
for large data sets as its average and worst-case time complexity is quite high.
int array[100], n, i, j, temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i= 0; i < n; i++)
scanf("%d", &array[i]);
for (i = 0 ; i < n - 1; i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
Insertion sort
Insertion sort is a simple sorting algorithm that works similarly to the way you
sort playing cards in your hands. The array is virtually split into a sorted and
an unsorted part. Values from the unsorted part are picked and placed in the
correct position in the sorted part.
int n, i, j, temp,arr[50];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
for (i = 1; i < n; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
Selection sort is a simple and efficient sorting algorithm that works by
repeatedly selecting the smallest (or largest) element from the unsorted
portion of the list and moving it to the sorted portion of the list.
Selection sort algorithm (for ascending order)
[Link] the minimum element in the array and swap it with the element in
the 1st position.
[Link] the minimum element again in the remaining array[2, n] and
swap it with the element at 2nd position, now we have two elements at
their correct positions.
[Link] have to do this n-1 times to sort the array.
int array[100], n, c, d, position, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);

You might also like