Category Archives: Sorting Techniques

Selection Sort (Algo) :

Selection sort is a sorting algorithm that has quadratic running time complexity given as O(n^2) thereby making it inefficient to be used on large lists.Selection sort is generally the preferred choice for sorting files with very large objects and small keys.

The algorithm for selection is shown in figure.The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.*

SMALLEST(ARR, K, N, POS)
Step I :     [Initialize] SET SMALL = ARR[K]
Step II :    [Initialize] SET POS = K
Step III :  Repeat for J=k+1 to N
 IF SMALL > ARR[J] , Then
 SET SMALL = ARR[J]
SET POS = J
[End of if ]
[ End of Loop ]
Step IV : Exit.

SELECTION SORT() :
Step I :     Repeat Steps 2 and 3 for K = 1 to N-1
Step II :   Call SMALLEST(ARR, K, N, POS)
Step III:   SWAP ARR[K] with ARR[POS]
[ End of Loop]
Step IV :   Exit.

Figure :-
SS

Insertion Sort (Algo) :

Insertion sort is a very simple sorting algorithm,in which the sorted array is built one element at a time.Insertion sort is very less efficient when compared with other more advanced algorithms such as quick sort, heap sort, and merge sort.

INSERTION_SORT (A, N) :
Here A is an unsorted array having N elements.

Step I :         Repeat Steps 2 to 5 for K = 1 to N
Step II :                   Set Temp = A [ k ]
Step III :                  Set J = k – 1
Step IV :                   Repeat while Temp <= A [ j ]
                                               Set A [ j + 1 ] = A [ j ]
                                               Set J = j – 1
 [ End of Inner loop ]
Step V :                   Set A [ j + 1 ]  = Temp
 [ End of for  Loop ]
Step VI :      Exit.

Sorting Steps :
IS
Advantages :-
1. Easy to implement
2. Efficient to use on small sets of data.
3. Requires less memory space.
4. It is twice as fast as the bubble sort, almost 40 % faster than the selection sort.

Merge Sort ( Algo ) :

Merge sort is a sorting algorithm that uses the Divide, Conquer, and Combine algorithm paradigm. It uses a function merge which combines the sub-arrays to form a sorted array. While the merge sort algorithm recursively divides the list into smaller lists, the merge algorithm conquers the list to sort the elements in the individual lists.Finally, the smaller lists are merged to form one list.

MERGE_SORT( A, BEG, END ) :
Here A is an unsorted array. BEG is the lower bound and END is the upper bound.

Step I :       If (BEG < END) Then
Step II :      Set MID = (BEG + END) / 2
Step III :    Call Merge Sort (A, BEG, MID)
Step IV :    Call Merge Sort (A, MID + 1, END)
Step V :      Call Merge (A, BEG, MID, END)
[End of If]
Step VI :     Exit.

MERGE ( A, BEG, MID, END ) :
Here A is an unsorted array. BEG is the lower bound, END is the upper bound and MID is the
middle value of array. B is an empty array.

Step I  :                Repeat For I = BEG to END
Step II :              Set B[I] = A[I]                                  [Assign array A to B]
[End of For Loop]
Step III :            Set I = BEG, J = MID + 1, K = BEG
Step IV :            Repeat While (I <= MID) and (J <= END)
Step V :               If (B[I] <= B[J]) Then                   [Assign smaller value to A]
Step VI :             Set A[K] = B[I]
Step VII :           Set I = I + 1 and K = K + 1
Step VIII :         Else
Step IX :            Set A[K] = B[J]
Step X :              Set J = J + 1 and K = K + 1
                  [End of If]
[End of While Loop]
Step XI :            If (I <= MID) Then                         [Check whether first half
Step XII :          Repeat While (I <= MID)                    has exhausted or not]
Step XIII :        Set A[K] = B[I]
Step XIV :         Set I = I + 1 and K = K + 1
[End of  While Loop]
Step XV :           Else
Step XVI :         Repeat While (J <= END)
Step XVII :       Set A[K] = B[J]
Step XVIII :     Set J = J + 1 and K = K + 1
                   [End of While Loop]
[End of If ]
Step XIX :        Exit.

Explained using figure :-
MS
Merge sort and Quick sort are widely used for online sorting where data may not be present as one big chunk in the beginning but may be available in pieces.

Quick Sort :

Quick sort is also known as ‘Partition exchange sort’ works by using a divide and conquer strategy to divide single unsorted array into two smaller sub arrays.
A function Partition is used, by the quick sort algorithm, to divide the array into two sub arrays.

 Partition ( A, BEG, END )
Here A is an unsorted array. BEG is the lower bound, END is the upper bound.

Step I : Set LOC = BEG
Step II : Repeat While (True)
Step III : Repeat While (A[LOC] <= A[END]) and (LOC != END)
Step IV : END = END – 1
[End of While Loop]
Step V : If (LOC == END) Then
Step VI : Return LOC
[End of If]
Step VII : Interchange A[LOC] and A[END]
Step VIII : Set LOC = END
Step IX : Repeat While (A[LOC] >= A[BEG]) and (LOC != BEG)
Step X : BEG = BEG + 1
[End of While Loop]
Step XI : If (LOC == BEG) Then
Step XII : Return LOC
[End of If]
Step XIII : Interchange A[LOC] and A[BEG]
Step XIV : Set LOC = BEG
[End of While Loop]
Step XV : Exit

Quick Sort ( A, BEG, END ) :
Here A is an unsorted Array. BEG is the lower bound and END is the upper bound.

Step I : If ( BEG < END ) Then
Step II :            X = Partition ( A, BEG, END )
Step III :          Call Quick Sort ( A, BEG, X – 1 )
Step IV :          Call Quick Sort ( A,  X + 1, END )
[End of If ]
Step V : Exit.

Explained using Figure :
QSNow both sub array are sorted in the same manner.

Pros :- Faster than other algorithm like bubble sort, Selection Sort, and insertion sort. It is one of the most favourable algorithm when speed is big concern. Quick sort can be used to sort arrays of small size, medium, or large size.

Cons :-
Complex and massively Recursive.Â