A New Sorting Algorithm with filling to the left and right

This paper presents an algorithm for sorting by using of LIT (left inversions table). The algorithm is named LR. The time complexity of the proposed algorithm analytically evaluated. Two approaches for acceleration of LR are presented. The proposed algorithm and its two improvements are implemented in C++. Experimental comparisons are done between LR and some known algorithms, and between LR and its two modifications. The experiments show that LR is faster than “bubble sort” and “LtoRA” algorithms but it is slower than the algorithms “insertion sort” and “selection sort”. The experiments also show that for rows in which there is a large number of the repetitions, the modification “LR – repeat” is faster than the original algorithm, “Bubble sort”, “Selection sort” and the modification “LRA – minimax”. The algorithm “LR minimax” is faster than algorithm LR in all cases (when the row has large or small number of repetitions).


I. INTRODUCTION AND AIMS
HIS paper is a continuation of the work in article [7].The two papers are part of a research on some sorting methods and the possibility for their improvement.It is considered that near 25% of the work of the computer systems is used for sorting of information [1].This shows how important it is to find good sorting methods and algorithms.There are many methods and algorithms for sorting and they are studied widely [1,2,3,4,5,6].This doesn't mean that everything in this area is finished and nothing new and better could be found, especially considering the characteristics of the given row.The aim of this paper is:  to propose and investigate a method for sorting of rows by a left inversions table (LIT) with left and right filling which is an improvement of the sorting methods proposed in [7];  to evaluate the complexity of the proposed method;  to make a program for the proposed method (algorithm) for row sorting by LIT with right and left filling; Naiden Borisov Vasilev was head of department of Computer Systems and Technologies in Technical University Sofia, branch in Plovdiv, Bulgaria.(email: mnvasilev@yahoo.com).
Atanaska Dimitrova Bosakova-Ardenska works in University of Food Technologies, Plovdiv in Bulgaria.She is associated professor in department of Computer Systems and Technologies (e-mail:a_bosakova@uft-plovdiv.bg).
 to evaluate and compare experimentally the proposed method: sorting by LIT with right and left filling.
The methods for row sorting considered in [7] are:  sorting by LIT with filling from left to right;  sorting by LIT with filling from right to left.They are based on the proven assertion: the table of the left inversions by positions of a given row aj (j = 1,2,…,n) uniquely defines the sorted ascending or descending row.LIT of a given row is the sequence of numbers in the j-th position in which the number of the elements dj is written, left from aj (j-th element, j = 1,2,...,n) and lager than it.The steps for sorting by LIT are the following: 1) constructing the LIT of the given row by counting the larger elements from the left of every element in the row; 2) constructing the searching row.Constructing the searching row begins with the element in the first position of the given row, continues with the second element and so on, until the element in the n-th position.The position for recording of the elements and the number of moved elements depending on the desired sorting, and the direction of moving are shown in Table 1.We construct the LIT of the row (Table 2). the time for constructing the descending row by sorting with right filling and the ascending row by sorting with left filling, without record operations in temporary positions of the array and intermediate saving of the elements will be: As seen above, the complexity of the proposed algorithms is quadratic.We will look for ways to reduce the number of comparisons and moves.

II. REDUCING THE NUMBER OF COMPARISONS
We will consider two ways for reducing the number of comparisons proposed in [7].These methods decrease the value of the first addend in the equations above for some rows with certain properties.In other words, the efficiency of the proposed methods depends on the characteristics of the given row and sometimes they do not reduce the number of comparisons.Approach 1. Comparisons with the current minimal and current maximal elements When comparing the elements for constructing the LIT, two fields are used.In these fields the minimal (amin) and the maximal (amax) values are written among the elements with which the current element is compared.When we count the left elements larger than aj, first we compare it with the minimal and the maximal values.If aj < amin= ai, i=1,2,...,j-1 the value dj will be j-1 because all elements to the left of aj are larger than it.The comparisons of aj with the left elements are not done.
If aj amax= ai, i=1,2,...,j-1 the value dj will be 0 because to the left of aj there are not elements larger than it.The comparisons of aj with the left elements are not done.This approach is effective for rows in which the small and the large elements are in the end of the rows.It is most effective (0 comparisons) for the rows, which can be split from left to right into two rowsan ascending one and a descending one, for which the smallest element of the ascending row is larger than the largest element of the descending row.The ascending and the descending rows are also such rows.The number of these rows is 2 n-1 .(The values in LIT for such rows are: 0 in the first position and 0 or j-1 in the other positions.)If the minimal and the maximal elements are in the first two position of the given row, the number of the comparisons will not be reduced.This approach adds at most 2n comparisons.First, the element aj is compared to element aj-1.If aj>aj-1 then aj will be compared to amax only.If aj<aj-1 then aj will be compared to amin only.If aj ≥ amin or aj < amax there will be only one more comparison.
Otherwise the added comparisons will be 2.If aj=aj-1 then comparisons to amin and amax are not done.For avoiding repeated comparisons, the approach 2 is used.Approach 2. Avoid repeated comparisons If the row has repeated elements, it is desirable to avoid repeated comparisons.
If ai = aj, i<j, i=1,2,...,n-1, j=2,3,...,n, then the value di+dt is assigned to dj, where dt is the current value of dj.Thus, one comparison and one summation are added, but (i-1) repeated comparisons are avoided for determination of dj.This approach adds n(n-1)/2 comparisons.It is efficient when the number of the repeated elements is large, the same elements are near and they are to the right in the given row.

III. REDUCING THE NUMBER OF THE MOVES
To reduce the number of the moves we will unite both methods (algorithms): filling from left to right and filling from right to left [7].In this case, the record of each element aj (j = 1,2, .., n) will be done to the left or right of the row depending on the result of the comparison of the values dj and j-dj-1.
If dj ≤ j-dj-1 the moves for ascending row will be done to the right and for descending row will be done to the left.
If dj > j-dj-1 the moves for ascending row will be done to the left and for descending` row will be done to the right.Thus, the number of the moves for sorting the row will be minimal.The size of the array for saving the sorted row must be (2n-1).The first element will be written in  We will note that:  if dj = 0, aj is written to the right after the last element in the row;  if dj = j-1, aj is written to the left before the first element in the row.
In both cases, moves of elements are not executed.The number of the moves for constructing the ascending and the descending rows is the same and its value is 4. The results are summarized in Table 3. -minimal-0 (the row is ascending); -average-n(n-1)/4; -maximal-n(n-1)/2 (the row is descending).The number of compares to determine the direction of minimal move (left or right) is equal to n-1.

The number of records (moves one position to left or right) is:
The minimal number of the moves is 0. For rows which has dj= 0 or j-1, j = 1,2,..,n, operations move aren't done.Every such row can be divided (from left to right) in two rows: one increasing row and one decreasing row like the smallest element in the increasing row is bigger than the biggest element in the decreasing row.Ascending and descending rows are such rows.When sort increasing row the fill up are to the right only.When sort decreasing row the fill up are to the left only.The number of these rows is 2 n-1 .For n=4, the number of these rows is 2 4-1 =8.These rows have LIT: 0000, 0100, 0020, 0003, 0120, 0103, 0023 and 0123.(See table 4.) The maximal number of the moves is: (n 2 -2n)/4 when n is even and (n 2 -2n+1)/4 when n is odd.This value is obtained when the LIT are: 0, 0 or 1, 1, 1 or 2, 2, 2 or 3,..., n/2-1 , n/2-1 or n/2 if n is even and 0, 0 or 1, 1, 1 or 2, 2, 2 or 3,..., (n-1)/2-1 or (n-1)/2, (n-1)/2 if n is odd.The number of these rows is 2 n/2 if n is even and 2 (n-1)/2 if n is odd.When n = 4, the number of these rows is 4. The LIT of these rows are: 0011, 0012, 0111 and 0112.The maximal number of the moves is 2. (See table 4.) The number of records (moves) of the elements in temporary positions in the array for constructing of the current sorted row is always n (a new array).The number of records (increments) of the nearest left (L) vacant position and nearest right (R) vacant position in the array is n.We note that the number of the operations (including the moves) for constructing the ascending and the descending row is the same.The time for constructing the sorted row with filling to the left and right will be: t11 is the time for comparing two elements of the array; t21 is the time for incrementing and recording; t12 is the time for comparing with the first operand being an number, and the second one being a value of the operation subtraction; t22 is the time for recording (moving to the left or right) of a element of the array; t23 is the time for additions/subtractions and recording of the result.The minimal and maximal time for constructing the sorted row by LIT with filling to the left and right will be: The first expression for Tlr_max is for n even and the second expression is for n odd.n 2 /4 is maximal number of recordings (increments) for the rows with maximal number of moves for n even.(n 2 -1)/4 is maximal number of recordings (increments) for the rows with maximal number of moves for n odd.The time Tlr_min is for the ascending row.The time Tlr_max is for the rows with LIT: 0,1,1,2,2,3,......,n/2-1,n/2-1, n/2 when n is even and 0, 1, 1, 2, 2, 3,......,(n-1)/2-1, (n-1)/2-1, (n-1)/2, (n-1)/2 when n is odd or for the descending row.
The number of operations in the second operand of the addend "max" is equal to the number of operations in the first operand: n(n-1)/2.So, the value of max will be determined by the ratio of the values of t21 and t22.Table 4 shows the relationship between operations "move" in the discussed methods for sorting for n = 4.The first column contains all rows (permutations) of the elements of the set {1,2,3,4}.
In the second column the tables of the left inversions (LIT) for every row are constructed.
In each cell of the third column the moves to the right are sequentially written and summed for the first, second, third and fourth elements of the row (in the cell of the first column) for constructing the ascending row with filling to the right.It can be seen that the numbers (digits) in the second and third column are the same.
In each cell of the fourth column the moves to the left are sequentially written and summed for the first, second, third and fourth elements of the row (in the cell of the first column) for constructing the ascending row with filling to the left.It is seen that the sum of the corresponding digits in the third and fourth columns is equal to the position number of digits minus one.In each cell of the fifth column the moves to the left and right are sequentially written and summed for the first, second, third and fourth elements of the row (in the cell of the first column) for constructing the ascending row with filling to the left and right.It is seen that the value of each digit is equal to the value of the smaller of the corresponding digits in the third and fourth columns.The smaller digit determines the direction of the filling.
The maximal number of moves for sorting with filling to the left and right is more than two times smaller than the sorting with filling from left to right.For example, when n=1000, the maximal number of the moves for constructing the ascending row with filling from left to right is 499500, and the average is 249750.When the filling is to the left and right the maximal number of the moves is 249500.This can be seen in table 4. When n = 4, these values are respectively 6, 3 and 2.
In comparison with sorting by LIT with filling from left to right [7] sorting by LIT with filling to the left and right use twice more memory and has additional operations: n-1 comparisons to determine the minimal number of moves; n recordings of the nearest left (L) or nearest right (R) vacant positions.

V. EXPERIMENTAL RESULTS
The proposed methods for sorting which use LIT are:  filling to the left and right;  filling to the left and right and comparisons with the current minimal and current maximal elements;  filling to the left and right with avoiding repeated comparisons.These methods are implemented in C++.We will name them LR, LR minimax, and LR repeat, respectively.The aims of the experimental work is the following: 1.To compare the execution times of the proposed algorithms with some known algorithms.We use the algorithms: Bubble, Insertion sort, Selection sort and LtoRA [7]. 2. To verify the influence of the proposed improvements (LR minimax, LR repeat) on the execution time of the investigated algorithm (LR).For experiments a computer system is used with processor Intel Celeron E3300 2,5GHz, RAM 2,96 GB.The elements of the rows are generated by the functions rand() and srand().The number of the elements of the rows for these experiments is from 2000 to 8000.The elements are integer numbers.Each algorithm sorts 10 times 4 different rows with number of the elements 2000, 3000, 4000, 5000, 6000, 7000 and 8000.The execution time is obtained after averaging the corresponding execution times.Experiment 1. Sorting by the algorithms: LtoRA, LR, LR minimax, LR repeat, Bubble, Insertion sort and Selection sort integer numbers from 0 to 32767 (a low repetition rate for the elements).The average times (ms) are shown in Table 5 and Fig. 3.In parentheses is shown the number of operations for sorting the row with algorithms LR, LR minimax and LR repeat.The number of operations is counted with a program.Working time of the program cannot be considered as a reliable estimation because computers work in a multiprogramming mode and there is no guarantee that the tested program is not interrupted, which could increase its execution time.Also, the execution time is influenced by the memory organization.Аs a result, the time for sorting of the same rows can be different.This is why, experimenting with the algorithms LR, LR repeat, LR minimax, first the number of operations for sorting the rows is counted and the obtained values are used as criteria for correct time results.Fig. 5 shows result of counting the operations for sorting the row with 4000 elements with algorithm LR repeat (low repetition rate for the elements).

Fig. 1 .
Fig.1.Ascending (a) and descending (b) rows of the given row sorted from left to right Figure 1a shows the ascending row and figure 1bthe descending row with filling from left to right.The moved elements are underlined.The number of the moves for constructing the ascending row is 17.The number of the moves for constructing the descending row is 19.If t11 is the time for execution of the comparison operation on the computer, t21the time for execution of the increment and record operations, t22the time for execution of the record operation (move to the right), then:  the time for constructing the ascending row by sorting with right filling and the descending row by sorting with left filling, without record operations in temporary positions of the array and intermediate saving of the elements will be: Figures 2a and 2b present the construction of the ascending and the descending rows sorted by LIT with filling to the left and right.The moved elements are underlined.

Fig. 2 .
Fig.2.Ascending (а) and descending (б) orders of the given row sorted by LIT with filling to the left and right

TABLE 3 .
POSITIONS FOR RECORDING AND DIRECTION OF FILLING BY SORTING TO THE LEFT AND RIGHT Row Direction of filling Position for record of aj dj ≤ j-dj-1 dj > j-dj-1 dj ≤ j-dj-1 dj > j-OF THE COMPLEXITY OF THE ALGORITHM FOR SORTING BY LIT WITH FILLING TO THE LEFT AND RIGHT The operations which are used in the proposed algorithm are: 1) compare for constructing of the LIT (for counting of the larger elements); the elements of the array are compared; 2) record (increment) for counting of the larger elements; 3) compare to determine the direction of the minimal move (left or right); the first operand is number and the second one is an expression which contains subtraction (see table 3); 4) record (move one position to the left or to the right) of the elements of the array to construct the current sorted row; 5) record (move) of the elements in temporary positions in the array for constructing the current sorted row; the record position is a result of addition/subtraction (see table 3); 6) record (increment) of nearest vacant left (L) and right (R) positions in the array for sorted row.Number of the compares for count the bigger elements (left inversions) by position is: C = n(n-1)/2.Number of the records (increments) by counting the bigger elements is:His values[7] are: ( − )/ +   ( − ) +    +    =   _ =  + (  ( − )/,      ⁄ +   (  − )  ⁄ )  _ =  + (  ( − )/,   (  − )  ⁄ +   (  −  + ) ) ⁄

Fig. 3
Fig.3 Average times for sorting the rows with low repetition rate for the elementsThe experiments show that: 1) algorithm LR is faster than algorithms LtoRA and Bubble sort but it is slower than algorithms Insertion sort and Selection sort; 2) algorithm "LR minimax" reduced the number of operations (the time) compared with the operations (the time) of the algorithm LR; 3) algorithm "LR repeat" increases the number of operations (the time) compared with the operations (the time) of algorithm LR; the number of added operations "compare" is equal to the not inversed and equal pairs of elements in the row; if there are no equal elements, no comparisons are avoided.

Fig. 4
Fig.4 Average times for sorting the rows with a high repetition rate for the elements

TABLE 4 .
MOVES IN THE THREE METHODS FOR SORTING BY LIT FOR N=4.

TABLE 6 .
AVERAGE TIMES (MS) FOR SORTING ROWS WITH HIGH REPETITION OF THE ELEMENTS