8.search and Sorting
8.search and Sorting
• Set two pointers low and high at the lowest and the highest positions
respectively.
• 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.
• x = 4 is found
• The following steps are followed to search for an element k = 1 in the list below.
• 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
• 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
• 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.
• 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
2. Remaining Iteration
• 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.
• 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.
• 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.
• To maintain the max-heap property for the entire tree, we will have to keep
pushing 2 downwards until it reaches its correct position.
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 38