2. Sorting is an algorithm that arranges the elements of a list in a
certain order [either ascending or descending].
The efficiency of data handling can be substantially
[significantly] increased if the data are sorted according to some
criteria or order.
Sorting can significantly reduce the complexity of a problem,
and is often used for database algorithms and searches.
Implementation example: Telephone directory, Product
catalogues in ecommerce etc.
3. 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.
4. Complexity of sorting algorithm
The complexity of sorting algorithm calculates the running time
of a function in which 'n' number of items are to be sorted. The
choice for which sorting method is suitable for a problem
depends on several dependency configurations for different
problems. The most noteworthy of these considerations are:
The length of time spent by the programmer in programming a
specific sorting program
Amount of machine time necessary for running the program
The amount of memory necessary for running the program
5. Efficiency of Sorting Algorithm
To get the amount of time required to sort an array of 'n'
elements by a particular method, the normal approach is to
analyze the method to find the number of comparisons (or
exchanges) required by it.
Most of the sorting techniques are data sensitive, and so the
metrics for them depends on the order in which they appear in an
input array.
Various sorting techniques are analyzed in various cases and
named these cases as follows:
◦ Best case
◦ Worst case
◦ Average case
Hence, the result of these cases is often a formula giving the
average time required for a particular sort of size 'n.' Most of the
sort methods have time requirements that range from O(nlog n)
to O(n2).
6. O(1): This denotes the constant time. 0(1) usually
means that an algorithm will have constant time
regardless of the input size.
O(log n): This denotes logarithmic time. O(log n)
means to decrease with each instance for the
operations. Binary search trees are the best examples
of logarithmic time.
O(n): This denotes linear time. O(n) means that the
performance is directly proportional to the input size.
In simple terms, the number of inputs and the time
taken to execute those inputs will be proportional or
the same. Linear search in arrays is the best example
of linear time complexity.
O(n2): This denotes quadratic time. O(n2) means that
the performance is directly proportional to the square
of the input taken. In simple, the time taken for
execution will take square times the input
size. Nested loops are perfect examples of quadratic
time complexity.
7. Insertion Sort
Insertion Sort is a sorting algorithm where the array is sorted by
taking one element at a time.
The principle behind insertion sort is to take one element, iterate
through the sorted array & find its correct position in the sorted
array.
Complexity of the Insertion Sort Algorithm
To sort an unsorted list with 'n' number of elements, we need to
make (1+2+3+......+n-1) = (n (n-1))/2 number of comparisions
in the worst case. If the list is already sorted then it
requires 'n' number of comparisions.
◦ Worst Case : O(n2)
◦ Best Case : Ω(n)
◦ Average Case : Θ(n2)
8. Algorithm for Insertion Sort
Step 1 − If the element is the first one, it is already sorted.
Step 2 – Move to next element
Step 3 − Compare the current element with all elements in the
sorted array
Step 4 – If the element in the sorted array is smaller than the
current element, iterate to the next element. Otherwise, shift all
the greater element in the array by one position towards the right
Step 5 − Insert the value at the correct position
Step 6 − Repeat until the complete list is sorted
9. How to sort data using insertion sort
Let’s understand how insertion sort is working in the above image.
122, 17, 93, 3, 36
for i = 1(2nd element) to
36 (last element)
i = 1. Since 17 is smaller than 122,
move 122 and insert 17 before 122
17, 122, 93, 3, 36
i = 2. Since 93 is smaller than 122,
move 122 and insert 93 before 122
17, 93,122, 3, 36
i = 3. 3 will move to the beginning
and all other elements from 17 to 122
will move one position ahead of their
current position.
3, 17, 93, 122, 36
i = 4. 36 will move to position after 17, a
nd elements from 93 to 122 will move
one position ahead of their current position.
3, 17, 36, 93 ,122
Now that we have understood how Insertion Sort works,
let us quickly look at a C code to implement insertion sort
10. Insertion sort function
void insertionSort(int array[], int n)
{
int i, element, j;
for (i = 1; i < n; i++)
{
element = array[i]; j = i - 1; //17///0
/* Move elements of arr[0..i-1], that are greater than key by one
position */
while (j >= 0 && array[j] > element) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = element;
}
}
11. 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.
Algorithm
Step 1 - Select the first element of the list (i.e., Element at first
position in the list).
Step 2: Compare the selected element with all the other
elements in the list.
Step 3: In every comparision, if any element is found smaller
than the selected element (for Ascending order), then both are
swapped.
Step 4: Repeat the same procedure with element in the next
position in the list till the entire list is sorted.
12. Complexity of the Selection Sort Algorithm
To sort an unsorted list with 'n' number of elements, we need to make ((n-1)+(n-2)+(n-3)+......+1) =
(n (n-1))/2 number of comparisons in the worst case.
If the list is already sorted then it requires 'n' number of comparisons.
Worst Case : O(n2)
Best Case : Ω(n2)
Average Case : Θ(n2)
Selection Sort Logic
//Selection sort logic
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}