Sorting Algorithms

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 84

Sorting

Sorting is ordering a list of objects.


If the number of objects is small enough to fits into the main memory,
sorting is called internal sorting.
If the number of objects is so large that some of them reside on
external storage during the sort, it is called external sorting.
Below are the internal sorting algorithms
Radix sort
Bucket sort
Bubble sort
Insertion sort
Selection sort
Heapsort
Mergesort
Radix sort _ O(n) algorithms
Radix Sort is a linear sorting algorithm.
It is not an in-place sorting algorithm as it requires extra additional
space.
Radix Sort is stable sort as relative order of elements with equal
values is maintained.
Radix sort can be slower than other sorting algorithms like merge sort
and quick sort, if the operations are not efficient enough. These
operations include inset and delete functions of the sub-list and the
process of isolating the digits we want.
Radix sort is less flexible than other sorts as it depends on the digits or
letter. Radix sort needs to be rewritten if the type of data is changed.
Bucket Sort/Bin Sort

Bucket sort is mainly useful when input is uniformly distributed


over a range.
Example: Sort a large set of floating point numbers which are in range
from 0.0 to 1.0 and are uniformly distributed across the range. How do
we sort the numbers efficiently?
A simple way is to apply a comparison based sorting algorithm.
The lower bound for Comparison based sorting algorithm (Merge Sort,
Heap Sort, Quick-Sort .. etc) is (n Log n), i.e., they cannot do better
than nLogn.
Can we sort the array in linear time? Counting sort can not be applied here
as we use keys as index in counting sort.
Here keys are floating point numbers.
The idea is to use bucket sort.
Bucket algorithm

bucketSort(arr[], n)
1) Create n empty buckets (Or lists).
2) Do following for every array element arr[i].
.......a) Insert arr[i] into bucket[n*array[i]]
3) Sort individual buckets using insertion sort.
4) Concatenate all sorted buckets.
Bucket sort vs counting sort vs radix sort
There are two main variants of bucket sort; one where there is a bucket
for each value, and where buckets hold several values.
Bucket sort is often seen as a generalisation of counting sort because
bucket sort with a bucket size of 1 is essentially counting sort, just
with a more complex implementation.
Another similar sort, radix sort, also distributes items into buckets.
However, they are distributed based on the individual digits within the
values themselves.
Counting sort: buckets hold only a single value
Bucket sort: buckets hold a range of values
Radix sort: buckets hold values based on digits within their values
Bubble sort _O(n2) algorithms

Bubble sort is a simple sorting algorithm.


This sorting algorithm is comparison-based algorithm in which each
pair of adjacent elements is compared and the elements are swapped if
they are not in order.
The algorithm repeats this process until it makes a pass all the way
through the list without swapping any items.
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.
Example 2:
Insertion Sort
This is an in-place comparison-based sorting algorithm.
Here, a sub-list is maintained which is always sorted.
For example, the lower part of an array is maintained to be sorted.
An element which is to be 'insert'ed in this sorted sub-list, has to find
its appropriate place and then it has to be inserted there. Hence the
name, insertion sort.
The array is searched sequentially and unsorted items are moved and
inserted into the sorted sub-list (in the same array).
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

// Sort an arr[] of size n


insertionSort(arr, n)
Loop from i = 1 to n-1.
a) Pick element arr[i] and insert it into sorted sequence arr[0i-1]
Selection sort
Selection sort is a simple sorting algorithm.
This sorting algorithm is an in-place comparison-based algorithm in
which the list is divided into two parts, the sorted part at the left end
and the unsorted part at the right end. Initially, the sorted part is empty
and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped
with the leftmost element, and that element becomes a part of the
sorted array. This process continues moving unsorted array boundary
by one element to the right.
This algorithm is not suitable for large data sets as its average and
worst case complexities are of (n2), where n is the number of items.
Heap Sort


..
Merge sort
Merge sort is a sorting technique based on divide and conquer
technique.
With worst-case time complexity being (n log n), it is one of the
most respected algorithms.
Merge sort first divides the array into equal halves and then combines
them in a sorted manner( Ascending or Descending).
Example
External Sorting
Pick First element as pivot.

Pick Last element as pivot.

Pick Random element as pivot.

Pick median as pivot.

You might also like