0% found this document useful (0 votes)
43 views45 pages

DS Unit 1

The document provides an overview of data structures, including their definitions, types (primitive and non-primitive), and operations such as traversal, insertion, deletion, searching, sorting, and merging. It discusses various searching techniques like linear and binary search, as well as sorting algorithms including bubble sort, selection sort, and insertion sort, along with their time and space complexities. Additionally, it highlights the applications of data structures in areas like operating systems, DBMS, and artificial intelligence.

Uploaded by

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

DS Unit 1

The document provides an overview of data structures, including their definitions, types (primitive and non-primitive), and operations such as traversal, insertion, deletion, searching, sorting, and merging. It discusses various searching techniques like linear and binary search, as well as sorting algorithms including bubble sort, selection sort, and insertion sort, along with their time and space complexities. Additionally, it highlights the applications of data structures in areas like operating systems, DBMS, and artificial intelligence.

Uploaded by

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

DATA STRUCTURES

UNIT I :
Introduction to Data Structures: Definition and
importance of data structures, Abstract data types (ADTs)
and their implementation, Overview of time and space
complexity.
Searching Techniques: Linear & Binary Search
Sorting Techniques: Bubble sort, Selection sort,
Insertion Sort
INTRODUCTION:
A data structure is a particular way of organising data
in a computer so that it can be used effectively. The
idea is to reduce the space and time complexities of
different tasks.
The choice of a good data structure makes it possible
to perform a variety of critical perations effectively.
An efficient data structure also uses minimum
memory space and execution time to process the
structure.
 A data structure is not only used for organising 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.

Need Of Data Structure:


Data structures provide an easy way of organising,
retrieving, managing, and storing data.i.e
Data structure modification is easy.
It requires less time.
Save storage memory space.
Data representation is easy.
Easy access to the large database
DEFINTION OF DATA STRUCTURES:
Primitive data structure :
The primitive data structures are known as
basic data structures.
These data structures are directly operated
upon by the machine instructions.
 Normally, primitive data structures have
different representation on different
computers.
Ex: Integer, Float,Character,Pointer.
Non-Primitive data structure :
The non-primitive data structures are highly
developed complex data structures.
Basically, these are developed from the primitive
data structure. The non-primitive data structure is
responsible for organizing the group of homogeneous
and heterogeneous data elements.
Ex : Arrays,Lists,Files
Based on the organizing method of a data
structure, data structures are divided into two types.
1. Linear Data Structures
2. Non - Linear Data Structures
Linear Data Structures : If a data structure is
organizing the data in sequential order, then that data
structure is called as Linear Data Structure.
For example: 1. Arrays 2. Lists (Linked List)
3. Stacks 4. Queues
Non - Linear Data Structures: If a data structure
is organizing the data in random order, then that data
structure is called as Non-Linear Data Structure.
For example:1. Trees 2. Graphs 3. Dictionaries
4. Heaps , etc.,
Operations on Data Structures
The basic operations that are performed on data
structures are as follows:
1. Traversal: Traversal of a data structure means
processing all the data elements present in it exactly
once.
2. Insertion: Insertion means addition of a new data
element in a data structure.
3. Deletion: Deletion means removal of a data
element from a data structure if it is found.
4. Searching: Searching involves searching for the
specified data element in a data structure.
5. Sorting: Arranging data
Elements of a data structure in a
specified order is called sorting.
6. Merging: Combining elements
of two similar data structures to
form a new data structure of the
same type, is called merging.
Application of Data structures: It
is widely used in following Areas.
•Operating System.
•DBMS : graphs, Trees and Arrays
•Simulation
•Graphics
•Compiler Design
•Numerical analysis
•Artificial analysis
Ex:
•Google maps(Shortest Path)
•Printing Papers
•Storing student information
•Library Management
•Evaluating Expression
•Data storage in Hard disk
•Possible moves in chess games.
•Dictionaries.
Abstract data type:
•ADT’s define data and operations
•but not implementation.
•ADT describes only the logical
implementation but not physical
implementation.
•DS is used to implement ADT
i.e physical implementation.
What is an algorithm?
•An algorithm is a finite set of step by
step instructions to solve a problem/Task.
•In normal language,algorithm is defined
as a sequence of statements which are used
to perform a task.
•In computer science, an algorithm can be
defined as follows:
An algorithm is a sequence of unambiguous
instructions used for solving a problem,
which can be implemented (as a program)
on a computer.
Performance analysis of an algorithm is
performed by using the following measures:
1. Space Complexity
2. Time Complexity
Space complexity: Total amount of
computer memory required by an algorithm to
complete its execution is called as space
complexity of that algorithm.

Time complexity: The time complexity of


an algorithm is the total amount of time required
by an algorithm to complete its execution.
Ex:

Total Time Complexity= O(n)

Total Space Complexity = Program Space + Variables


Space(S,I,a[])
Searching Techniques:
Searching is an operation or a technique that helps finds
the place of a given element or value in the list.
Any search is said to be successful or unsuccessful
depending upon whether the element that is being searched
is found or not.
Some of the standard searching technique that is being
followed in the data structure is listed below:
1)Linear Search or Sequential Search
2)Binary Search
Linear Search
Linear search is a sequential searching algorithm
where we start from one end and check every
element of the list until the desired element is found.
It is the simplest searching algorithm.
In Linear Search Algorithm,
Every element is considered as a potential match for
the key and checked for the same.
If any element is found equal to the key, the search
is successful and the index of that element is returned.
If no element is found equal to the key, the search
yields “No match found”.
For example: Consider the array
arr[] = {10, 50,30,70,80,20,90,40} and
key = 30
Step 1: Start from the first element (index 0) and
compare key with each element (arr[i]).
•Comparing key with first element arr[0]. SInce not equal, the
iterator moves to the next element as a potential match.

•Step-2Comparing key with next element arr[1]. Since not


equal, the iterator moves to the next element as a potential
match.
Step 3: 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 (here 2).
BINARY SEARCH

Binary Search is a searching algorithm for finding an element's


position in a sorted array.
Binary search can be implemented only on a sorted list of items. If
the elements are not sorted already, we need to sort them first.
In this approach, the element is always searched in the middle of a
portion of an array.
The search starts with comparing the search element with the
middle element of the array. If value matches then the position of the
element is returned.
Else if the search element is less than the middle element of the array
then the second half of the array is discarded and the search continues
by dividing the first half.
Else the process is the same when the search element is greater than
the middle element then the first half of the array is discarded and the
search continues by the second half.
 The iteration repeats until a match for the serach element is found.
Binary Search Working
Binary Search Algorithm can be implemented in two
ways which are discussed below.
a)Iterative Method
b)Recursive Method
The recursive method follows the divide and conquer
approach.

Ex: Let the elements of array are -


Let the element to search is, K = 56
We have to use the below formula to calculate the mid of the
array –
mid=(low + high)/2
So, in the given array -
low = 0 high = 8
mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.
Now, the element to search is found. So algorithm will
return the index of the element matched.
Binary Search Algorithm
a) Iteration Method
do until the pointers low and high meet each other.
mid = (low + high)/2
if (k == arr[mid])
return mid
else if (k > arr[mid]) // k is on the right side
low = mid + 1
else // k is on the left side
high = mid - 1
b)Recursive Method
binarySearch(arr, k, low, high)
if low > high
return False
else
mid = (low + high) / 2
if k == arr[mid]
return mid
else if k > arr[mid] // k is on the right side
return binarySearch(arr, k, mid + 1, high)
else // x is on the left side
return binarySearch(arr, k, low, mid - 1)
Time Complexities
Best case complexity: O(1)
Average case complexity: O(log n)
Worst case complexity: O(log n)

Space Complexity :
The space complexity of the binary search is O(1).
Sorting Techniques: are Arranging the Elements in
Ascending or Descending Order .

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.

Ex: How does Bubble Sort Work?


Let us understand the working of bubble sort with the help
of the following illustration:
2. Remaining Iteration
•The same process goes on for the remaining iterations.
•After each iteration, the largest element among the
unsorted elements is placed at the end.
In each iteration, the comparison takes place up to the last
unsorted element.

The array is sorted when all the unsorted elements are


placed at their correct positions.
Algorithm:
begin BubbleSort(arr)
for all array elements
if arr[i] > arr[i+1]
swap(arr[i], arr[i+1])
end if
end for
return arr
end BubbleSort
Bubble Sort Time Complexity
Best O(n)
Worst O(n2)
Average O(n2)
Space Complexity O(1)
SELECTION SORT

Selection sort is a simple and efficient sorting


algorithm.
The algorithm repeatedly selects the smallest (or
largest) element from the unsorted portion of the
list and swaps it with the first element of the
unsorted part. This process is repeated for the
remaining unsorted portion until the entire list is
sorted.
Algorithm:
Step by Step Process
The selection sort algorithm is performed using the
following steps...
Step 1 - Select the first element of the list (i.e., Element at
first position in the list).
Step 2: Compare the selected element with all the other
elements in the list.
Step 3: In every comparision, if any element is found
smaller than the selected element (for Ascending order),
then both are swapped.
Step 4: Repeat the same procedure with element in the
next position in the list till the entire list is sorted.
Ex:

Interation-1:
Compare First Element with remaining Elements
Complexity of the Selection Sort Algorithm
WorstCase: O(n2) BestCase: Ω(n2) Average Case : Θ(n2)
//Selection sort Algorithm
Algorithm for Selectionsort(list,size)
{
for i:=0 to size-1
for j=i+1 to size;
{
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
Advantages of Selection Sort Algorithm
Simple and easy to understand.
Works well with small datasets.
Disadvantages of the Selection Sort Algorithm
Selection sort has a time complexity of O(n^2) in
the worst and average case.
Does not work well on large datasets.
Does not preserve the relative order of items with
equal keys which means it is not stable.
INSERTION SORT
Sorting is the process of arranging a list of elements in a
particular order (Ascending or Descending).
•Insertion sort algorithm arranges a list of elements in a
particular order.
• In insertion sort algorithm, every iteration moves an
element from unsorted portion to sorted portion until all the
elements are sorted in the list.
Step by Step Process
The insertion sort algorithm is performed using the
following steps...
•Step 1 - Assume that first element in the list is in sorted
portion and all the remaining elements are in unsorted
portion.
Step 2: Take first element from the unsorted portion and insert
that element into the sorted portion in the order specified.
•Step 3: Repeat the above process until all the elements from
the unsorted portion are moved into the sorted portion.

Following is the sample code for insertion sort...


Insertion Sort Logic
//Insertion sort logic
for i = 1 to size-1 {
temp = list[i];
j = i-1;
while ((temp < list[j]) && (j > =0)) {
list[j+1] = list[j];
j = j - 1;
}
list[j+1] = temp; }
Example
Time Complexity
WorstCase: O(n2)
BestCase: Ω(n)
Average Case : Θ(n2)

You might also like