Enhancement of Selection , Bubble and Insertion Sorting Algorithm

In everyday life there is a large amount of data to arrange because sorting removes any ambiguities and make the data analysis and data processing very easy, efficient and provides with cost less effort. In this study a set of improved sorting algorithms are proposed which gives better performance and design idea. In this study five new sorting algorithms (Bi-directional Selection Sort, Bi-directional bubble sort, MIDBiDirectional Selection Sort, MIDBidirectional bubble sort and linear insertion sort are presented. Bi-directional Selection Sort and MIDBiDirectional Selection Sort are the enhancement on basic selection sort while Bidirectional bubble sort and MIDBidirectional bubble sort are the enhancement on basic bubble sort by changing the selection and swapping mechanism of data for sorting. Enhanced sorting algorithms reduced the iteration by half and quarter times respectively. Asymptotically complexities of these algorithms are reduced to O (n/2) and O (n/4) from O (n). Linear insertion sort is the enhancement of insertion sort by changing the design of algorithm (convert two loops to one loop). So asymptotically this algorithm is converted to linear time complexity from quadratic complexity. These sorting algorithms are described using C. The proposed algorithms are analyzed using asymptotic analysis and also using machine-running time and compared with their basic sorting algorithms. In this study we also discuss how the performance and complexity can be improved by optimizing the code and design.


INTRODUCTION
In algorithmic engineering Sorting is very profound area for researchers and algorithmic engineers.Sorting also improves performance of logarithms and make suitable, efficient and convenient for usage data.There are many sorting algorithms are proposed for sorting data which have some pros and cons respectively in terms of its efficiency and algorithmic complexity by Friend (1956).Some sorting algorithms are considered basic or well known for sorting the unsorted data like Selection sort, Bubble sort by Astrachan (2003), Quick sort by Hoare (1962), Insertion sort by Cormen et al. (2004) and Shell (1959).But still many researchers are working for the optimization of these algorithms.
Optimization is much more important factor for writing or designing algorithms.The output produced by the algorithm is also most important factor, which should be reliable and convenient to use, read and understand by Deitel and Deitel (1998) and Friend (1956).The output of sorting algorithm should be in non-decreasing order by using permutation or reordering concepts by Mansi (2010).
The sorting algorithms can be categorized in two types, which are called internal sort and external sort.Internal sort uses the data residing in RAM while in external sort data is very large so that data may be resided on external memory by Xiaokai (1995), Weimin and Weimin (2000) and Min (2010).Results of sorting algorithm are abundant and are not absolute because these are specific to some factors.It can be used as traditional knowledge by Khamitkar et al. (2010).
Analysis of sorting algorithms also depends on the type of the data.Analysis of algorithms by considering all input is very difficult.The time complexity of algorithm can be analyzed by considering just one input with the help of Kolmogorov complexity by Vitányi (2007) and Xusong (1996).
Selection, bubble and insertion are characterizes as comparison sort because these algorithms sort the data by comparing the data element with each other and selects the element location and swap on that location.These all costs O (n 2 ) in worst case scenario which is the basic limitation of comparison sort techniques.Another limitation of these algorithms is that these also has n2 times iteration in worst case but it can be reduced by optimizing these algorithms and in this study the approach followed is same.
In this study five algorithms are presented two algorithms are the extension of selection sort and two algorithms are the extension of bubble sort and one algorithm is the enhancement of insertion sort.These presented algorithms are optimized, efficient asymptotically as well as running time wise.

LITERATURE REVIEW
Selection sort: In the basic selection sort algorithm we sort the data by selecting (maximum or minimum) element in the selected list by decreasing the list size from n to 2 with the decrement of size by one in each iteration.This has the two loops outer and inner loop.Outer loop manages the size of list to be processed for finding maximum or minimum number and the inner loop is for finding the smallest or biggest number from list selected by outer loop.
The asymptotic complexity of basic selection sort in worst case is O (n 2 ) which is due to comparisons of each data element with each other and its number of iterations.So these algorithms can be improved or enhanced by reducing the number of iterations or comparisons.In this study two enhancement of selection sort algorithms have been discussed which have been discussed below.

Bi-directional selection sort algorithm:
Concept: In this enhancement of selection sort algorithm we sort the data by selecting (maximum and minimum) elements in the selected list by decreasing the list size from n to 2 with the decrement of two in the size.This has the two loops outer and inner loop.Outer loop manages the size of list to be processed for finding maximum and minimum number and the inner loop is for finding the smallest and biggest number from list selected by outer loop.
Pseudo code: Pseudo code for bi-selection sort is given below: Function Bi Selection Sort (array, size) 1 var i, j 2 var min, max, temp 3 for i: = 0 to size/2 do 4 min: = max: = i 5 for j: = i+1 to size-i do 6 if array (j) <array (min) 7 min: = j 8 end if 9 if array (j) >array (max) 10 max: = j 11 end if 12 end for j 13 if array (i) <> array (min) 14 temp: = array (i) 15 array (i): = array (min) 16 array (min): = temp 17 if i <> max 18 temp: = array (i) 19 array (i): = array (max) 20 array (max): = temp 21 end for i In this pseudo code we use array subscript form where Array () is the array to be sort, min is the variable for smallest number selected from the selected list from outer loop and temp is a temporary variable used for swapping.

Running time:
The recurrence for Bi-selection sort algorithm is: To terminate the recursion, we should have n-2k = 0 => 2k = n.By solving this recurrence with the help of any algorithm gives its order of growth as: Bi-directional mid selection sort algorithm: Concept: In this enhancement of selection sort algorithm we sort the data by selecting (maximum and minimum) elements by starting searching from middle to both sides in the selected list by decreasing the list size from n to 2 with the decrement of two in the size.This has the two loops outer and inner loop.Outer loop manages the size of list to be processed for finding maximum and minimum number and the inner loop is for finding the smallest and biggest number from list selected by outer loop.In this pseudo code we use array subscript form where Array() is the array to be sort, min is the variable for smallest number selected from the selected list from outer loop and temp is a temporary variable used for swapping.

Running time:
The recurrence for Bi-MIDselection sort algorithm is: To terminate the recursion, we should have n/2 -2k = 0 => 4k = n.By solving this recurrence with the help of any algorithm gives its order of growth as: The bubble sort algorithm: In the basic bubble sort algorithm we sort the data by finding and replacing the (maximum or minimum) elements in the selected list by decreasing the list size from n to 2 with the decrement of size by one.This has the two loops outer and inner loop.Outer loop manages the size of list to be processed for finding and replacing maximum or minimum number and the inner loop is for finding and replacing the smallest or biggest number from list selected by outer loop.
The asymptotic complexity of basic bubble sort in worst case is O (n 2 ) which is due to comparisons of each data element with each other and its number of iterations.So these algorithms can be improved or enhanced by reducing the number of iterations, comparisons or swapping.In this study two enhancement of bubble sort algorithms have been discussed which are given below.
Bi-directional bubble sort algorithm: Concept: In this enhancement of bubble sort algorithm we sort the data by finding and replacing (maximum and minimum) elements in the selected list by decreasing the list size from n to 2 with the decrement of two in the size.This has the two loops outer and inner loop.Outer loop manages the size of list to be processed for finding maximum and minimum number and the inner loop is for finding and replacing the smallest and biggest number from list selected by outer loop.
Pseudo code: Pseudo code for basic bubble sort is given below: Function Bi Bubble Sort (array, size) 1 var i, j 2 var temp 3 for i: = 0 to size/2 do 4 for j: = i+1 to size-i do 5 if array (j) < array (i) 6 temp: = array (j) 7 array (j): = array (i) 8 array (i): = temp 9 end if 10 if array (j) >array (limit-1-i) 11 temp: = array (j) 12 array (j): = array (limit-1-i) 13 array (limit-1-i): = temp 14 end if 15 end for j 16 end for i In this pseudo code we use array subscript form where Array() is the array to be sort, min is the variable for smallest number selected from the selected list from outer loop and temp is a temporary variable used for swapping.

Running time:
The recurrence for Bi-bubble sort algorithm is: To terminate the recursion, we should have n-2k = 0 => 2k = n.By solving this recurrence with the help of any algorithm gives its order of growth as: Bi-directional mid bubble sort algorithm: Concept: In this enhancement of bubble sort algorithm we sort the data by finding and replacing (maximum and minimum) elements by starting searching from middle to both sides in the selected list by decreasing the list size from n to 2 with the decrement of two in the size.This has the two loops outer and inner loop.Outer loop manages the size of list to be processed for finding maximum and minimum number and the inner loop is for finding and replacing the smallest and biggest number from list selected by outer loop.

Pseudo code: Pseudo code for Bidirectional mid bubble sort is given below:
In this pseudo code we use array subscript form where Array() is the array to be sort, min is the variable for smallest number selected from the selected list from outer loop and temp is a temporary variable used for swapping.

Running time:
The recurrence for Bi-MIDBubble sort algorithm is: To terminate the recursion, we should have n/2 -2k = 0 => 4k = n.By solving this recurrence with the help of any algorithm gives its order of growth as: Insertion sort algorithm: Concept: In basic insertion sort algorithm we sort the data by inserting each element one by one in sorted list to its location according to its order starting list size from one to n.This has the two loops outer and inner loop.Outer loop manages the size of sorted list which increases by one after each iteration in which new element has to be inserted and the inner loop is for finding and inserting at the location according to sorting.
The asymptotic complexity of basic insertion sort in worst case is O (n 2 ) which is due to inserting each data element to its specific location and two loops.So this algorithm can be improved or enhanced by reducing the number of iterations, comparisons or loop.In this study one enhancement of insertion sort algorithm have been discussed which are given below.
Linear order insertion sort algorithm: Concept: In this enhancement of insertion sort algorithm we sort the data by inserting an element one by one in sorted list to its location according to its order starting list size from one to n.This has just one loop outer and inner loop.Outer loop manages the size of sorted list in which new element has to be inserted and the inner loop is for finding and inserting at the location according to sorting.

End if 22. End for
In this pseudo code we use array subscript form where Array() is the array to be sort, size is the variable for representing the array size to be sorted.

Running time:
The recurrence for linear insertion sort algorithm is:

for n≥y and n≥k
To terminate the recursion, we should have n -k = 0 => y = n.By solving this recurrence with the help of any algorithm gives its order of growth as:

RESULTS AND DISCUSSION
Analysis: The behavior of algorithms can be analyzed by determining their order of growth in terms of big-O notation for west case scenario.
Basic selection sort has the order of growth O (n 2 ) but the presented two enhancements of selection sort have the order of growth as O (n 2 /2) and O (n 2 /4) which is shown in Fig. 1.
In Fig. 1 it is clearly is showing that the enhanced algorithms have the much better order of growth.This order of growth of basic and enhanced selection sort can be described as Fig. 2.
From Fig. 2 it can be easily noticed that number of iterations (order of growth) has been reduced to half in Fig. 2b and quarter in Fig. 2c.
Basic bubble sort has the order of growth O (n 2 ) but the presented two enhancements of bubble sort have the order of growth as O (n 2 /2) and O (n 2 /4) which can be shown using chart as below.
In Fig. 3 it is clearly is showing that the enhanced algorithms have the much better order of growth.This order of growth of basic and enhanced selection sort can be described as Fig. 4.
From Fig. 4 it can be easily noticed that number of iterations (order of growth) has been reduced to half in Fig. 4b and quarter in Fig. 4c.
Basic insertion sort has the order of growth O (n 2 ) and has two nested loops but the presented enhancement of insertion sort have the order of growth as O (n + k) which can be says as O (n) which can be shown using chart as below in Fig. 5.
In Fig. 5 it is clearly is showing that the enhanced algorithms have the much better order of growth.This order of growth of basic and enhanced selection sort can be described as Fig. 6.
From Fig. 6 it can be easily noticed that number of iterations (order of growth) has been reduced to linear from quadratic in Fig. 6b.

Running time:
All mentioned basic sorting algorithms with their enhancements are implemented using c++ on windows 7. The pc used for this purpose  has the core i3 processor with 2 gb RAM.In this experiment, inputs are used in sorted order in best case while in reverse order in worst case.For both cases, results have been gotten for 10, 20, 30, 40 and 50 k number of data sets, respectively.The results of these algorithms are the machine running times which are given below in the form of two tables and charts as shown in Table 1 and Fig. 7 for each case.

Best time:
As from above table and chart, it shows that in case of sorted datasets (best case) all enhancements of basic sorting algorithms performs better than their basic sorting algorithms for each set of data sets except EMID bubble sort which takes little more time.
Worst case: As shown in Table 2 and Fig. 8 it shows that in case of reversed sorted datasets (worst case) all enhancements of basic sorting algorithms performs better than their basic sorting algorithms for each set of data sets.

CONCLUSION
In this study five new sorting algorithms are presented by enhanced the selection, bubble and insertion sort algorithms.These are named as E selection Sort, E Mid Selection Sort, E Bubble Sort, E Mid Bubble Sort and linear insertion Sort.As from experiments by using integers as inputs it is showed that these enhanced algorithms are much better in terms of order of growth as well as machine running time by using Intel core i3 processor.These are implemented and executed on Intel core i3 processors.In these algorithms the complexity of algorithm, order of growth and machine running time has been improved by improving the algorithm's design and execution.

Fig. 1 :Fig. 7 :
Fig. 1: Comparison of order of growth of basic selection sort and its enhancements Pseudo code for linear insertion sort is given below:

Table 1 :
Machine running time of basic and their proposed sorting algorithms (best case)

Table 2 :
Machine running time of basic and their proposed sorting algorithms (worst case)