You are on page 1of 18

Sorting Algorithms

Sorting refers to arranging the list of elements of a list in ascending or descending order. Many
sorting algorithms have been invented each having its own advantage and disadvantages.
Sorting algorithms can be classified in to
Sorting by exchange
Sorting by insertion
Sorting by selection
Sorting by distribution and so on...

Sorting algorithm can be


Internal sort: When a set of data to be sorted is small enough such that the entire sorting can
be performed in a computer’s internal storage(Primary memory)
External sort: Sorting of large set of data ,which is stored in computers external memory
Ascending order: An arrangement of data is called ascending order if it satisfies ‘greater than
or equal to ’ relation between any two consecutive data
Descending order: An arrangement of data is said to be in descending order if it satisfies
‘greater than or equal ’ relation between any two consecutive data

Sorting techniques
Bubble sort
Bubble sort belongs to the family of sorting by exchange. During the sorting process pair of
elements that are out of order are interchanged until the whole list is ordered.Bubble algorithm
is comparison-based algorithm. Bubble sort works as follows
 Each pair of adjacent elements are compared
 The elements are swapped if they are not in order.
 If we have total n elements, then we need to repeat this process for n-1 times.
 It is known as bubble sort, because with every complete iteration the largest element in
the given array, bubbles up towards the last place or the highest index

This algorithm is not suitable for large data sets as its average and worst case complexity are
of Ο(n2) where n is the number of items.

Algorithm for bubble sort


Input:An array A[1,2,…n] where n is the number of elements
Output:An array A with elements in sorted order
Remarks : Sort the elements on ascending order

Steps:
1. For i=1 to n-1 do
2. For j=1 to n-i do
3. If (A[j]>A[j+1]) then
4. Swap (A[j],A[j+1])
5. Endif
6. Endfor
7. Endfor
8. stop

During the execution of bubble sort it compare A[1] and A[2] and arrange them in the desired
order so that A[1]<A[2]. Then compare A[2] and A[3] and arrange them in the order
A[2]<A[3].this will continue until we compare A[N-1] and A[N] and arrange it into A[N-
1]<A[N]
Consider an example

In the above example during first pass the largest element 92 will bubbles to the last position
During the second pass the next largest element 90 will bubble to the position A[N-1] and so on.
Insertion sort
Insertion sort is the most memory efficient sorting techniques. It included in sorting by insertion
category of sorting algorithms. This algorithm is very popular with bridge players when they are
first sorting their cards

Insertion sort algorithm


Input:L[1:n] unsorted list of data elements
Process: key is the key to be inserted and position is the location
Output: sorted data list

INSERTION_SORT[A,N]
Steps :
1. for i=2 to N do
2. key=A[i]
3. position =i
4. While(position > 1 ) and A[position-1>key do
5. A[position]=A[postion-1]
6. Position=postion-1
7. A[position]=key
8. Endwhile
9. Endfor
10. Endsort
The for loop in the algorithm keeps the count of the passes and while loop implements the
comparison of the key key with its sorted sublist of predecessors

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 uses a very simple logic pattern, which involves finding the minimum value of the
items being sorted, and then moving it to the start of the of the list. In the next iteration, we
narrow our range to exclude the first (now sorted) minimum value and proceed to locate the next
minimum. We then place this minimum at the start of our search range, which means it’s after
the first minimum value we found. This process continues till all values have been sorted.
Selection sort algorithm
Input: An input list A[1…N]
Output: List in sorted order

Steps:
1. For i=1 to (n-1) do
2. Min=selectMin(I,n)
3. If(i!=min)
4. Swap(A[i],A[min])
5. Endif
6. Endfor
7. stop

Algorithm selectMin
Steps:
1. min=A[L]
2. Min_index=L
3. For(j=L+1 to N do
4. If(Min>A[j])
5. Min=A[j]
6. Min_Index=j
7. endIf
8. Endfor
9. Return(Min_Index)
10. stop

Algorithm Swap
Input: X and Y are two variable
Output: the value of X goes to Y and vice versa

Steps
1. temp=X
2. X=Y
3. Y=temp

Selection sort for n=8 items

Quick sort
QuickSort is a sorting algorithm belongs to the classification of sorting by exchange.Quik sort
is based on the Divide and Conquer algorithm that picks an element as a pivot element and
partitions the given array around the picked pivot by placing the pivot in its correct position in
the sorted array .Every stage of the sorting process is based on the pivot element.

Divide-and conquer
The principle of divide and conquer strategy is to solve large problem by solving the smaller
instances of the problem.it has three basic steps ,divide, conquer and combine

Divide :
Divide the large problem in to a number of smaller sub problems.

Conquer:
Find a suitable technique so that each sub problem can be solved quickly.
Combine:
Combine the results of all solutions to all sub problems to get the final solutions

Partition(A,LB,UB)
1. Pivot=A[LB]
2. Start=LB
3. End=UB
4. While(start<end) do
5. While(A[start]<=pivot)
6. Start=start+1
7. Endwhile
8. While(A[end]>piovt)
9. End=end-1
10. Endwhile
11. If(start<end)
12. Swap(A[start],A[end])
13. Endif
14. Swap (A[lb],A[end]
15. Endwhile
16. Return end
17. stop

QuickSort(A,LB,UB)
1. If(LB<UB) then
2. Loc=Partition(A,LB,UB)
3. QuickSort(A,LB,loc-1)
4. QuickSort(A,Loc+1,UB)
5. return

Quick sort uses divide and conquer method for sorting the elements
Merge sort
Comparison of sorting algorithms based on Complexity
The following table shows the time complexities of various algorithms .The lower the time
complexity is the better one .Quick sort and merge sort will be a better choice

Sorting Algorithm Average Case Best Case Worst Case

Bubble Sort O(n2) O(n) O(n2)

Insertion Sort O(n2) O(n) O(n2)

Selection Sort O(n2) O(n2) O(n2)

Quick Sort O(n.log(n)) O(n.log(n)) O(n2)

Merge Sort O(n.log(n)) O(n.log(n)) O(n.log(n))


Searching algorithms
Searching Algorithms are designed to check for an element or retrieve an element from any data
structure where it is stored.
Types of searching algorithm

1. Linear or Sequential Search


2. Binary Search
Linear or Sequential Search
This algorithm works by sequentially iterating through the whole array or list from one end until
the target element is found. If the element is found, it returns its index, else it will return key not
found message.
Flow chart for sequential search

The procedures for implementing linear search are as follows:

Step 1: First, read the search element (Target element) in the array.

Step 2: In the second step compare the search element with the first element in the array.

Step 3: If both are matched, display "Target element is found" and terminate the Linear Search
function.

Step 4: If both are not matched, compare the search element with the next element in the array.

Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is compared with the
last element of the array.
Step 6 - If the last element in the list does not match, the Linear Search Function will be
terminated, and the message "Element is not found" will be displayed.

Binary search
Binary search is a widely used algorithm in computer science and data structures for efficiently
searching for a specific element in a sorted collection of data. It works by repeatedly dividing the
search space in half and comparing the target element with the middle element of the current
search space.
Binary search uses divide and Conquer mechanism to find out an element
Here's how binary search works:
1. Initialize: Binary search starts with the entire sorted collection and two pointers, one
pointing to the beginning and the other to the end of the collection.
2. Find the middle: Calculate the middle index of the current search space by taking the
average of the two pointers:
Mid=(left+right)/2

Compare: Compare the element at the middle index with the target element.
 If the element at the middle index is equal to the target, the search is successful,
and the index of the target element is returned.
 If the element at the middle index is less than the target, update the left pointer
to mid + 1, effectively eliminating the left half of the search space.
 If the element at the middle index is greater than the target, update the right
pointer to mid - 1, effectively eliminating the right half of the search space.
2. Repeat: Repeat steps 2 and 3 until either the target element is found, or the left
pointer is greater than the right pointer. If the left pointer becomes greater than the
right pointer, the target element is not in the collection, and the search terminates.
Advantage of binary search
 It is better than a linear search algorithm since its run time complexity is O(logN).
 At each iteration, the binary search algorithm eliminates half of the list and significantly
reduces the search space.
 When it comes to comparing large data, it is quite efficient as it works on the technique to
eliminate half of the array element.
 But the list should be in sorted order

You might also like