Part 8
Part 8
Sorting by selection:
This is one of the easiest sorting algorithms to understand and write but not the most efficient one. In
Selection Sort we start at the first element of the array and go through the array and find the minimum
element. We swap the minimum element with the element at the first place. We start at the second position in
the array and go through the array and find the minimum element in the remaining portion of the array. We
swap that minimum element with the element with the element at the second position. We start at the third
position and repeat the procedure until we reach the end of the array.
Selection Sort is not efficient. It does the same amount searches if the values in the array are in random order,
partially sorted or completely sorted.
Sorting by Exchange:
Let K is an array that consists of n elements from index 1 to n. Sorting refers to the process of
arranging these elements in ascending order such that K[1]≤K[2]≤ - - - ≤K[n]. For this, exchange sort works
as:
Step 1: Compare the first and second and data items. If the first data item is greater than the second data item,
then interchange first and second data items. If the second data item is greater than the third data item,
then interchange second and third data items. The process is repeated till the last data item is reached.
Step 2: When the last data item is reached, it is said to be one pass. At the end of the first pass, the largest
element is bubble out and occupies in last position of the array.
Step 3: Step-1 and Step-2 are repeated for the data items from index 1 to n-1. At the end of the second pass,
the second largest element is bubble out and occupies in n-1th position of the array.
Step 4: The above steps are repeated for n-1 passes. At the end of the n-1th pass, all elements of the array K
are in sorted order.
Algorithm sort (K,n): Function is used to sort the elements of K in ascending order.
Step 2: RETURN
Sorting by Insertion:
Insertion sort is the sorting mechanism where the sorted array is built having one item at a time. The
array elements are compared with each other sequentially and then arranged simultaneously in some particular
order. The analogy can be understood from the style we arrange a deck of cards. This sort works on the
principle of inserting an element at a particular position, hence the name Insertion Sort.
Insertion Sort works as follows:
1. The first step involves the comparison of the element in question with its adjacent element.
2. And if at every comparison reveals that the element in question can be inserted at a particular position,
then space is created for it by shifting the other elements one position to the right and inserting the element
at the suitable position.
3. The above procedure is repeated until all the element in the array is at their apt position.
INSERTION-SORT(A)
for i = 1 to n
key ← A [i]
j←i–1
while j > = 0 and A[j] > key
A[j+1] ← A[j]
j←j–1
End while
A[j+1] ← key
End for
Sorting by Partition:
QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given
array around the picked pivot. There are many different versions of quickSort that pick pivot in different
ways.
SET Lo to 0
SET Hi to array length - 1
WHILE Lo <= Hi
SET Mid to (Lo + Hi) / 2
IF X < array[Mid] THEN
SET Hi to Mid - 1
ELSE IF X > array[Mid] THEN
SET Lo to Mid + 1
ELSE
RETURN Mid
ENDIF
ENDWHILE
RETURN -1
Binary Search is one of the the most efficient algorithms that we have. It is efficient because in each iteration
we are halving the search space.