Unit V Sorting
Unit V Sorting
Introduction
Definition
Sorting refers to the operation or technique of arranging and rearranging sets of data in some
specific order.
Sorting is the operation performed to arrange the records of a table or list in some order according
to some specific ordering criterion. Sorting is performed according to some key value of each record.
Example
The records are either sorted either numerically or alphanumerically. The records are then arranged
in ascending or descending order depending on the numerical value of the key. Here is an example, where
the sorting of a lists of marks obtained by a student in any particular subject of a class.
Categories of Sorting
The techniques of sorting can be divided into two categories. These are:
Internal Sorting
External Sorting
Internal Sorting: If all the data that is to be sorted can be adjusted at a time in the main memory, the
internal sorting method is being performed.
External Sorting: When the data that is to be sorted cannot be accommodated in the memory at the same
time and some has to be kept in auxiliary memory such as hard disk, floppy disk, magnetic tapes etc, then
external sorting methods are performed.
Types of Sorting
Insertion Sort
Selection Sort
Bubble Sort
Radix Sort
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.
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
2
Ο(n ), where n is the number of items.
Bubble Sort
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.
This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n 2)
where n is the number of items.
Radix Sort
Radix sort or bucket sort is a method that can be used to sort a list of a number by its base. If we
want to sort the list of English words, where radix or base is 26 then 26 buckets are used to sort the words.
To sort an array of decimal number where the radix or base is 10 we need 10 buckets and can be
numbered as 0,1,2,3,4,5,6,7,8,9. A number of passes required to have a sorted array depend upon the
number of digits in the largest element.
Example:
English
https://www.youtube.com/watch?v=dLEsrqgbJ2s
Tamil
https://www.youtube.com/watch?v=1YvFImvXtiA
https://www.youtube.com/watch?v=pyJwnhRuZ54