Lecture Sorting
Lecture Sorting
Lecture Sorting
Sorting
Sorting is a process of ordering or placing a list of elements from a collection in some kind of
order. It is nothing but storage of data in sorted order. Sorting can be done in ascending and
descending order. It arranges the data in a sequence which makes searching easier.
For example: The below list of characters is sorted in increasing order of their ASCII values. That
is, the character with lesser ASCII value will be placed first than the character with higher ASCII
value.
datastructure aacderrstttuu
Input Output
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.
If a sorting algorithm, after sorting the contents, does not change the sequence of similar content
in which they appear, it is called stable sorting.
If a sorting algorithm, after sorting the contents, changes the sequence of similar content in which
they appear, it is called unstable sorting.
A sorting algorithm is said to be adaptive, if it takes advantage of already 'sorted' elements in the
list that is to be sorted. That is, while sorting if the source list has some element already sorted,
adaptive algorithms will take this into account and will try not to re-order them.
A non-adaptive algorithm is one which does not take into account the elements which are already
sorted. They try to force every single element to be re-ordered to confirm their sortedness.
Important Terms
Some terms are generally coined while discussing sorting techniques, here is a brief introduction
to them −
Increasing Order
A sequence of values is said to be in increasing order, if the successive element is greater than
the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next element is
greater than the previous element.
Decreasing Order
A sequence of values is said to be in decreasing order, if the successive element is less than the
current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next element is less than
the previous element.
Non-Increasing Order
A sequence of values is said to be in non-increasing order, if the successive element is less than
or equal to its previous element in the sequence. This order occurs when the sequence contains
duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasing order, as every next element
is less than or equal to (in case of 3) but not greater than any previous element.
Non-Decreasing Order
A sequence of values is said to be in non-decreasing order, if the successive element is greater
than or equal to its previous element in the sequence. This order occurs when the sequence
contains duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasing order, as every next
element is greater than or equal to (in case of 3) but not less than the previous one.
Pass Comparisons
1 n−1
2 n−2
3 n−3
… …
n−1 1
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort.
Algorithm for selection sort
1. Input array A[1…..n]
2. for(i=1; i<=n-1; i++)
{
small_index=i;
for(j=i+1; j<=n; j++)
{
if(A[j] < A[small_index])
small_index=j;
}
temp=A[i];
A[i]=A[small_index];
A[small_index]=temp;
}
3. Output: Sorted list
The Insertion Sort
It always maintains a sorted sublist in the lower positions of the list. Each new item is then
“inserted” back into the previous sublist such that the sorted sublist is one item larger. Figure
shows the insertion sorting process. The shaded items represent the ordered sublists as the
algorithm makes each pass. We can derive simple steps by which we can achieve insertion sort.
Divide/Break
This step involves breaking the problem into smaller sub-problems. Sub-problems should
represent a part of the original problem. This step generally takes a recursive approach to divide
the problem until no sub-problem is further divisible. At this stage, sub-problems become atomic
in nature but still represent some part of the actual problem.
Conquer/Solve
This step receives a lot of smaller sub-problems to be solved. Generally, at this level, the problems
are considered 'solved' on their own.
Merge/Combine
When the smaller sub-problems are solved, this stage recursively combines them until they
formulate a solution of the original problem. This algorithmic approach works recursively and
conquer & merge steps works so close that they appear as one.