0% found this document useful (0 votes)
6 views4 pages

Part 8

The document discusses various sorting algorithms including Selection Sort, Exchange Sort, Insertion Sort, and QuickSort, detailing their processes and algorithms. It also covers Binary Search, emphasizing its efficiency in finding elements in a sorted array. Each sorting method has its own approach and efficiency, with Binary Search being highlighted as particularly effective due to its halving of the search space with each iteration.

Uploaded by

Sampurna prusty
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)
6 views4 pages

Part 8

The document discusses various sorting algorithms including Selection Sort, Exchange Sort, Insertion Sort, and QuickSort, detailing their processes and algorithms. It also covers Binary Search, emphasizing its efficiency in finding elements in a sorted array. Each sorting method has its own approach and efficiency, with Binary Search being highlighted as particularly effective due to its halving of the search space with each iteration.

Uploaded by

Sampurna prusty
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/ 4

SORTING AND SEARCHING

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.

FOR i = 0 to array length - 2


SET Min to array[i]
SET MinIndex to i
FOR j = i + 1 to array length - 1
IF array[j] < Min THEN
Min = array[j]
MinIndex = j
ENDIF
ENDFOR
SET array[MinIndex] to array[i]
SET array[i] to Min
ENDFOR

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 1: REPEAT FOR i ← 1 TO n DO STEPS BY 1


j←1
REPEAT WHILE j ≤ n-i
IF K[j] > K[j+1] THEN
Temp ← k[j]
K[j] ← K[j+1]
K[j+1] ← Temp
ENDIF
j ← j+1
ENDREPEAT
ENDREPEAT

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.

1. Always pick first element as pivot.


2. Always pick last element as pivot (implemented below)
3. Pick a random element as pivot.
4. Pick median as pivot.
The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as
pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and
put all greater elements (greater than x) after x. All this should be done in linear time.

algorithm quicksort(A, lo, hi) is


if lo < hi then
p := partition(A, lo, hi)
quicksort(A, lo, p - 1)
quicksort(A, p + 1, hi)

algorithm partition(A, lo, hi) is


pivot := A[hi]
i := lo
for j := lo to hi - 1 do
if A[j] < pivot then
swap A[i] with A[j]
i := i + 1
swap A[i] with A[hi]
return i
Binary search:
For binary search to work, let us assume that the array is already sorted in ascending order. We will
return the position of the element X if we find it, otherwise we will return -1.

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.

You might also like