Sorting Techniques: 1. Explain in Detail About Sorting and Different Types of Sorting Techniques
Sorting Techniques: 1. Explain in Detail About Sorting and Different Types of Sorting Techniques
Sorting Techniques: 1. Explain in Detail About Sorting and Different Types of Sorting Techniques
Internal Sorts:- This method uses only the primary memory during sorting process. All data items
are held in main memory and no secondary memory is required this sorting process. If all the data that
is to be sorted can be accommodated at a time in memory is called internal sorting. There is a limitation
for internal sorts; they can only process relatively small lists due to memory constraints. There are 3 types
of internal sorts.
(i) SELECTION SORT :- Ex:- Selection sort algorithm, Heap Sort algorithm
(ii) INSERTION SORT :- Ex:- Insertion sort algorithm, Shell Sort algorithm
(iii) EXCHANGE SORT :- Ex:- Bubble Sort Algorithm, Quick sort algorithm
External Sorts:- Sorting large amount of data requires external or secondary memory. This process
uses external memory such as HDD, to store the data which is not fit into the main memory. So, primary
memory holds the currently being sorted data only. All external sorts are based on process of merging.
Different parts of data are sorted separately and merged together. Ex:- Merge Sort
2. Explain the algorithm for bubble sort and give a suitable example. (OR) Explain the algorithm
for exchange sort with a suitable example.
In bubble sort method the list is divided into two sub-lists sorted and unsorted. The smallest element is
bubbled from unsorted sub-list. After moving the smallest element the imaginary wall moves one element
ahead. The bubble sort was originally written to bubble up the highest element in the list. But there is no
difference whether highest / lowest element is bubbled. This method is easy to understand but time
consuming. In this type, two successive elements are compared and swapping is done. Thus, step-by-step
entire array elements are checked. Given a list of n elements the bubble sort requires up to n-1 passes
to sort the data.
-MALCOLM MONSERRATE
SORTING TECHNIQUES
Example:
3. Explain the algorithm for insertion sort and give a suitable example.
Both the selection and bubble sorts exchange elements. But insertion sort does not exchange elements.
In insertion sort the element is inserted at an appropriate place similar to card insertion. Here the
list is divided into two parts sorted and unsorted sub-lists. In each pass, the first element of unsorted sub
list is picked up and moved into the sorted sub list by inserting it in suitable position. Suppose we have
n elements, we need n-1 passes to sort the elements. Insertion sort works this way:
1. We start with an empty left hand [sorted array] and the cards face down on the table [unsorted
array].
2. Then remove one card [key] at a time from the table [unsorted array], and insert it into the
correct position in the left hand [sorted array].
3. To find the correct position for the card, we compare it with each of the cards already in the
hand, from right to left.
INSERTION_SORT (A)
1. FOR j 2 TO length[A]
2. DO key A[j]
3. {Put A[j] into the sorted sequence A[1 . . j 1]}
4. ij1
-MALCOLM MONSERRATE
SORTING TECHNIQUES
Read the figure row by row. Elements to the left of A[j] that are greater than A[j] move one
position to the right, and A[j] moves into the evacuated position.
4. Explain the algorithm for selection sort and give a suitable example.
In selection sort the list is divided into two sub-lists sorted and unsorted. These two lists are divided by
imaginary wall. We find a smallest element from unsorted sub-list and swap it to the beginning. And the
wall moves one element ahead, as the sorted list is increases and unsorted list is decreases.
Assume that we have a list on n elements. By applying selection sort, the first element is compared with
all remaining (n-1) elements. The smallest element is placed at the first location. Again, the second
-MALCOLM MONSERRATE
SORTING TECHNIQUES
element is compared with remaining (n-1) elements. At the time of comparison, the smaller element is
swapped with larger element. Similarly, entire array is checked for smallest element and then swapping
is done accordingly. Here we need n-1 passes or iterations to completely rearrange the data.
Algorithm: Selection_Sort ( A [ ] , N )
If A[ J ] < A [ POS ]
Set POS = J
End For
End For
Step 6 : Exit
Ex:- A list of unsorted elements are: 23 78 45 8 32 5
5. Explain the algorithm for QUICK sort ( partition exchange sort) and give a suitable example.
Quick sort is based on partition. It is also known as partition exchange sorting. The basic
concept of quick sort process is pick one element from an array and rearranges the remaining
elements around it. This element divides the main list into two sub lists. This chosen element is
called pivot. Once pivot is chosen, then it shifts all the elements less than pivot to left of value
-MALCOLM MONSERRATE
SORTING TECHNIQUES
pivot and all the elements greater than pivot are shifted to the right side. This procedure of
choosing pivot and partition the list is applied recursively until sub-lists consisting of only one
element.
Ex:- A list of unsorted elements are: 8 3 2 11 5 14 0 2 9 4
20
It is also known as partition exchange sort. It was invented by CAR Hoare. It is based on partition.
The basic concept of quick sort process is pick one element from an array and rearranges the
remaining elements around it. This element divides the main list into two sub lists. This chosen
element is called pivot. Once pivot is chosen, then it shifts all the elements less than pivot to left of value
pivot and all the elements greater than pivot are shifted to the right side. This procedure of choosing pivot
and partition the list is applied recursively until sub-lists consisting of only one element.
quicksort(q)
if length(q) 1
return q
-MALCOLM MONSERRATE
SORTING TECHNIQUES
1. It is complex method of sorting so, it is little hard to implement than other sorting methods.
6. Explain the algorithm for Merge sort and give a suitable example.
The basic concept of merge sort is divides the list into two smaller sub-lists of approximately equal
size. Recursively repeat this procedure till only one element is left in the sub-list.After this, various sorted
sub-lists are merged to form sorted parent list. This process goes on recursively till the original sorted list
arrived.
Merge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower
order of growth than insertion sort. Since we are dealing with sub-problems, we state each sub-
problem as sorting a sub-array A[p .. r]. Initially, p = 1 and r = n, but these values change as we
recurse through sub-problems.
1. Divide Step
If a given array A has zero or one element, simply return; it is already sorted. Otherwise,
split A[p .. r] into two sub-arrays A[p .. q] and A[q + 1 .. r], each containing about half of
the elements of A[p .. r]. That is, q is the halfway point of A[p .. r].
2. Conquer Step
Conquer by recursively sorting the two sub-arrays A[p .. q] and A[q + 1 .. r].
3. Combine Step
Combine the elements back in A[p .. r] by merging the two sorted sub-arrays A[p .. q]
and A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure
MERGE (A, p, q, r).
-MALCOLM MONSERRATE
SORTING TECHNIQUES
Note that the recursion bottoms out when the sub-array has just one element, so that it is trivially
sorted.
To sort the entire sequence A[1 .. n], make the initial call to the procedure MERGE-SORT (A, 1, n).
MERGE-SORT (A, p, r
1. IF p < r // Check for base case
2. THEN q = FLOOR[(p + r)/2] // Divide step
3. MERGE (A, p, q) // Conquer step.
4. MERGE (A, q + 1, r) // Conquer step.
5. MERGE (A, p, q, r) // Conquer step.
-MALCOLM MONSERRATE