DSA Java Day13 SortAlgoPart2 SearchingAlgo
DSA Java Day13 SortAlgoPart2 SearchingAlgo
Insertion sort works similarly as we sort cards in our hand in a card game.
We assume that the first card is already sorted then, we select an unsorted
card. If the unsorted card is greater than the card in hand, it is placed on the
right otherwise, to the left.
In the same way, other unsorted cards are taken and put at their right place.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Merge Sort Algorithm
Algo.
Merge Sort is one of the most popular sorting algorithms that is based on the
principle of Divide and Conquer Algorithm.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
The MergeSort function repeatedly divides the array into two halves until we
reach a stage where we try to perform MergeSort on a subarray of size 1 i.e. p
== r.
After that, the merge function comes into play and combines the sorted arrays
into larger arrays until the whole array is merged.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Every recursive algorithm is dependent on a base case and the ability to combine the
results from base cases. Merge sort is no different. The most important part of the merge
sort algorithm is, you guessed it, merge step.
The merge step is the solution to the simple problem of merging two sorted lists(arrays) to
build one large sorted list(array).
The algorithm maintains three pointers, one for each of the two arrays and one for
maintaining the current index of the final sorted array.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Merge( ) Function Explained Step-By-
Algo.
Step
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Step 1: Create duplicate copies of sub-arrays to be
sorted
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Step 2: Maintain current index of sub-arrays and main
array
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Step 3: Until we reach the end of either L or M, pick larger among elements L
and M and place them in the correct position at A[p..r]
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Step 4: When we run out of elements in either L or M, pick up the remaining
elements and put in A[p..r]
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Pass Unsorted list divide Sorted list
2 {12,23,2,43} {12,23}{2,43} {}
{51,35,19,4} {51,35}{19,4}
6 {} {} {2,4,12,19,23,35,43,51}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Quicksort Algorithm
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching Working: Quicksort Algorithm
Algo. 1. Select the Pivot Element
There are different variations of quicksort where the pivot element is selected from
different positions. Here, we will be selecting the rightmost element of the array as
the pivot element.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Here's how we rearrange the array:
A pointer is fixed at the pivot element. The pivot element is compared with the
elements beginning from the first index.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
If the element is greater than the pivot element, a second pointer is set for that
element.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Now, pivot is compared with other elements. If an element smaller than the pivot
element is reached, the smaller element is swapped with the greater element found
earlier.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Again, the process is repeated to set the next greater element as the second
pointer. And, swap it with another smaller element.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
The process goes on until the second last element is reached.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Divide Subarrays
Pivot elements are again
chosen for the left and
the right sub-parts
separately. And, step
2 is repeated.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Visual Illustration of Quicksort
Algo.
Algorithm
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Linear Search
Algo.
Linear search is the simplest searching algorithm that searches for an element in
a list in sequential order. We start at one end and check every element until the
desired element is not found.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Binary Search
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
discussed below.
1.Iterative Method
2.Recursive Method
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
The array in which searching is to be performed is
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Set two pointers low and high at the lowest and the highest positions
respectively
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Find the middle element mid of the array ie. arr[(low + high)/2] = 6
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
• If x == mid, then return mid. Else, compare the element to be searched with m.
• If x > mid, compare x with the middle element of the elements on the right side of
mid. This is done by setting low to low = mid + 1.
Else, compare x with the middle element of the elements on the left side of mid. This is
done by setting high to high = mid - 1.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Iteration Method
do until the pointers low and high meet each other.
mid = (low + high)/2
if (x == arr[mid])
return mid
else if (x > arr[mid]) // x is on the right side
low = mid + 1
else // x is on the left side
high = mid - 1
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.
Recursive Method
binarySearch(arr, x, low, high)
if low > high
return False
else
mid = (low + high) / 2
if x == arr[mid]
return mid
else if x > arr[mid] // x is on the right side
return binarySearch(arr, x, mid + 1, high)
else // x is on the right side
return binarySearch(arr, x, low, mid - 1)
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video