0% found this document useful (0 votes)
11 views

DSA Java Day13 SortAlgoPart2 SearchingAlgo

Uploaded by

Sameeksh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

DSA Java Day13 SortAlgoPart2 SearchingAlgo

Uploaded by

Sameeksh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Sorting and Searching

Algo. Insertion Sort Algorithm

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.

A similar approach is used by insertion sort.

Insertion sort is a sorting algorithm that places an unsorted element at its


suitable place in each iteration.

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.

How Insertion Sort Works?


Suppose we need to sort the following
array.

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 first element in the array is assumed


to be sorted.
Take the second element and store it
separately in key.

Compare key with the first element. If


the first element is greater than key, then
key is placed in front of the first 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, the first two elements are sorted.

Take the third element and compare it with


the elements on the left of it. Placed it just
behind the element smaller than it. If there
is no element smaller than it, then place it
at the beginning of the array.

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.

Similarly, place every unsorted element at


its correct position

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.

Here, a problem is divided into multiple sub-problems. Each sub-problem is


solved individually. Finally, sub-problems are combined to form the final
solution.

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 Algorithm

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.

The merge Step of Merge Sort

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.

The merge function works as follows:

• Create copies of the subarrays L ← A[p..q] and M ← A[q+1..r].


• Create three pointers i, j and k
• i maintains current index of L, starting at 1
• j maintains current index of M, starting at 1
• k maintains the current index of A[p..q], starting at p.
• Until we reach the end of either L or M, pick the larger among the elements from L
and M and place them in the correct position at A[p..q]
• When we run out of elements in either L or M, pick up the remaining elements and
put in A[p..q]

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

1 {12, 23,2,43,51,35,19,4 } {12,23,2,43} {}


{51,35,19,4}

2 {12,23,2,43} {12,23}{2,43} {}
{51,35,19,4} {51,35}{19,4}

3 {12,23}{2,43} {12,23} {2,43} {12,23} {2,43}


{51,35}{19,4} {35,51}{4,19} {35,51}{4,19}

4 {12,23} {2,43} {2,12,23,43} {2,12,23,43}


{35,51}{4,19} {4,19,35,51} {4,19,35,51}

5 {2,12,23,43} {2,4,12,19,23,35,43,51} {2,4,12,19,23,35,43,51}


{4,19,35,51}

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.

Quicksort algorithm is based on the divide and conquer approach where:

1.An array is divided into subarrays by selecting a pivot element (element


selected from the array).
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.

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.

2. Rearrange the Array


Now the elements of the array are rearranged so that elements that are smaller
than the pivot are put on the left and the elements greater than the pivot are
put on the right.

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.

Finally, the pivot element is swapped with the second pointer.

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.

How Linear Search Works?


The following steps are followed to search for an element k = 1 in
the list below.

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.

Start from the first element, compare k with each element x

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 == k, return the index.

Else, return 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
Binary Search
Algo.

Binary Search is a searching algorithm for finding an element's position in a


sorted array.

In this approach, the element is always searched in the middle of a portion of


an array.

Binary search can be implemented only on a sorted list of items. If the


elements are not sorted already, we need to sort them first.

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.

Binary Search Working

Binary Search Algorithm can be implemented in two ways which are

discussed below.

1.Iterative Method

2.Recursive Method

The recursive method follows the divide and conquer approach.

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

Let x = 4 be the element to be searched.

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.

Binary Search Algorithm

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

You might also like