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

Day 10 - Sorting and Searching

Sorting and Searching in java

Uploaded by

Thảo Phương
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 views88 pages

Day 10 - Sorting and Searching

Sorting and Searching in java

Uploaded by

Thảo Phương
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/ 88

Sorting & Searching

Faculty of Information Technology, Hanoi University


Getting Started 2
❖ Welcome, everyone! In this lecture, we're diving into two critical concepts in data
management: Searching and Sorting. These are not just theoretical algorithms,
but foundational tools used in virtually every application that handles data. Imagine
trying to find a file on your computer without search capabilities, or organizing
records in a database without sorting — it would be a mess!
Getting Started 3
❖ Sorting is key to data organization, improving efficiency in both retrieval and
processing. Whether you're managing a small list of items or massive data sets,
choosing the right sorting algorithm can drastically reduce computational time.
Getting Started 4
❖ On the other hand, searching allows us to pinpoint specific information quickly. As
datasets grow larger, the importance of efficient search algorithms like Binary
Search or more advanced methods becomes essential.
Getting Started 5
❖ Together, these algorithms form the backbone of data management, making
systems faster, more efficient, and capable of handling today's big data demands.
Let’s explore how these techniques work and why they're so essential for
developers and data scientists!
What You Are About To Achieve 6
❖ By the end of this lecture, we are going to:
❑ Understand the fundamental concepts of sorting and searching algorithms.
❑ Implement Sorting algorithms to arrange data in a specified order.
❑ Apply Searching algorithms for finding elements in a list.
❑ Compare the time complexities of sorting algorithms.
❑ Analyze the performance of algorithms in large datasets.
❑ Evaluate different algorithm choices based on their efficiency and use cases.
7
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Sorting Algorithms 8
❖ You know, when presidential candidate Barack Obama visited Google in 2007, Google
CEO Eric Schmidt asked Obama the most efficient way to sort a million 32-bit integers.
Obama answered that the bubble sort would be the wrong way to go. Was he right? Let’s
examine different sorting algorithms in this lecture and see if he was correct.
Sorting Algorithms 9
❖ Sorting is a classic subject in computer science. There are three reasons to study sorting
algorithms.
❑ First, sorting algorithms illustrate many creative approaches to problem solving, and these
approaches can be applied to solve other problems.
❑ Second, sorting algorithms are good for practicing fundamental programming techniques
using selection statements, loops, methods, and arrays.
❑ Third, sorting algorithms are excellent examples to demonstrate algorithm performance.
Sorting Algorithms 10
❖ The data to be sorted might be integers, doubles, characters, or objects. For simplicity,
this lecture assumes:
1. data to be sorted are integers,
2. data are stored in an array,
3. data are sorted in ascending order.
Sorting Algorithms 11
❖ There are many algorithms for sorting. This lecture introduces selection sort, insertion
sort, bubble sort, merge sort, quick sort, bucket sort, radix sort, and external sort.
12
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Sorting Algorithms – Selection Sort 13
❖ Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly
selecting the smallest (or largest) element from the unsorted portion and swapping it
with the first unsorted element. This process continues until the entire array is sorted.
❖ There are three main steps in this algorithm:
❑ First, we find the smallest element and swap it with the first element. This way we
get the smallest element at its correct position.
❑ Then we find the smallest among remaining elements (or second smallest) and move
it to its correct position by swapping.
❑ We keep doing this until we get all elements moved to correct position.
Sorting Algorithms – Selection Sort 14
❖ And here is how to sort the list {2, 9, 5, 4, 8, 1, 6} using selection sort.
Sorting Algorithms – Selection Sort 15
❖ You know how the selection-sort approach works. The task now is to implement it in
Java. Beginners find it difficult to develop a complete solution on the first attempt. Start
by writing the code for the first iteration to find the smallest element in the list and swap
it with the first element, and then observe what would be different for the second
iteration, the third, and so on. The insight this gives will enable you to write a loop that
generalizes all the iterations.

for (int i = 0; i < list.length - 1; i++) {


select the smallest element in list[i..list.length-1];
swap the smallest with list[i], if necessary;
// list[i] is in its correct position.
// The next iteration applies on list[i+1..list.length-1]
}
Sorting Algorithms – Selection Sort 16
❖ Let’s see how Selection Sort works:
Sorting Algorithms – Selection Sort 17
❖ And here is an example:
/** The method for sorting the numbers */
public static void selectionSort(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
// Find the minimum in the list[i..list.length-1]
int currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < list.length; j++) {
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex != i) {
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
18
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Sorting Algorithms – Insertion Sort 19
❖ 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. It is like
sorting playing cards in your hands. You split the cards into two groups: the sorted cards
and the unsorted cards. Then, you pick a card from the unsorted group and put it in the
right place in the sorted group.
❖ And here is the steps:
❑ We start with second element of the array as first element in the array is assumed to
be sorted.
❑ Compare second element with the first element and check if the second element is
smaller then swap them.
❑ Move to the third element and compare it with the first two elements and put at its
correct position.
❑ Repeat until the entire array is sorted.
Sorting Algorithms – Insertion Sort 20
❖ The algorithm can be described as follows:

for (int i = 1; i < list.length; i++) {


insert list[i] into a sorted sublist list[0..i-1] so that list[0..i] is sorted.
}
Sorting Algorithms – Insertion Sort 21
❖ And here is how to sort the list {2, 9, 5, 4, 8, 1, 6} using insertion sort.
Sorting Algorithms – Insertion Sort 22
❖ And here is how Insertion Sort works.
Sorting Algorithms – Insertion Sort 23
❖ Here is a method that implements Insertion Sort.
/** The method for sorting the numbers */
public static void insertionSort(int[] list) {
for (int i = 1; i < list.length; i++) {
/**
* insert list[i] into a sorted sublist list[0..i-1]
* so that list[0..i] is sorted.
*/
int currentElement = list[i];
int k;
for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {
list[k + 1] = list[k];
}
// Insert the current element into list[k+1]
list[k + 1] = currentElement;
}
}
24
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Sorting Algorithms – Bubble Sort 25
❖ The bubble sort algorithm makes several passes through the array. On each pass,
successive neighboring pairs are compared. If a pair is in decreasing order, its values are
swapped; otherwise, the values remain unchanged. The technique is called a bubble sort
or sinking sort, because the smaller values gradually “bubble” their way to the top and
the larger values sink to the bottom. After the first pass, the last element becomes the
largest in the array. After the second pass, the second-to-last element becomes the
second largest in the array. This process is continued until all elements are sorted.
Sorting Algorithms – Bubble Sort 26
❖ The algorithm can be described as follow:

for (int k = 1; k < list.length; k++) {


// Perform the kth pass
for (int i = 0; i < list.length - k; i++) {
if (list[i] > list[i + 1])
swap list[i] with list[i + 1];
}
}
Sorting Algorithms – Bubble Sort 27
❖ Here is an example of Bubble Sort.
Sorting Algorithms – Bubble Sort 28
❖ And here is a method that implements Bubble Sort.
/** Bubble sort method */
public static void bubbleSort(int[] list) {
boolean needNextPass = true;
for (int k = 1; k < list.length && needNextPass; k++) {
// Array may be sorted and next pass not needed
needNextPass = false;
for (int i = 0; i < list.length - k; i++) {
if (list[i] > list[i + 1]) {
// Swap list[i] with list[i + 1]
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
needNextPass = true; // Next pass still needed
}
}
}
}
29
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Sorting Algorithms – Merge Sort 30
❖ 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. This figure illustrates a merge sort of an array of
eight elements (2 9 5 4 8 1 6 7). The original array is
split into (2 9 5 4) and (8 1 6 7). Apply a merge sort
on these two subarrays recursively to split (2 9 5 4)
into (2 9) and (5 4) and (8 1 6 7) into (8 1) and (6 7).
This process continues until the subarray contains
only one element. For example, array (2 9) is split
into the subarrays (2) and (9). Since array (2)
contains a single element, it cannot be further split.
Now merge (2) with (9) into a new sorted array (2 9);
merge (5) with (4) into a new sorted array (4 5).
Merge (2 9) with (4 5) into a new sorted array (2 4 5
9), and finally merge (2 4 5 9) with (1 6 7 8) into a
new sorted array (1 2 4 5 6 7 8 9).
Sorting Algorithms – Merge Sort 31
❖ The algorithm can be described as follow:

public static void mergeSort(int[] list) {


if (list.length > 1) {
mergeSort(list[0 ... list.length / 2]);
mergeSort(list[list.length / 2 + 1 ... list.length]);
merge list[0 ... list.length / 2] with
list[list.length / 2 + 1 ... list.length];
}
}
Sorting Algorithms – Bubble Sort 32
❖ Here is an example of Merge Sort.
Sorting Algorithms – Merge Sort 33
❖ And here is a method that implements Merge Sort.
/** The method for sorting the numbers */
public static void mergeSort(int[] list) {
if (list.length > 1) {
// Merge sort the first half
int[] firstHalf = new int[list.length / 2];
System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
mergeSort(firstHalf);
// Merge sort the second half
int secondHalfLength = list.length - list.length / 2;
int[] secondHalf = new int[secondHalfLength];
System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);
mergeSort(secondHalf);
// Merge firstHalf with secondHalf into list
merge(firstHalf, secondHalf, list);
}
}
34
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Sorting Algorithms – Quick Sort 35
❖ Quick Sort 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.
❖ There are mainly three steps in the algorithm:
❑ Choose a Pivot: Select an element from the array as the pivot. The choice of pivot can vary (e.g., first
element, last element, random element, or median).
❑ Partition the Array: Rearrange the array around the pivot. After partitioning, all elements smaller
than the pivot will be on its left, and all elements greater than the pivot will be on its right. The pivot
is then in its correct position, and we obtain the index of the pivot.
❑ Recursively Call: Recursively apply the same process to the two partitioned sub-arrays (left and right
of the pivot).
❑ Base Case: The recursion stops when there is only one element left in the sub-array, as a single
element is already sorted.
Sorting Algorithms – Quick Sort 36
❖ The algorithm can be described as follow:

public static void quickSort(int[] list) {


if (list.length > 1) {
select a pivot;
partition list into list1 and list2 such that
all elements in list1 <= pivot and
all elements in list2 > pivot;
quickSort(list1);
quickSort(list2);
}
}
Sorting Algorithms – Quick Sort 37
❖ And here is an example using Quick Sort.
Sorting Algorithms – Quick Sort 38
❖ And here is a method that implements Quick Sort.
/** The method for sorting the numbers */
public static void quickSort(int[] list) {
quickSort(list, 0, list.length - 1);
}
private static void quickSort(int[] list, int first, int last) {
if (last > first) {
int pivotIndex = partition(list, first, last);
quickSort(list, first, pivotIndex - 1);
quickSort(list, pivotIndex + 1, last);
}
}
/** Partition the array list[first..last] */
private static int partition(int[] list, int first, int last) {
// The code goes here
}
39

Organizing data through sorting is essential for efficient processing and


retrieval. When data is sorted, it not only makes the information more
manageable but also speeds up various operations, including searches.
However, sorting is just the foundation. Once data is sorted, or even if it
remains unsorted, a frequent task is finding specific elements within the
array. This is where search algorithms play a crucial role, determining
how quickly and efficiently we can locate the desired information.
Searching Algorithms 40
❖ In the process of organizing and managing information, data search plays a role just as
important as data storage and arrangement. Quickly and accurately retrieving
information is essential for effective management. As the volume of data grows, fast
search capabilities allow employees to easily access the necessary information, thereby
improving decision-making speed and overall work efficiency. Moreover, efficient
search helps minimize potential errors in data management processes and supports
accurate information analysis, providing a solid foundation for making sound strategies.
Searching Algorithms 41
❖ A practical example of search application is a computer dictionary. In the dictionary,
there are many entries, and the key to each entry is the spelling of the word. The
accompanying information includes the definition of the word, its pronunciation, and
other details such as the word type, synonyms, antonyms, etc.
Searching Algorithms 42
❖ Another example of a search application: a bank stores records of customer information
and wants to find a record of a particular customer in this list to check balances and
perform transactions.
Searching Algorithms 43
❖ So, it can be seen that searching plays an important role in data organization structure. In
this lecture, we will learn some popular searching algorithms:
44
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Searching Algorithms – Linear Search 45
❖ Linear Search, also known as Sequential Search, is a very simple method used to
search an array for a specific value. It works by comparing the value to be searched
against each element of the array in sequence until a match is found. Linear search is
primarily used to search an unordered list of elements (arrays where the data elements
are not sorted).
Searching Algorithms – Linear Search 46
❖ The algorithm can be described as follow:
❑ In Steps 1 and 2 of the algorithm, we initialize
the value of POS and I.
❑ In Step 3, a while loop is executed that would be
executed till I is less than N (total number of
elements in the array).
❑ In Step 4, a check is made to see if a match is
found between the current array element and
VAL. If a match is found, then the position of
the array element is printed, else the value of I is
incremented to match the next element with
VAL.
❑ However, if all the array elements have been
compared with VAL and no match is found, then
it means that VAL is not present in the array.
Searching Algorithms – Linear Search 47
❖ We can simplify the above algorithm with the following
basic steps:
Step 1: Set i to 0
Step 2: If i > n-1 then go to step 7
Step 3: If A[i] = K then go to step 6
Step 4: Set i to i + 1
Step 5: Go to step 2
Step 6: Print “Element K found at index i” and go to step 8
Step 7: Print “Element not found”
Step 8: Exit
Searching Algorithms – Linear Search 48
❖ The Linear Search can be represented as follows:
Searching Algorithms – Linear Search 49
❖ Write a method to search an element in an array using the linear search technique.
public static int linearSearch(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
Practice:
Linear Search
51
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Searching Algorithms – Binary Search 52
❖ Binary search is an efficient search algorithm
that works with sorted lists. Its mechanism can
be understood more clearly through an analogy
with a dictionary.
❑ First, we open the dictionary somewhere in the
middle. Then, we compare the first word on that
page with the word we’re looking for.
❑ If the desired word comes before the word on the
page, we’ll search the first half of the dictionary. If
not, we’ll search the second half.
❑ Again, we open a page in the relevant half of the
dictionary and compare the first word on that page
with the desired word, repeating this process until
we eventually find the word.
Searching Algorithms – Binary Search 53
❖ The algorithm can be described as follow:
❑ In Step 1, we initialize the value of variables, BEG, END,
and POS.
❑ In Step 2, a while loop is executed until BEG is less than or
equal to END.
❑ In Step 3, the value of MID is calculated.
❑ In Step 4, we check if the array value at MID is equal to
VAL (item to be searched in the array). If a match is found,
then the value of POS is printed and the algorithm exits.
However, if a match is not found, and if the value of
A[MID] is greater than VAL, the value of END is modified,
otherwise if A[MID] is greater than VAL, then the value of
BEG is altered.
❑ In Step 5, if the value of POS = –1, then VAL is not present
in the array and an appropriate message is printed on the
screen before the algorithm exits.
Searching Algorithms – Binary Search 54
❖ We can simplify the above algorithm into the following five basic steps:
1. Initialization: Define the index of the first element (left) and the last
element (right) of the list.
2. Calculate the middle index (mid): Compute the middle index as mid
= (left + right) / 2 (or use mid = left + (right - left) / 2 to avoid
overflow).
3. Comparison: Compare the element at the middle index (arr[mid])
with the target value:
❑ If arr[mid] == target: Return the index mid since the target value
has been found.
❑ If arr[mid] > target: Narrow the search to the left side by setting
right = mid - 1.
❑ If arr[mid] < target: Narrow the search to the right side by setting
left = mid + 1.
4. Repeat: Continue repeating steps 2 and 3 until the target element is
found or the left index exceeds the right.
5. Conclusion: If the value is not found in the list, return a result
indicating the value is not present.
Searching Algorithms – Binary Search 55
❖ The Binary Search can be represented as follows:
Searching Algorithms – Binary Search 56
❖ Write a method to search an element in an array using the binary search technique.
// Returns index of key if it is present in arr[].
public static int binarySearch(int arr[], int key) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
// Check if key is present at mid
if (arr[mid] == key)
return mid;
// If key greater, ignore left half
if (arr[mid] < key)
low = mid + 1;
// If key is smaller, ignore right half
else
high = mid - 1;
}
// If we reach here, then element was
// not present
return -1;
}
Practice:
Binary Search
how do they actually compare in practice?
Let's find out by a quick demo

58
Linear Search vs. Binary Search 59
60

Next, let's take a quick look at some advanced search algorithms like
Ternary Search, Jump Search, Exponential Search, and Fibonacci
Search. These algorithms are highly efficient for searching through
sorted data. Given our limited time, we'll focus on providing a brief
overview of how each one works, rather than diving into complex
details such as time complexity, or data structures, etc. We'll cover
those more thoroughly in the advanced lessons.
61
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Searching Algorithms – Ternary Search 62
❖ Computer systems use different methods to find specific data. There are various search
algorithms, each better suited for certain situations. For instance, a binary search
divides information into two parts, while a ternary search does the same but into three
equal parts. It’s worth noting that ternary search is only effective for sorted data.
❖ Ternary search is a search algorithm that is used to find the position of a target value
within a sorted array. It operates on the principle of dividing the array into three parts
instead of two, as in binary search. The basic idea is to narrow down the search space
by comparing the target value with elements at two points that divide the array into
three equal parts:
❑ mid1 = left + (right - left) / 3
❑ mid2 = right – (right - left) / 3
Searching Algorithms – Ternary Search 63
❖ The Ternary Search can be represented as follows:
Searching Algorithms – Ternary Search 64
❖ Write a method to search an element in an array using the ternary search technique.
// Function to perform Ternary Search
public static int ternarySearch(int[] arr, int left, int right, int target) {
if (right >= left) {
int mid1 = left + (right - left) / 3;
int mid2 = right - (right - left) / 3;
// Check if target is at mid1
if (arr[mid1] == target) {
return mid1;
}
// Check if target is at mid2
if (arr[mid2] == target) {
return mid2;
}
// If target is in the left one-third
if (target < arr[mid1]) {
return ternarySearch(arr, left, mid1 - 1, target);
}
// If target is in the right one-third
if (target > arr[mid2]) {
return ternarySearch(arr, mid2 + 1, right, target);
}
// If target is in the middle one-third
return ternarySearch(arr, mid1 + 1, mid2 - 1, target);
}
// Target is not found
return -1;
}
Practice:
Ternary Search
66
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Searching Algorithms – Jump Search 67
❖ Like Binary Search, Jump Search is a searching algorithm for sorted arrays. The basic
idea is to check fewer elements (than linear search) by jumping ahead by fixed steps or
skipping some elements in place of searching all elements.
❖ For example, suppose we have an array arr[] of size n and a block (to be jumped) of
size m. Then we search in the indexes arr[0], arr[m], arr[2m]…..arr[km], and so on.
Once we find the interval (arr[km] < x < arr[(k+1)m]), we perform a linear search
operation from the index km to find the element x.
Searching Algorithms – Jump Search 68
❖ The Jump Search can be represented as follows:
Searching Algorithms – Jump Search 69
❖ Write a method to search an element in an array using the jump search technique.
// Method to perform Jump Search
public static int jumpSearch(int[] arr, int target) {
int n = arr.length;
// Finding the block size to be jumped
int step = (int) Math.floor(Math.sqrt(n));
// Finding the block where the element may be present
int prev = 0;
while (arr[Math.min(step, n) - 1] < target) {
prev = step;
step += (int) Math.floor(Math.sqrt(n));
if (prev >= n)
return -1; // Element is not present
}
// Doing a linear search for target in the block starting with prev
while (arr[prev] < target) {
prev++;
// If we reached the next block or end of array
if (prev == Math.min(step, n))
return -1; // Element is not present
}
// If element is found
if (arr[prev] == target)
return prev;
return -1; // Element is not present
}
Practice:
Jump Search
71
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Searching Algorithms – Exponential Search 72
❖ Exponential Search focuses on a specific range of an input array, assuming that the desired element is
within that range, and performs a binary search on the specific smaller range. This algorithm is also known
as Doubling Search or Finger Search.
❖ It works similarly to Jump Search in that it divides the sorted input into blocks and performs a search on
a smaller scale. However, the difference lies in how the blocks are divided and the type of search applied
on a smaller scale (Jump Search applies linear search, while Exponential Search applies binary search).
❖ Therefore, this algorithm “jumps” exponentially using powers of 2. Simply put, the search is conducted on
blocks divided by pow(2, k), where 𝑘 is a non-negative integer. When the element at position pow(2, n)
exceeds the key, a binary search is performed on the current block.
Searching Algorithms – Exponential Search 73
❖ The Exponential Search can be represented as follows:
Begin
m := pow(2, k) // m is the block size
start := 1
low := 0
high := size – 1 // size is the size of input
if array[0] == key
return 0
while array[m] <= key AND m < size do
start := start + 1
m := pow(2, start)
while low <= high do:
mid = low + (high - low) / 2
if array[mid] == x
return mid
if array[mid] < x
low = mid + 1
else
high = mid - 1
done
return invalid location
End
Searching Algorithms – Exponential Search 74
❖ Write a method to search an element in an array using the exponential search technique.
// Function to perform binary search within a given range
public static int binarySearch(int[] arr, int left, int right, int key) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key)
return mid;
if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return -1; // Key not found
}
// Function to perform exponential search
public static int exponentialSearch(int[] arr, int key) {
int n = arr.length;
// If the element is at the first position
if (arr[0] == key)
return 0;
// Find range for binary search by repeated doubling
int i = 1;
while (i < n && arr[i] <= key)
i = i * 2;
// Perform binary search within the found range
return binarySearch(arr, i / 2, Math.min(i, n - 1), key);
}
Practice:
Exponential Search
76
❖ Sorting Algorithms
❑ Selection Sort
❑ Insertion Sort
❑ Bubble Sort
❑ Merge Sort
❑ Quick Sort
❖ Searching Algorithms
❑ Linear Search
❑ Binary Search
❑ Ternary Search
❑ Jump Search
❑ Exponential Search
❑ Fibonacci Search
❖ Final Touches
Searching Algorithms – Fibonacci Search 77
❖ The Fibonacci Search is a technique for searching a sorted array using a divide-and-conquer algorithm
that compresses the possible positions with Fibonacci numbers. Compared to the Binary Search, where
the sorted array is divided into two equally sized arrays, one of which is searched further, the Fibonacci
Search divides the array into two parts whose size corresponds to successive Fibonacci numbers. On
average, this means that around 4% more comparisons have to be carried out. However, it has the
advantage that only addition and subtraction are required to calculate the indices of the called array
elements.
❖ In addition, the conventional Binary Search requires bit-shift, division or multiplication operations, which
were less common at the time of the introduction of the Fibonacci Search. The Fibonacci Search has an
average and worst-case complexity of O(log n). The Fibonacci sequence has the property that a number
is the sum of its two incestors. Therefore, the sequence can be generated by repeated addition. The binary
search works by dividing the search area into equal parts (1:1). The Fibonacci Search can divide the
search range into parts that approximate 1:1.618, using simpler operations.
Searching Algorithms – Fibonacci Search 78
❖ The Fibonacci Search can be represented as follows:

❖ For example, we want to find the value 85:


Searching Algorithms – Fibonacci Search 79
❖ Write a method to search an element in an array using the fibonacci search technique.
// Fibonacci Search Method
public static int fibonacciSearch(int arr[], int x) {
int n = arr.length;
// Initialize Fibonacci numbers
int fibMMm2 = 0; // (m-2)'th Fibonacci number
int fibMMm1 = 1; // (m-1)'th Fibonacci number
int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci number
// Find the smallest Fibonacci number greater than or equal to n
while (fibM < n) {
fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}
// Marks the eliminated range from the front
int offset = -1;
// While there are elements to be inspected
while (fibM > 1) {
// Check if fibMMm2 is a valid index
int i = min(offset + fibMMm2, n - 1);
// If x is greater than the value at index i, cut the subarray from offset to i
if (arr[i] < x) {
fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
}
// If x is less than the value at index i, cut the subarray after i
else if (arr[i] > x) {
fibM = fibMMm2;
fibMMm1 -= fibMMm2;
fibMMm2 = fibM - fibMMm1;
}
// Element found
else {
return i;
}
}
// Compare the last element with x
if (fibMMm1 == 1 && arr[offset + 1] == x) {
return offset + 1;
}
// Element not found
return -1;
}
Practice:
Fibonacci Search
81

Besides these search algorithms, there are many other


algorithms that you can explore on your own. These algorithms
help solve complex search problems more efficiently in various
situations. You can look into algorithms such as Tree Search,
Binary Tree Search, Fuzzy Search, Depth-First Search, or
Breadth-First Search, among others. Make an effort to study
and apply this knowledge to real-world problems, while also
seeking out new algorithms to enhance your skills.
Challenge 82
❖ Compare all the aforementioned algorithms, draw specific flowcharts for each type, and conclude:
when should we apply which algorithm to optimize the software development process?
Now that you have learned
how to implement Sorting
and Searching Algorithms.
Do you have any question?
If you don’t, in the next
section, I will show the
summary of this lecture...
Summary 84
1. The worst-case complexity for a selection sort, insertion sort, bubble sort, and quick sort
is O(𝑛2 ).
2. The average-case and worst-case complexity for a merge sort is O(nlogn). The average
time for a quick sort is also O(nlogn).
3. Heaps are a useful data structure for designing efficient algorithms such as sorting. You
learned how to define and implement a heap class, and how to insert and delete elements
to/from a heap.
4. The time complexity for a heap sort is O(nlogn).
5. Bucket sorts and radix sorts are specialized sorting algorithms for integer keys. These
algorithms sort keys using buckets rather than by comparing keys. They are more efficient
than general sorting algorithms.
6. A variation of the merge sort - called an external sort - can be applied to sort large
amounts of data from external files.
Summary 85
❖ In this lecture we have presented a few searching algorithms. We have presented more
efficient searching algorithms earlier on, like for instance the logarithmic searching
algorithm. Searching algorithms and their efficiency largely depends on the underlying
data structure being used to store the data. For instance, it is quicker to determine
whether an item is in a hash table than it is an array, similarly it is quicker to search a
binary search tree than it is a linked list.
❖ If you are going to search for data fairly often then we strongly advise that you sit down
and research the data structures available to you. In most cases using a list or any other
primarily linear data structure is down to lack of knowledge. Model your data and then
research the data structures that best fit your scenario.
This brings us to the
end of this lecture!
It’s time for Final
Touches…

86
Final Touches 87
❖ The Fibonacci Search Algorithm makes use of the Fibonacci Series to diminish the range of an array on
which the searching is set to be performed. With every iteration, the search range decreases making it
easier to locate the element in the array. The detailed procedure of the searching is seen below:
❖ Step 1 − As the first step, find the immediate Fibonacci number that is greater than or equal to the size of the
input array. Then, also hold the two preceding numbers of the selected Fibonacci number, that is, we hold Fm,
Fm-1, Fm-2 numbers from the Fibonacci Series.
❖ Step 2 − Initialize the offset value as -1, as we are considering the entire array as the searching range in the
beginning.
❖ Step 3 − Until Fm-2 is greater than 0, we perform the following steps:
❑ Compare the key element to be found with the element at index [min(offset+Fm-2,n-1)]. If a match is found, return the
index.
❑ If the key element is found to be lesser value than this element, we reduce the range of the input from 0 to the index of
this element. The Fibonacci numbers are also updated with Fm = Fm-2.
❑ But if the key element is greater than the element at this index, we remove the elements before this element from the
search range. The Fibonacci numbers are updated as Fm = Fm-1. The offset value is set to the index of this element.
❖ Step 4 − As there are two 1s in the Fibonacci series, there arises a case where your two preceding numbers will
become 1. So if Fm-1 becomes 1, there is only one element left in the array to be searched. We compare the key
element with that element and return the 1st index. Otherwise, the algorithm returns an unsuccessful search.
88

Thanks!
Any questions?
For an in-depth understanding of Java, I highly recommend
referring to the textbooks. This slide provides a brief overview
and may not cover all the details you're eager to explore!

You might also like