Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
49 views32 pages

DS E-Content - Module 4 Searching & Sorting

The document is a course outline for 'Data Structures using JAVA' by Dr. A K Yadav, detailing various searching and sorting algorithms. It covers searching techniques like Linear Search, Binary Search, Indexed Sequential Search, and Hashing, as well as sorting methods such as Insertion Sort, Bubble Sort, Selection Sort, Quick Sort, and Merge Sort. Each section provides explanations, algorithms, and characteristics of the respective methods.
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)
49 views32 pages

DS E-Content - Module 4 Searching & Sorting

The document is a course outline for 'Data Structures using JAVA' by Dr. A K Yadav, detailing various searching and sorting algorithms. It covers searching techniques like Linear Search, Binary Search, Indexed Sequential Search, and Hashing, as well as sorting methods such as Insertion Sort, Bubble Sort, Selection Sort, Quick Sort, and Merge Sort. Each section provides explanations, algorithms, and characteristics of the respective methods.
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
You are on page 1/ 32

School of Computer Science and Engineering

Data Structures using JAVA [R1UC303B]

Dr. A K Yadav

School of Computer Science and Engineering


Plat No 2, Sector 17A, Yamuna Expressway
Greater Noida, Uttar Pradesh - 203201

November 13, 2024

Dr. A K Yadav Data Structures using JAVA 1/32


School of Computer Science and Engineering

Contents
Searching 3
Linear Search 4
Binary Search 7
Indexed Sequential Search 11
Hashing 13

Sorting 19
Insertion Sort 20
Bubble Sort 22
Selection Sort 24
Quick Sort 26
Merge Sort 29

Dr. A K Yadav Data Structures using JAVA 2/32


School of Computer Science and Engineering

Searching
Searching algorithms are essential tools in computer science used to
locate specific items within a collection of data. These algorithms
are designed to efficiently navigate through data structures to find
the desired information, making them fundamental in various
applications such as databases, web search engines, and more.
Different searching algorithms are:
I Linear Search
I Binary Search
I Indexed Sequential Search
I Hashing

Dr. A K Yadav Data Structures using JAVA 3/32


School of Computer Science and Engineering

Linear Search
Linear search is a method for searching for an element in a
collection of elements. Each element of the collection is visited one
by one in a sequential fashion to find the desired element. Linear
search is also known as sequential search.
Linear Search Algorithm:
I Every element is considered as a potential match for the key
and checked for the same.
I If any element is equal to the key, the search is successful and
the index of that element is returned.
I If no element is found equal to the key, the search yields “No
match found”.

Dr. A K Yadav Data Structures using JAVA 4/32


School of Computer Science and Engineering

Figure: Linear Search using Iteration

Dr. A K Yadav Data Structures using JAVA 5/32


School of Computer Science and Engineering

Figure: Linear Search using Recursion

Dr. A K Yadav Data Structures using JAVA 6/32


School of Computer Science and Engineering

Binary Search
Binary search is a search algorithm used to find the position of a
target value within a sorted array. It works by repeatedly dividing
the search interval in half until the target value is found or the
interval is empty. The search interval is halved by comparing the
target element with the middle value of the search space.
Conditions to apply Binary Search
I The data structure must be sorted.
I Access to any element of the data structure should take
constant time.
Binary Search Algorithm:
I Divide the search space into two halves by finding the middle
index “mid”.
I Compare the middle element of the search space with the key.

Dr. A K Yadav Data Structures using JAVA 7/32


School of Computer Science and Engineering

I If the key is found at middle element, the process is


terminated.
I If the key is not found at middle element, choose which half
will be used as the next search space.
I If the key is smaller than the middle element, then the left side
is used for next search.
I If the key is larger than the middle element, then the right side
is used for next search.
I This process is continued until the key is found or the total
search space is exhausted.

Dr. A K Yadav Data Structures using JAVA 8/32


School of Computer Science and Engineering

Figure: Binary Search using Iteration

Dr. A K Yadav Data Structures using JAVA 9/32


School of Computer Science and Engineering

Figure: Binary Search using Recursion

Dr. A K Yadav Data Structures using JAVA 10/32


School of Computer Science and Engineering

Indexed Sequential Search


Indexed Sequential search: In this searching method, first of all, an
index file is created, that contains some specific group or division
of required record when the index is obtained, then the partial
indexing takes less time because it is located in a specified group.
When the user makes a request for specific records it will find that
index group first where that specific record is recorded.
Characteristics:
I In Indexed Sequential Search a sorted index is set aside in
addition to the array.
I Each element in the index points to a block of elements in the
array or another expanded index.
I The index is searched 1st then the array and guides the search
in the array.

Dr. A K Yadav Data Structures using JAVA 11/32


School of Computer Science and Engineering

Indexed Sequential Search actually does the indexing multiple


time, like creating the index of an index.

Figure: Indexed Sequential Search

Dr. A K Yadav Data Structures using JAVA 12/32


School of Computer Science and Engineering

Hashing
Hashing is a technique used in data structures that efficiently
stores and retrieves data in a way that allows for quick access.
- Hashing refers to the process of generating a fixed-size output
from an input of variable size using the mathematical formulas
known as hash functions.
- This technique determines an index or location for the storage of
an item in a data structure. - It involves mapping data to a
specific index in a hash table using a hash function that enables
fast retrieval of information based on its key.
- This method is commonly used in databases, caching systems,
and various programming applications to optimize search and
retrieval operations.
- The great thing about hashing is, we can achieve all three
operations (search, insert and delete) in O(1) time on average.

Dr. A K Yadav Data Structures using JAVA 13/32


School of Computer Science and Engineering

Components of Hashing: There are majorly three components of


hashing:
1. Key: A Key can be anything string or integer which is fed as
input in the hash function the technique that determines an
index or location for storage of an item in a data structure.
2. Hash Function: The hash function receives the input key and
returns the index of an element in an array called a hash
table. The index is known as the hash index.
3. Hash Table: Hash table is a data structure that maps keys to
values using a special function called a hash function. Hash
stores the data in an associative manner in an array where
each data value has its own unique index.

Dr. A K Yadav Data Structures using JAVA 14/32


School of Computer Science and Engineering

Collision: in Hashing occurs when two different keys map to the


same hash value.
- The hashing process generates a small number for a big key, so
there is a possibility that two keys could produce the same value.
- The situation where the newly inserted key maps to an already
occupied key value then it must be handled using some collision
handling technology.
Causes of Hash Collisions:
I Poor Hash Function: A hash function that does not distribute
keys evenly across the hash table can lead to more collisions.
I High Load Factor: A high load factor (ratio of keys to hash
table size) increases the probability of collisions.
I Similar Keys: Keys that are similar in value or structure are
more likely to collide.
- There are mainly two methods to handle collision:

Dr. A K Yadav Data Structures using JAVA 15/32


School of Computer Science and Engineering

I Open Addressing:
Linear Probing: Search for an empty slot sequentially
Quadratic Probing: Search for an empty slot using a quadratic
function
I Closed Addressing:
Chaining: Store colliding keys in a linked list or binary search
tree at each index
Cuckoo Hashing: Use multiple hash functions to distribute
keys Separate Chaining
Applications of Hashing: Hash tables are used wherever we have a
combinations of search, insert and/or delete operations.
I Dictionaries: To implement a dictionary so that we can
quickly search a word.

Dr. A K Yadav Data Structures using JAVA 16/32


School of Computer Science and Engineering

I Databases: Hashing is used in database indexing. There are


two popular ways to implement indexing, search trees (B or
B+ Tree) and hashing.
I Cryptography: When we create a password on a website, they
typically store it after applying a hash function rather than
plain text.
I Caching: Storing frequently accessed data for faster retrieval.
For example browser caches, we can use URL as keys and find
the local storage of the URL.
I Symbol Tables: Mapping identifiers to their values in
programming languages
I Network Routing: Determining the best path for data packets

Dr. A K Yadav Data Structures using JAVA 17/32


School of Computer Science and Engineering

I Associative Arrays: Associative arrays are nothing but hash


tables only. Commonly SQL library functions allow you
retrieve data as associative arrays so that the retrieved data in
RAM can be quickly searched for a key.

Dr. A K Yadav Data Structures using JAVA 18/32


School of Computer Science and Engineering

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. For example arranging students
acoording to hight in morning assembly, seating roll no wise in
exams, arranging names marks wise in merit list etc. There are
different algorithms for sorting:
I Insertion Sort
I Bubble Sort
I Selection Sort
I Quick Sort
I Merge Sort

Dr. A K Yadav Data Structures using JAVA 19/32


School of Computer Science and Engineering

Insertion Sort
I Insertion sort is a simple sorting algorithm that works by
iteratively inserting each element of an unsorted list into its
correct position in a sorted portion of the list.
I It is a stable sorting algorithm, meaning that elements with
equal values maintain their relative order in the sorted output.
I Insertion sort is like sorting playing cards in your hands.
I You split the cards into two groups: the sorted cards and the
unsorted cards.
I Then, you pick a card from the unsorted group and put it in
the right place in the sorted group.

Dr. A K Yadav Data Structures using JAVA 20/32


School of Computer Science and Engineering

Figure: Insertion Sort

Dr. A K Yadav Data Structures using JAVA 21/32


School of Computer Science and Engineering

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.
Algorithm:
I traverse from left and compare adjacent elements and the
higher one is placed at right side.
I In this way, the largest element is moved to the rightmost end
at first.
I This process is then continued to find the second largest and
place it and so on until the data is sorted.

Dr. A K Yadav Data Structures using JAVA 22/32


School of Computer Science and Engineering

Figure: Bubble Sort

Dr. A K Yadav Data Structures using JAVA 23/32


School of Computer Science and Engineering

Selection Sort
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.
I 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.
I This process is repeated for the remaining unsorted portion
until the entire list is sorted.

Dr. A K Yadav Data Structures using JAVA 24/32


School of Computer Science and Engineering

Figure: Selection Sort


Dr. A K Yadav Data Structures using JAVA 25/32
School of Computer Science and Engineering

Quick Sort
I QuickSort is a sorting algorithm based on the Divide and
Conquer that picks an element as a pivot and partitions the
given array around the picked pivot by placing the pivot in its
correct position in the sorted array.
I There are mainly three steps in the algorithm.
I 1. Choose a pivot
I 2. Partition the array around pivot. After partition, it is
ensured that all elements are smaller than all right and we get
index of the end point of smaller elements. The left and right
may not be sorted individually.
I 3. Recursively call for the two partitioned left and right
subarrays. We stop recursion when there is only one element
is left.

Dr. A K Yadav Data Structures using JAVA 26/32


School of Computer Science and Engineering

Dr. A K Yadav Data Structures using JAVA 27/32


School of Computer Science and Engineering

Figure: Quick Sort

Dr. A K Yadav Data Structures using JAVA 28/32


School of Computer Science and Engineering

Merge Sort
I Merge sort is a sorting algorithm that follows the
divide-and-conquer approach.
I 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.
I In simple terms, the process of merge sort is to divide the
array into two halves, sort each half, and then merge the
sorted halves back together.
I This process is repeated until the entire array is sorted.

Dr. A K Yadav Data Structures using JAVA 29/32


School of Computer Science and Engineering

Dr. A K Yadav Data Structures using JAVA 30/32


School of Computer Science and Engineering

Figure: Merge Sort

Dr. A K Yadav Data Structures using JAVA 31/32


School of Computer Science and Engineering

Thank you
Please send your feedback or any queries to
[email protected]

Dr. A K Yadav Data Structures using JAVA 32/32

You might also like