Unit 1
Unit 1
Syllabus:
• Data Structures - Definition, Classification of Data Structures, Operations on Data Structures, Abstract
Data Type (ADT).
• Searching-Linear search,Binary search,
• Sorting-Insertion sort,Selection sort,Exchange (Bubblesort), algorithms.
INTRODUCTION:
A data structure is 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
To day computer programmers do not write programs just to solve a problem but to write an
efficient program.
When selecting a data structure to solve a problem, the following steps must be performed.
1. Analys is of the problem to determine the basic operations that must be supported.
2. Quantify the resource constraints for each operation.
3. Select the data structure that best meets the requirements.
The term data means a value or set of values.It specifies either the value of a variable or a
constant (e.g., marks of students, name of an employee, address of a customer, value of pi, etc.).
A record is a collection of data items.For example, the name,address,course,and marks obtained
are individual data items. But all these data items can be grouped together to form a record.
A file is a collection of related records.For example,if there are 60 students in a class,then there
are 60 records of the students. All these related records are stored in a file.
Data structures are generally categorized into two classes: primitive and non-primitive data
structures.
Primitiveand Non-primitiveDataStructures:
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 are those data structures which are created using primitive data
structures. Examples of such data structures include linked lists, stacks, trees, and graphs.
Non-primitive data structures can further be classified into two categories: linear and non-linear
Data structures.
LinearandNon-linear Structures:
If the elements of a data structure are stored in a linear or sequential order,then it is a linear data
structure.
o Examples include arrays,linked lists,stacks,and queues.
o Linear data structures can be represented in memory in two different ways. One way is
to have to a linear relationship between elements by means of sequential memory
locations. The other way is to have a linear relationship between elements by means of
links.
If the elements of a data structure are not stored in a sequential order, then it is a non-linear data
structure.
o The relationship of adjacency is not maintained between elements of a non-linear data
structure. Examples include 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).
In C, arrays are declared using the following syntax: data type name[size];
Ex: int marks[10];
limitations:
o Arrays are of fixed size.
o Data elements are stored in contiguous memory locations which may not be always available.
o Insertion and deletion of elements can be problematic because of shifting of elements from their
positions.
LinkedLists:
Linked list is a dynamic data structure in which elements(called nodes)form a sequential list.
In a linked list, each node is allocated space as it is added to the list. Every node in the list points
to the next node in the list.
Every node contains the following
The value of the node or any other data that corresponds to that node
A pointer or link to the next node in the list
The first node in the list is pointed by Head/Start/First. The last node in the list contains a NULL
pointer to indicate that it is the end or tail of the list.
Advantage: Easier to insert or delete data elements
Disadvantage: Slow search operation and requires more memory space
Stacks:
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.
Stack is called a last-in, first-out (LIFO)
structure because the last element which is
added to the stack is the first element
which is deleted from the stack.
Stacks can be implemented using arrays or
linked lists.
Every stack has a variable top associated
with it. Top is used to store the address of
the topmost element of the stack.
It is this position from where the element will be added or deleted.There is another variable
MAX, which is used to store the maximum number of elements that the stack can store.
If top=NULL, then it indicates that the stack is empty and if top=MAX–1,then the stack is full.
A stack supports three basic operations: push, pop, and peep.The push operation adds an
element to the top of the stack.The pop operation removes the element from the top of the stack
and the peep operation returns the value of the topmost element of the stack (without deleting
it).
Queues:
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.
A queue is a first-in, first-out (FIFO) data
structure in which the element that is
inserted first is the first one to be taken
out.
Like stacks, queues can be implemented by using either arrays or linked lists.
A queue is full when rear = MAX – 1, An underflow condition occurs when we try to delete an
element from a queue that is already empty. If front = NULL and rear = NULL, then there is no
element in the queue.
Trees:
A tree is a non-linear data structure which consists of a collection of nodes arranged in a
hierarchical order.
One of the nodes is designated as the root node, and the remaining nodes can be partitioned into
disjoint sets such that each set is a sub-tree of the root
The simplest form of a tree is a binary tree. A binary tree
consists of a root node and left and right sub-trees, where
both sub-trees are also binary trees.
Each node contains a data element, a left pointer which
points to the left sub-tree, and a right pointer which points to
the right sub-tree.
The root element is the topmost node which is pointed by a
‘root’ pointer. If root = NULL then the tree is empty.
Here R is the root node and T1 and T2 are the left and right sub trees of R. If T1 is non-empty,
then T1 is said to be the left successor of R. Likewise, if T2 is non-empty, then it is called the
right successor of R.
Advantage: Provides quick search, insert, and delete operations
Disadvantage: Complicated deletion algorithm
Graphs:
A graph is a non-linear data structure which is a collection of vertices (also called nodes) and
Edges that connect these vertices.
A node in the graph may represent a city and the edges
connecting the nodes can represent roads.
A graph can also be used to represent a computer network where
the nodes are workstations and the edges are the network
connections.
Graphs do not have any root node. Rather, every node in the graph can be connected with every
another node in the graph.
Advantage: Best models real-world situations
Disadvantage: Some algorithms are slow and very complex
This section discusses the different operations that can be performed on the various data
structures previously mentioned.
Traversing It means to access each data item exactly once so that it can be processed. 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 marks in mathematics.
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.
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, or 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 form a single list of sorted data items.
ABSTRACT DATA TYPE:
An abstract data type (ADT)is a data structure, focusing on what it does and ignoring how it
does its job. (or) Abstract Data type (ADT) is a
Type (or class) for objects whose behavior
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.
Advantage of using ADTs
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.
SEARCHING:
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.
Linear search is a technique which traverses the arrays equentially to locate given item or search
element.
In Linear search, we access each element of an array one by one sequentially and see weather it
is desired element or not. We traverse the entire list and match each element of the list with the
item whose location is to be found. If the match found then location of the item is returned
otherwise the algorithm return NULL.
A search is successful then it will return the location of desired element
If A search will un successful if all the elements are accessed and desired element not found.
Linear search is mostly used to search an unordered list in which the items are not
Step 4 – If both are not matched, then compare search element with the next element in the list.
Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.
Step6-Iflastelementinthelistalsodoesn'tmatch,then display "Element is not found!!!"and terminate the
function.
Example:
Consider the following list of elements and the element to be searched...
BINARYSEARCH:
• Binary search is the search technique which works efficiently on the sorted lists. Hence, in
order to search an element into some list by using binary search technique, we must ensure that
the list is sorted.
• Binary search follows divide and conquer approach in which, the list is divided into two halves
and the item is compared with the middle element of the list. If the match is found then, the
location of middle element is returned otherwise, we search into either of the halves depending
upon the result produced through the match.
Algorithm:
Step1-Read the search element from the user.
Step2- Find the middle element in the sorted list.
Step3- Compare the search element with the middle element in the sorted list.
Step4-If both are matched, then display "Given element is found!!!"and terminate the function.
Step 5-If both are not matched, then check whether the search element is smaller or larger than
the middle element.
Step6-If the search element is smaller than middle element, repeat steps2,3,4 and 5for the left
sub list of the middle element.
Step7-If the search element is larger than middle element, repeat steps2,3,4 and 5 for the right
sub list of the middle element.
Step 8 - Repeat the same process until we find the search element in the list or until sub list
contains only one element.
Step 9-Ifthat element also doesn't match with the search element, then display" Element is not
found in the list!!!" and terminate the function.
Example:
Example2:
SORTINGS:
INSERTION SORT:
In Insertion sort the list can be divided into two parts, one is sorted list and other is unsorted list.
In each pass the first element of unsorted list is transfers to sorted list by inserting it in
appropriate position or proper place.
The similarity can be understood from the
style we arrange a deck of cards. This sort
works on the principle of inserting an
element at a particular position, hence the
name Insertion Sort.
Following are the steps involved in insertion sort:
1. We start by taking the second element of the given array, i.e. element at index 1, the key. The
key element here is the new card that we need to add to our existing sorted set of cards
2. We compare the key element with the element(s) before it, in this case, element at index0:
o If the key element is less than the first element, we insert the key element before the first
element.
o If the key element is greater than the first element, then we insert it after the first element.
3. Then, we make the third element of the array as key and will compare it with elements to it's left
and insert it at the proper position.
4. And we go on repeating this, until the array is sorted.
Example 1:
Example2:
SELECTION SORT:
Given a list of data to be sorted, we simply select the smallest item and place it in a sorted list.
These steps are then repeated until we have sorted all of the data.
In first step, the smallest element is search in the list, once the smallest element is found, it is
exchanged with the element in the first position.
Now the list is divided into two parts.
One is sorted list other is unsorted list.
Find out the smallest element in the
unsorted list and it is exchange with the
starting position of unsorted list, after
that it will added in to sorted list.
This process is repeated until all the elements are sorted.
Ex: asked to sort a list on paper.
Example 1:
Example2:Considertheelements23,78,45,88,32,56
BUBBLE SORT:
In this case, value 33 is greater than14,so it is already in sorted locations. Next, we compare 33
with 27. We find that 27 is smaller than 33 and these two values must be swapped.
swapped.
Next we compare 33and 35.We find that both are in already sorted positions.
Then we move to the next two values, 35and10.We know then that10 is smaller 35.
We swap these values. We find that we have reached the end of the array. After one iteration,
the array should look like this −
To be defined, we are now showing how an array should look like after each iteration. After the
second iteration, it should look like this
this
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is completely sorted.
Example2:
17