Day 5 - Basic Algorithms
Day 5 - Basic Algorithms
Day 5 - Basic Algorithms
Structure
Algorithms
What’s algorithms?
Objectives Search Algorithms
Sorting Algorithms
Algorithms
A finite sequence of rigorous instructions, typically used to solve a class of
specific problems or to perform a computation.
Example:
Ethiopian binary math:
a Shaman in Ethiopian villages:1941
Algorithms provide step by step instructions on solving specific problems. They
help you solve problems using efficient, standard, and reusable steps
Searching Algorithms
Searching in data structure refers to the process of finding the required
information from a collection of items stored as elements in the computer
memory.
Based on the type of search operation, these algorithms are generally classified
into two categories:
1. Sequential Search: In this, the list or array is traversed sequentially and every
element is checked. For example: Linear Search.
2. Interval Search: These type of searching algorithms are much more efficient
than Linear Search as they repeatedly target the center of the search
structure and divide the search space in half. For Example: Binary Search.
Linear Search in Java
Algorithm:
Algorithm:
1. Compare x with the middle element.
2. If x matches with the middle element, we return the mid index.
3. Else If x is greater than the mid element, then x can only lie in the right half
subarray after the mid element. So we recur for the right half.
4. Else (x is smaller) recur for the left half.
Binary Search
steps
Sort the Array
Assume we're looking for is 13
Choose Low and High Values
The first index of the array will be denoted as low while the highest index will
be denoted as high.
Choose Midpoint/Middle Element
Midpoint = (low + high) / 2.
Now the search begins
If Key item (number_to_search_for) is equal to midpoint, the midpoint index
will be returned.
If Key item is greater than midpoint, search through the elements on the right
side of the midpoint.
If Key item is less than midpoint, search through the elements on the left side
midpoint.
Iteration #1
Iteration #2
Code in Java
int binarySearch(int[] numsArray, int key) {
int lowIx = 0;
int highIx = numsArray.length - 1;
while (lowIx <= highIx) {
int midIx = (lowIx + highIx) / 2;
if (key == numsArray[midIx]) {
return midIx;
}
if (key < numsArray[midIx]) {
highIx = midIx - 1;
}
if (key > numsArray[midIx]) {
lowIx = midIx + 1;
}
}
return -1;
} // In a case where the element doesn't exist, -1 will be returned
Sorting Algorithms
A Sorting Algorithm is used to rearrange a given array or list elements according
to a comparison operator on the elements. The comparison operator is used to
decide the new order of element in the respective data structure.
You can use various ordering criteria, common ones being sorting numbers
from least to greatest or vice-versa
Types of Sorting Algorithm
There are numerous types of sorting in data structures some of which are
1. Bubble Sort
2. Selection sort
3. Insertion sort
4. Merge sort
Selection Sort
Selection Sort also divides the array into a sorted and unsorted subarray.
Though, this time, the sorted subarray is formed by inserting the minimum
element of the unsorted subarray at the end of the sorted array, by swapping.
As the algorithm progresses, the biggest items “bubble up” to the top end of
the array.
The end of the 1st pass
.
After this first pass through all the data, you’ve made N-1 comparisons and
somewhere between 0 and N-1 swaps, depending on the initial arrangement of
the players. The item at the end of the array is sorted and won’t be moved
again. Now you go back and start another pass from the left end of the line.
Again, you go toward the right, comparing and swapping when appropriate.
However, this time you can stop one player short of the end of the line, at
position N-2, because you know the last position, at N-1, already contains the
tallest player
Bubble sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping
the adjacent elements if they are in wrong order.
Example ( 5 1 4 2 8 )
Code in Java
public void bubblesort(int[] bubble) {
for(int i = bubble.length - 1; i >= 0; i--) {
for(int j = 0; j < i; j++) {
//do swap adjacent files after comparison
if(bubble[j] > bubble[j + 1]) {
int temp = bubble[j];
bubble[j] = bubble[j + 1];
bubble[j + 1] = temp;
}
}
}
}
Insertion Sort
Insertion sort is a simple sorting algorithm that works similar to the way you
sort playing cards in your hands.
The array is virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed at the correct position in the sorted part.
Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements
before. Move the greater elements one position up to make space for the
swapped element.
Insertion Sort: Have you ever played cards?
Example
logic
public void sort(int [] numbers) {
for(int i= 1; i < numbers.length; i++) {
int key = numbers[i];
int j = i - 1;
While dividing the array, the pivot element should be positioned in such a way
that elements less than pivot are kept on the left side and elements greater than
pivot are on the right side of the pivot.
2.The left and right subarrays are also divided using the same approach. This
process continues until each subarray contains a single element.
3.At this point, elements are already sorted. Finally, elements are combined to
form a sorted array.
References
https://www.javatpoint.com/abstract-class-in-java
https://www.javatpoint.com/linear-search-in-java
https://stackabuse.com/sorting-algorithms-in-java/
https://www.javatpoint.com/binary-search-in-java
https://www.geeksforgeeks.org/bubble-sort/