0% found this document useful (0 votes)
12 views30 pages

Unit-II Searching, Sorting, Linked List (DSUC)

The document provides an overview of searching and sorting techniques in data structures, detailing methods such as Sequential Search, Binary Search, and various sorting algorithms including Selection Sort, Bubble Sort, and Quick Sort. It also covers the concept of linked lists, their types, and operations such as insertion and deletion. Additionally, it discusses sparse matrices and their efficiency in memory usage.

Uploaded by

nnagammahiremath
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)
12 views30 pages

Unit-II Searching, Sorting, Linked List (DSUC)

The document provides an overview of searching and sorting techniques in data structures, detailing methods such as Sequential Search, Binary Search, and various sorting algorithms including Selection Sort, Bubble Sort, and Quick Sort. It also covers the concept of linked lists, their types, and operations such as insertion and deletion. Additionally, it discusses sparse matrices and their efficiency in memory usage.

Uploaded by

nnagammahiremath
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/ 30

Searching &

1
Sorting
Lecture By:
Mohammed Faraaz Ahmed
2 Searching Operations:
 Searching Operation:
Searching in Data Structure refers to the process of finding the desired element in the
list of elements. The desired element is called as ‘Target’. This searching can be implemented
in any data Structure like Array, Linked-list, graph, tree, etc.
Searching involves searching the desired element in the collection of items. Some of
the basic searching techniques are:
 Sequential  Binary Search.
 Search.
Sequential Search:
Sequential search is the most natural method of searching. In this the searching
process begins with searching every element in the list till the required element is found. It
does not involve any order of arrangement of records. Hence it is taking longer time & is
considered slower.
In data structure when elements are stored, they have a linear or sequential
relationship, as each data stored in a relative position to the others. These relative
positionsIn
areSequential
referred asSearch
index values, which are in order & helps in searching techniques.
it starts
from the beginning & simply scans the
entire list of elements until we find the
items or we run-out of the elements,
thus concluding item being searched is
not present.
3 Sorting Techniques :
 Analysis of Sequential Search:
To analyze
this searching
technique, where
there is a possibility
that the item being
searched may be
present OR may not
be present. The best
way
thattothe
find
item is present or not we need to search the complete list.
This involve three scenario First the Best Case- Where the item being searched is
present at the beginning so we need only less comparison, Second the Worst Case- Where the
item being searched is present at the last so lengthy comparison is needed till the item is found.
Third the Average Case- the item being searched is present half the way so we need to search
half the list.
4 Sorting Techniques :
 Binary Search:
Binary search is the searching techniques that work efficiently in sorted list of
elements.
The Binary search fallows the “Divide & Conquer” approach to find the search element.
Where in the list is divided into two halves. Then the item is compared with the middle
element in the list, if successful in searching result is returned, otherwise the search
continues in the other half of the list to produce a match.
Binary search has a draw back that it can be implemented only in sorted elements if
we don’t have a sorted array elements, we need to first sort the elements & later implement
the searching technique.
5 Sorting in D S:
 Sorting Operation:
“Sorting is arrangement of elements in some order [Ascending /
Descending].
Sorting operation refers to arranging the data in sequential Order. This will help in searching,
inserting, deleting a data item in an array or group of data. As sporting plays an important
role in Data Analysis, & is considered the most important operation performed by the
computer.
A sorting Algorithm comprises several operation such as comparison between data &
interchanges depending on the result of comparison & assignments.
The most common sorting techniques are:
 Selection Sort.
 Bubble Sort.
 Quick Sort.
 Insertion Sort.
6 Sorting Techniques:
 Selection Sort:
Selection Sort is a simple sorting Technique. In this method of sorting we find the first
smallest element & exchange it with the element in the first position on the array, Then we
find the second smallest element & exchange with the element in the second position & this
continues till all the elements in the array are sorted.
7 Sorting Techniques :
 Bubble Sort:
Bubble Sort is a most popular sorting Technique. In this method of sorting is achieved
by comparing each item in the list with the item next to it & swapping them if required. While
sorting the first element is compared with the second item to arrange the numbers in the list
in either Ascending or Descending order. If the first element is greater than the second the
position of the elements are exchanged, likewise all the elements are compared & rearranged.
Each swapping is referred as ‘Pass-n’
8 Sorting Techniques :
 Quick Sort:
Quick Sort is a sorting Technique which is achieved by portioning. In this method of
sorting we take one number & finalize its position, simultaneously we swap the data in a
manner that when the numbers are sorted all the elements on the left are smaller & numbers
on the right are greater than the reference number. When arranging in ascending order, we
find how many numbers are less than the number under consideration.
Basic Idea of Quick
Sort:
 Choose a pivot from the
array & Swap elements
& place it in a[first]
 Compare each element
in a[first+1…last] with
the pivot
☼ if element is < then the
pivot add it to S1
☼ if the element is ≥ then
the pivot add it to S2
 Arrange the array in the
order S1 pivot S2
9 Sorting Techniques :
 Insertion Sort:
Insertion Sort is a sorting Technique which is achieved in a similar way as Selection
sort. In this method of sorting elements from the unsorted list placed in the appropriate
position in the sorted list, similar as selection sort where the smallest element is selected from
the unordered list & place it at the beginning of the sorted list.
10 Sorting Techniques :
 Merge Sort:
Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It
works by recursively dividing the input array into smaller subarrays and sorting those
subarrays then merging them back together to obtain the sorted array.
In simple terms, we can say that the process of merge sort is to divide the array into
two halves, sort each half, and then merge the sorted halves back together. This process is
repeated until the entire array is sorted.
How does Merge Sort work?
Merge sort is a popular sorting algorithm known for its
efficiency and stability. It follows the divide-and-conquer
approach to sort a given array of elements.
Here’s a step-by-step explanation of how merge sort works:
Divide: Divide the list or array recursively into two halves
until it can no more be divided.
Conquer: Each subarray is sorted individually using the
merge sort algorithm.
Merge: The sorted subarrays are merged back together in
sorted order. The process continues until all elements from
both subarrays have been merged.
11 Sparse Matrix:
 Sparse Matrix:
A matrix in which most of the elements are zero’s is known as Sparse Matrix.
Many a times when using matrix it happens that most elements in the given matrix are zero,
which when stored in memory is a waste of memory. & when processed it is waste of time. If in
such cases a sparse matrix is used we just store the elements that are non-zero & implicitly
assume the elements not stored as zero elements. This will greatly reduce the number of
times to be stored & the number of operation to be performed, thus reducing the time &
efforts to deal with matrix operations.
12 Linked List:
 Basic Concept:
Linked List consist of a sequence of nodes containing data
fields and references, which point to their neighbouring node.
They are useful because the order of the item can be different
from the order that has stored in memory. Nodes stored can be
Inserted or removed any point in the list.
 Features of Linked List:
 A Linked List is a linear data structure that stores a collection of data elements
dynamically.
 Nodes represent those data elements or links which connects each nodes.
 Each node consists of two fields, the Information stored in the linked list & a pointer
that stores the address of its next node.
 The last node contains NULL in its second field as the last node will not point to any
node.
 A linked list can grow & shrink its size as per the users requirement.
 It does not waste memory space.
13 Linked List:
 Representation of Linked List:
A linked list can be observed as a chain of nodes. Where every node points to the
next node.
 Terminology:
 Node: A linked list record contains a data field, a reference field & a self referential
structure.
 Next Pointer: The field of a node that contains a reference to the next node.
 Prev Pointer: The field of the node that contains a reference to the previous node.
 Head Node: The first node of the linked list.
 Tail Node: The last node of the linked list.
14 Types of Linked List:
 Types of Linked List:
The following are the different types of Linked List:
 Singly Linked List.
 Doubly Linked List.
 Header Linked List.
 Circular Linked List.
 Singly Linked List:
A Singly Linked List is like a train system, where it connects each bogie to the next
bogie. A Singly Linked List is a unidirectional linked list, i.e. we can traverse it in one
direction i.e. from head to tail
15 Types of Linked List:
 Doubly Linked List:
A Doubly Linked List is same like a Singly Linked List, except the point that each
node has two pointers, where one points to the next node and the other points to the
previous node.
 You can travers the list either in forward or backward direction.
 You can insert a new node any where very easily, i.e either inserting before a node,
after a node, at the front of the list or at the end of the list.
 You can also delete nodes from any point in the list.
16 Operations on Linked List:
 Inserting into Doubly Linked List:
Inserting into a Doubly Linked List can be performed at any point in the list just the
point to remember is that we need to establish the new connection with the old nodes
correctly

Inserting at the Starting node

Inserting at the middle/desired


node

Inserting at the End node


17 Operations on Linked List:
 Deleting from Doubly Linked List:
Inserting into a Doubly Linked List can be performed at any point in the list just the
point to remember is that we need to establish the new connection with the old nodes
correctly
Deleting at the Starting node

Deleting at the middle/desired


node
Deleting at the End node
18 Types of Linked List:
 Header Linked List:
A Header Linked List is Linked List with special node at the beginning, This special
node stores the numbers of nodes present in the Linked List. In other Linked List we fallow
the traversing technique to identify the number of nodes in the list, but in Header Linked
List size of the list is stored in the header itself.

 Types of Header Linked List:


 Grounded Header Linked List
 Circular Header Linked List
19 Types of Linked List:
 Grounded Header Linked List:
In this type of Header Linked List, the last node of the list points to NULL or holds
the reference to NULL Pointer. The head pointer points to the Header node of the list. If the
there is no node to the next of head pointer or head. next equals NULL then we know that
the Linked List is empty. The operations performed on the Header Linked List are same as
Singly Linked List such as Insertion, Deletion, and Traversal of nodes.
20 Types of Linked List:
 Circular Header Linked List:
In this type of Header Linked List, the last node points back to the header node is
called circular linked list. The chains do not indicate first or last nodes. In this case, external
pointers provide a frame of reference because last node of a circular linked list does not
contain the NULL pointer. The possible operations on this type of linked list are Insertion,
Deletion and Traversing.
21 Types of Linked List:
 Circular Linked List:
The circular linked list is a linked list where all nodes are connected to each other
to form a circle. In a circular linked list, the first node and the last node are connected to
each other which forms a circle. There is no NULL at the end.

 There are Two Types of Circular Linked List:


Circular Singly linked list: In a circular Singly linked list, the last node of the list contains
a pointer to the first node of the list.
22 Types of Linked List:
Circular Doubly linked list: Circular Doubly Linked List has properties of both doubly
linked list and circular linked list in which two consecutive elements are linked or connected
by the previous and next pointer and the last node points to the first node by the next
pointer and also the first node points to the last node by the previous pointer.

 Representation of Linked List in Memory:


The Linked list when stored in memory in scattered positions. The memory for each
node is assigned dynamically which means as and when required, so the linked can be
increased or decreased as users choice.
Root 100 Root 100 500
8 8 6
1008 INFO 1008 INFO 500
6 INFO

Root 100 500 700


8 6 9
1008 INFO 500
6 INFO 700
9 INFO
23 Operations on Singly Linked List:
 Operations on Singly Linked List:
There are various operations that can be performed on linked list such as
 Traversing
 Searching
 Insertion
 Deletion
 Traversing:
The Concept of traversing refers to moving in the linked list from beginning to end visiting
each node.
The algorithm for traversing a list is as fallows.
 Start from the head of the list. Access the content of the head node if it is not null.
 Then go the next node [if exists] & access the node information
 Continue until no more nodes exists [ i.e. you have reached the null node]
24 Operations on Singly Linked List:
 Insertion:
The Concept of inserting refers to inserting an element in the linked list . There are three
possible cases of Insertion.
o Insertion at the Beginning.
o Insertions at the End
o Insertion at the specified position.
 Insertion at the Beginning:
If the List is empty, new node added becomes the first node or the head node in the list, if
the node exists then add the new node & connect it to the head of the list.

1. Allocate new
2. Store data.
3. Change to
point to head.
4. Change head
to point the
new node
25 Operations on Singly Linked List:
 Insertion at the End:
We first traverse the list to find the end node then a new node will be added at the end &
mark it as NULL as being the new last node. In Special case where the list is empty node
added becomes the first & last node of the linked list.
1. Allocate new
2. Store data.
3. Traverse to
last
4. Change next
 Insertion at the Specified Position: of last node to
new node.
The insertion is specified with the reference of the node, there by insertion is done at that
reference.
1. Allocate new
2. Traverse to
node
3. Change next
pointer to the
new node.
26 Operations on Singly Linked List:
 Deletion:
This is a process where a node is deleted form the Linked List. We can achieve this with
fallowing steps.
o Find the previous node of the node to be deleted.
o Change the pointer of the previous node.
o Free the allocated memory of the node.
In special case if the first node is deleted, we must update the head of the linked list.

 Searching:
This is a process where a node is searched to find the value in the Linked List, to do so we
need to traverse the Linked list & compare the value to be searched with the value present
in the Linked List. This is as same as Traversing, Just we do not display the Date, we just
check its presence.
27 Garbage Collection:
 Garbage Collection:
Garbage Collection [GC] is a dynamic technique for memory management & heap allocation
that evaluates & Identifies dead memory blocks before reallocating storage for reuse. Its main
purpose is to reduce memory leaks or [wastage]. It reduces the efforts or programmer of
deallocating the memory manually.
The basic principle of GC is to find data objects in the program which are not accessed
in future & reclaiming the resources allocated to these objects.
Commonly implemented GC techniques are:
 Mark & Sweep.
 Reference Counting.
 Mark & Sweep:
The Mark Sweep algorithm is as efficient as its name . As it consists of two Phases: A
Mark Phase & A Sweep Phase. The collector traverses through all the global variables,
local variables, stack frames, virtual & hardware registers etc to mark every item of memory by
setting a bit around the object during this phase, thus reclaiming memory from all the
unmarked items.
Sweep ( ) is a simple function with a straight forward implementation, as it linearly
traverses the
28 Garbage Collection:
- memory, freeing any objects that aren’t tagged. Thus the marked memory is left &
unmarked memory is swept off to free the unused memory & resource allocations & use it
for future allocation in the program.
29 Reference Counting:
 Reference Counting:
The Method of Reference Counting is simple, as it is based on counting how many
pointer references each allocated object has, it’s a straightforward incremental solution,
because it has a distributed incremental memory management. Apart form its
implementation in programming it also has its use in Operating System as a resource
management tool, for managing system resources such as files, etc.
Questionnaires:
30
1. What is sorting? What are the different sorting techniques available in
the data structure?
2. Explain the working of selection sort.
3. Explain the working of a bubble sort.
4. Explain the working of Quicksort with an example.
5. Explain the working of selection sort with example.
6. How does insertion sort work, given example?
7. What is searching? What are the different types of searching we have
in the data structure?
8. Show the working of sequential search. Using example.
9. Show the working binary search with an example.
10. What's the difference between iterative searching and recursive
searching of a binary search?
11. Define multidimensional arrays.
12. Show the representation of multidimensional arrays.
13. What are sparse matrices? Give an example.

You might also like