Data Structure and
Algorithms
Using C Programming
INTRODUCTION
A Program is said to be efficient when it executes in minimum
time and with minimum memory space. In order to write efficient
programs we need to apply certain data management concepts.
The concept of data management is a complex task that includes
actives like data collection, organization of data into appropriate
structures, and developing and maintaining routines for quality
assurance.
Data structure is a crucial part of data management
INTRODUCTION
A data structure is basically a group of data elements that are put
together under one name, and which defines a particular way of
storing and organizing data in a computer so that it can be used
efficiently.
Some common examples of data structures are arrays, linked lists,
queues, stacks, binary trees, and hash tables.
Classification of Data Structures
Data Structure are generally categorized into two classes:
1. Primitive
2. Non-primitive Data Structure
Classification of Data Structures
Primitive Data Structure:
Primitive Data Structure are the fundamental data types
which are supported by a programming language.
Some basic data types are integer, real character, and
Boolean.
Classification of Data Structures
Non-Primitive data structure:
Non-primitive Data Structure are those Data Structure
which are created using primitive Data Structure.
Non-primitive Data Structure can further be classified into
two categories: linear and non-linear Data Structure.
Classification of Data Structures
Linear Data Structure:
If the elements of a Data Structure are stored in a liner or
sequential order, then it is a liner data structure.
Examples: arrays, linked lists, Stacks, and queues.
0 1 2 3
4 5 6 7
Classification of Data Structures
Non-Linear Data Structure:
If the elements of a Data Structure are not stored in a
sequential order, then it is a non-linear Data Structure
Examples: trees and graphs.
Operation on Data Structure
Traversing:
It means to access each data item exactly once so that it can
be proceeded.
For example, to print the names of all the students in a class.
Searching:
It is used to find the location of one or more data items that
satisfy the given constraint. Such a data item may or may not
be present in the given collection of data items.
For example, to find the names of all the students who
secured 100 mark in mathematics
Operation on Data Structure
Inserting:
It is used to add new data items to the given list of data items.
For example, to add the details of a new student who has
recently joined the course
Deleting:
It means to remove (delete) a particular data item from the
given collection of data items.
For example, to delete the name of a student who has left the
course.
Operation on Data Structure
Sorting:
Data items can be arranged in some order like ascending
order or descending order depending on the type of
application.
For example, arranging the names of students in a class in an
alphabetical order.
calculating the top three winners by arranging the
participants’ scores in descending order and then extracting
the top three.
Merging:
Lists of two sorted data items can be combined to from a
single list of sorted data items.
ARRAY
An array is a collection of elements, all of the same type, stored in
contiguous memory locations.
• Bellow arrays consist of 10 data values:
a 5 2 84 0 25 1 43 3 9 6
b
‘a’ ‘b’ ‘c’ ‘d’ ‘e’ ‘f’ ‘g’ ‘h’ ‘I’ ‘j’
C ‘a’ ‘b’ 84 ‘d’ ‘e’ 1 43 3 9 6
Declaration and Definition of 1D
Array:
Syntax:
type name of the array[size];
Data Type- the kind of values it can store. Ex : int, char, float,
double.
Name- to identify the array.
Size- the maximum number of values that the array can hold.
Example: an array can be declared as follows:
int marks[5];
Size = 5 * 4bytes = 20 bytes
marks
Compiler allocate a consecutive block of memory size = n * sizeof(datatype).
Initialization of 1D Array:
Method 1:
int arr[5] = {10, 20, 30, 40, 50};
10 20 arr 30
= 40 50
0 1 2 3 4
Initialization of 1D Array:
Method 2:
int arr[ ] = {10, 20, 30, 40, 50};
10 20 arr 30
= 40 50
0 1 2 3 4
Initialization of 1D Array:
Method 3:
int arr[5] ;
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
10 20 arr30
= 40 50
0 1 2 3 4
Initialization of 1D Array:
Method 4:
int arr[5];
for (i=0; i<5; i++){
scanf(“%d”,&arr[i]);
}
85 37 arr 30
= 78 92
0 1 2 3 4
🤔
What if number of elements are lesser than the length?
int arr[5] = {11, 12, 13}
0 1 2 3
🤔
What if number of elements are lesser than the length?
int arr[5] = {11, 12, 13}
11 12 13
0 1 2 3
The remaining blocks of the array are filled by value null(0).
🤔
What if number of elements are lesser than the length?
int arr[5] = {11, 12, 13}
11 12 13 \0 \0
0 1 2 3
The remaining blocks of the array are filled by value null(0).
Accessing Elements from 1D
Array:
Syntax:
array_name[index_value];
Example:
Access the first element by: = 85
marks[0]
Access the second element by: marks[1] = 37
And so on……
Coding Exercise - 1
Write a program to print the following numbers in reverse order:
85, 45, 62, 23, 74, 94, 90, 28, 23,58
Assume that all these numbers are stored in an array.
Coding Exercise - 2
Write a program to check whether any of the digits in a
number appears more than once:
Example:
number = 586418
OPERATIONS ON ARRAYS
Traversing an array.
Inserting an element in an array.
Searching an element from an array.
Deleting an element from an array.
Merging two arrays
Sorting an array in ascending or descending order.
OPERATIONS ON ARRAYS
Traversing an array.
Inserting an element in an array.
Searching an element from an array.
Deleting an element from an array.
Merging two arrays
Sorting an array in ascending or descending order.
OPERATIONS ON ARRAYS
Traversing an array:
Traversing an array means accessing each and every element of
the array for a specific purpose.
Algorithm:
Step 1: [INITIALIZATION] Set I = lower_bound
Step 2: Repeat Steps 3 to 4 while I < = upper_bound
Step 3: Apply Process to A[I]
Step 4: SET I = I + 1
[End of loop]
Step 5: EXIT
OPERATIONS ON ARRAYS
Inserting an element in an array :
Adds an element at a specific index.
Algorithm:
Step 1: [INITIALIZATION] Set I = SIZE
Step 2: Repeat Steps 3 to 4 while I >= POS
Step 3: SET A[I+1] =A[I]
Step 4: SET I = I - 1
[End of loop]
Step 5: SET SIZE = SIZE + 1
Step 6: SET A[POS] = VAL
Step 7: EXIT
OPERATIONS ON ARRAYS
Deleting an element from an array:
Deleting an element from an array means removing a data
element from an already existing array.
Algorithm:
Step 1: [INITIALIZATION] Set I = POS
Step 2: Repeat Steps 3 to 4 while I <= SIZE - 1
Step 3: SET A[I] =A[I+1]
Step 4: SET I = I + 1
[End of loop]
Step 5: SET SIZE = SIZE – 1
Step 6: EXIT