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

8.search and Sorting

The document discusses various searching and sorting algorithms, including binary search, linear search, selection sort, insertion sort, bubble sort, and heap sort. Binary search uses pointers to efficiently search through sorted data by comparing the search key to the middle element of the array. Linear search sequentially checks each element to find a match. Selection, insertion, and bubble sorts are different approaches to sorting data by comparing and swapping elements. Heap sort uses a heap data structure represented as a complete binary tree to sort elements.

Uploaded by

Patil Vinayak
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)
55 views

8.search and Sorting

The document discusses various searching and sorting algorithms, including binary search, linear search, selection sort, insertion sort, bubble sort, and heap sort. Binary search uses pointers to efficiently search through sorted data by comparing the search key to the middle element of the array. Linear search sequentially checks each element to find a match. Selection, insertion, and bubble sorts are different approaches to sorting data by comparing and swapping elements. Heap sort uses a heap data structure represented as a complete binary tree to sort elements.

Uploaded by

Patil Vinayak
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/ 38

Searching and Sorting

1 June 2022 C-DAC 1


Binary Search

• Let x = 4 be the element to be searched.

• Set two pointers low and high at the lowest and the highest positions
respectively.

1 June 2022 C-DAC 2


Binary Search
• Find the middle element mid of the array ie. arr[(low + high)/2] = 6.

• 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.

1 June 2022 C-DAC 3


Binary Search
• Repeat steps 3 to 6 until low meets high.

• x = 4 is found

1 June 2022 C-DAC 4


Linear Search
• Linear search is a sequential searching algorithm where to start from one end and
check every element of the list until the desired element is found. It is the
simplest searching algorithm.
• LinearSearch(array, key)
• for each item in the array
• if item == value
• return its index

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

1 June 2022 C-DAC 5


Linear Search
• Start from the first element, compare k with each element x

• If x == k, return the index.

• Else, return not found.

1 June 2022 C-DAC 6


Sorting

1 June 2022 C-DAC 7


Sorting
• Rearranges 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.
• Selection sort
• Insertion sort
• Bubble sort
• Heap sort
• Merge sort
• Quick sort

1 June 2022 C-DAC 8


Selection Sort

• Selection sort is a sorting algorithm that selects the smallest element from an
unsorted list in each iteration and places that element at the beginning of the
unsorted list.
selection Sort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort

1 June 2022 C-DAC 9


Working of Selection Sort
• Set the first element as minimum

1 June 2022 C-DAC 10


Working of Selection Sort
• Compare minimum with the second element. If the second element is smaller
than minimum, assign the second element as minimum.
• Compare minimum with the third element. Again, if the third element is smaller,
then assign minimum to the third element otherwise do nothing. The process
goes on until the last element.

1 June 2022 C-DAC 11


Working of Selection Sort
• After each iteration, minimum is placed in the front of the unsorted list

• For each iteration, indexing starts


from the first unsorted element. Step
1 to 3 are repeated until all the
elements are placed at their correct
positions.

1 June 2022 C-DAC 12


Working of Selection Sort

1 June 2022 C-DAC 13


Insertion Sort
• Insertion sort places an unsorted element at its suitable place in each iteration.

• Insertion sort works similarly as sort cards in our hand in a card game. Assume
that the first card is already sorted then, 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 in their right place.
insertion Sort(array)
mark first element as sorted
for each unsorted element X
'extract' the element X
for j <- lastSortedIndex down to 0
if current element j > X
move sorted element to the right by 1
break loop and insert X here
end insertionSort

1 June 2022 C-DAC 14


Insertion Sort

• 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.

1 June 2022 C-DAC 15


Insertion Sort

• 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.

1 June 2022 C-DAC 16


Insertion Sort

• Similarly, place every unsorted element at its correct position

1 June 2022 C-DAC 17


Bubble Sort

• Bubble sort is a sorting algorithm that compares two adjacent elements and
swaps them until they are not in the intended order
bubbleSort(array)
for i <- 1 to indexOfLastUnsortedElement-1
if leftElement > rightElement
swap leftElement and rightElement
end bubbleSort

1 June 2022 C-DAC 18


Bubble Sort

1. First Iteration (Compare and Swap)

• Starting from the first index, compare the first


and the second elements.
• If the first element is greater than the second
element, they are swapped.

• Now, compare the second and the third


elements. Swap them if they are not in order.

• The above process goes on until the last


element

1 June 2022 C-DAC 19


Bubble Sort

2. Remaining Iteration

• The same process goes on for the remaining


iterations.
• After each iteration, the largest element
among the unsorted elements is placed at the
end.

1 June 2022 C-DAC 20


Bubble Sort

• In each iteration, the comparison takes place up to the last unsorted element

• The array is sorted when all the unsorted elements are placed at their correct
positions.

1 June 2022 C-DAC 21


Heap Sort

• Heap Sort is a popular and efficient sorting algorithm in computer


programming. Learning how to write the heap sort algorithm requires
knowledge of two types of data structures - arrays and trees.

• The initial set of numbers that we want to sort is stored in an array e.g. [10, 3,
76, 34, 23, 32] and after sorting, we get a sorted array [3,10,23,32,34,76].

• Heap sort works by visualizing the elements of the array as a special kind of
complete binary tree called a heap.

1 June 2022 C-DAC 22


Heap Sort

• A complete binary tree has an interesting property that can find the children
and parents of any node.
• If the index of any element in the array is i, the element in the index 2i+1 will
become the left child and element in 2i+2 index will become the right child.
Also, the parent of any element at index i is given by the lower bound of
(i-1)/2.

1 June 2022 C-DAC 23


Heap Sort
Heap is a special tree-based data structure. A binary tree is said to follow a heap
data structure if
• it is a complete binary tree
• All nodes in the tree follow the property that they are greater than their
children i.e. the largest element is at the root and both its children and smaller
than the root and so on. Such a heap is called a max-heap. If instead, all nodes
are smaller than their children, it is called a min-heap

1 June 2022 C-DAC 24


Heap Sort
heapify(array)
Root = array[0]
Largest = largest( array[0] , array [2*0 + 1]. array[2*0+2])
if(Root != Largest)
Swap(Root, Largest)

• Two scenarios - one in which the


root is the largest element and
don't need to do anything. And
another in which the root had a
larger element as a child and
needed to swap to maintain
max-heap property.

1 June 2022 C-DAC 25


Heap Sort
• Now let's think of another scenario in which there is more than one level.

1 June 2022 C-DAC 26


Heap Sort
• The top element isn't a max-heap but all the sub-trees are max-heaps.

• To maintain the max-heap property for the entire tree, we will have to keep
pushing 2 downwards until it reaches its correct position.

1 June 2022 C-DAC 27


Merge Sort
• 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.

1 June 2022 C-DAC 28


Merge Sort
• 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.

1 June 2022 C-DAC 29


Merge Sort

1 June 2022 C-DAC 30


Quick Sort

Quicksort is based on the divide and conquer approach where


• 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.
• The left and right subarrays are also divided using the same approach. This
process continues until each subarray contains a single element.
• At this point, elements are already sorted. Finally, elements are combined to
form a sorted array.

1 June 2022 C-DAC 31


Quick Sort

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.

1 June 2022 C-DAC 32


Quick Sort
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.

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.

1 June 2022 C-DAC 33


Quick Sort
If the element is greater than the pivot element, a second pointer is set for that
element.

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.

1 June 2022 C-DAC 34


Quick Sort
Again, the process is repeated to set the next greater element as the second pointer.
And, swap it with another smaller element.

1 June 2022 C-DAC 35


Quick Sort
The process goes on until the second last element is reached..

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

1 June 2022 C-DAC 36


Quick Sort

1 June 2022 C-DAC 37


Thank you . . .
[email protected]

1 June 2022 38

You might also like