Mirza Abdulla, et al International Journal of Computer and Electronics Research [Volume 5, Issue 3, June 2016] An O(n^(4/3)) Worst Case Time Selection Sort Algorithm Mirza Abdulla Computer Science Department, AMA international University, Bahrain mirza999@hotmail.com Abstractβ Sorting is one of the most important and fundamental problems in Computer Science. Techniques like Insertion sort, selection, or bubble sort were seen as inefficient due to their πΆ(ππ ) worst case time complexity. However, this didn’t prevent researchers from attempting to improve these bounds. Shell sort was an improvement over Insertion sort. We present a modified selection sort technique that runs in πΆ(ππ/π ) in the worst case. Thus we obtain an asymptotic improvement of a factor of πΆ(ππ/π ) over the classical selection sort technique worst case running time. Keywords: Selection sort; time complexity; O notation 1. INTRODUCTION Ever since the construction of modern day computer, the problems of sorting and searching were fundamental to the solution of most problems. This stems from the fact we need to access data as fast possible, and some arrangements or permutations of data can improve the access time substantially. Indeed, sorting amounts to about half the processing and data manipulations performed I commercial applications. Sorting is a permutation of data. More formally, given a set of n elements π1 , π2 , π3 , … . , ππ , we need to permute the set into ππ(1) , ππ(2) , ππ(3) , … . , ππ(π) . Usually, we want to reorder the data into ascending or descending sequence. In this case we haveππ(π) ≤ ππ(π) ,for π < π, when the ordering is to be ascending, and ππ(π) ≥ ππ(π) when the ordering is to be descending. In comparing sorting algorithms we may also seek other criteria to be satisfied. Some of the criteria that are usually considered when comparing sorting algorithms are: ο· ο· ο· ο· ο· Time efficiency of the sorting technique. There are different measures of time. These include best, average and worst case asymptotic performance. Space efficiency of the sorting technique. Number of comparisons made. Number of data movements made Stability of the sort technique. Some of the most well known sorting techniques include bubble, Insertion, and Selection sort. These sort techniques share two common criteria among them: simplicity of coding, make about n passes over data, gross inefficiency in running ©http://ijcer.org e- ISSN: 2278-5795 time, and a good introduction to sorting at introductory programming level courses. The behavior of these techniques on some other properties is presented in Table 1. Table 1:.Properties Sort Properties Selection Insertion Bubble Not stable Stable Stable O(1) O(1) O(1) extra space extra space extra space Θ(n2) O(n2) O(n2) comparisons comparisons comparisons Θ(n) swaps O(n2) swaps O(n2) swaps Not adaptive Adaptive: Adaptive: O(n) time O(n) when when nearly nearly sorted sorted In this paper we seek to convert such grossly inefficient sorting techniques to more efficient ones by identifying areas of inefficiency and attempt to do away with unduly repetitive tasks, with focus on the Selection sort technique. Enhancements to Selection sort technique appear in the literature. For example, [1] consider improving the data movement in the selection sort process. The asymptotic performances remains as bad as it was before, however, in the best case less data movements are made. Along the same lines [2] make some improvements to data movement thus reducing the running time of selection sort to some extent but with no effect on the asymptotic time performance and the expense of extra space. Other work such as that of [3-5] don’t make any asymptotic time improvement in the running time of the sorting algorithm. On the other hand [9] makes a substantial worst case improvement by marrying the ideas of insertion sort and selection sort to get an π(π√π) worst case running time sorting technique. [11] improves that idea even further to make it apply to just selection sort without scarifying the running time of π(π√π)or the use of π(1) extra space. In this paper, we present an improved worst case running time selection algorithm. We prove that it is substantially better and with π(π2/3 ) factor of time improvement than the classical Selection sort technique. Section 2 gives a brief p- ISSN: 2320-9348 Page 36 Mirza Abdulla, et al International Journal of Computer and Electronics Research [Volume 5, Issue 3, June 2016] background of the improvement to the classical Selection sort obtained by [11] and worst case running time analysis. In section 3 we present the concept of Improved Selection Sort (ISS) and the pseudo code as well as the theoretical time analysis of the algorithm. 2. contains the minimum element of the block. However, when any of the other elements in the current block becomes the current element block, it may not have the minimum of the block in that position initially, and therefore, a search over that block is needed. WORST CASE SELECTION SORT ALGORITHM πΆ(π. √π)Worst case time Selection Sort algorithm. In selection sort we have two partitions: one sorted and the other unsorted. Initially the sorted list is empty, and all the elements are in the unsorted lists. For an unsorted list that initially has n items, n-1 iterations are made on the list where each time the minimum is found and swapped with the first element in the list. Thus increasing the sorted list by and similarly decreasing the unsorted list by one. Unfortunately, and despite its simplicity, this technique requires an overall of π(π2 ) operations in the best-average-worst case. However, [11] showed that the use of blocks can significantly improve the time for the selection of the minimum element of the unsorted list during each iteration of Selection sort algorithm. The idea is to treat the array of π elements to be composed of 2 2 √π consecutive blocks each of size √π elements, which were referred to as level 0 or πΏ0 blocks.The method used to achieve this improvement required that we always keep the minimum of a block in the first location of that block.During each iteration we only search thru the unsorted list but only the items of the block to which the current item belongs to, and the first element of the other blocks in the unsorted list.The minimum item found is thus the minimum of the unsorted list and therefore is swapped with the current item. After the swap is made the block from which the minimum was found may now not contain in its first position the minimum of that block and therefore another search for the minimum of the block is conducted, and the minimum found then is swapped with the first item of that block. Such an action guarantees that all right blocks contain the minimum of their elements in the first position prior to the start of each iteration of the improved selection sort algorithm. π The πΆ(π √π) time algorithm Step0 find the minimum element in each block and interchanges it with the first element in that block.This step is to make sure that prior to step 1 all the blocks in the array would contain the minimum of that block in the first position of the block. Step1 iterate over steps 2 to 6 until the whole array is sorted. Thisis the main loop in the selection sort algorithm. Step 2find the minimum element in the current block. Note that first position of a block ©http://ijcer.org e- ISSN: 2278-5795 Step3 find the minimum element in the entire unsorted list. The loop need only inspect the first element in each block of the unsorted list so far to compare it with the minimum. Step4 find the number of the block where the minimum of the unsorted list was found. This value can be easily obtained by dividing the location by the size of a block. The quotient may be incremented by one if the number of the block is to start from 1. Step 5 we have the location of the minimum element of the entire unsorted list and therefore, we can swap it with the current element. Step 6 find and put the minimum of the block where the minimum of the entire unsorted elements was found (step 4) in the first position of that block. This operations is needed since the minimum of the entire unsorted list was swapped with the current element, the block which contained that element before the swap may not have the minimum of the block in the first position. Therefore, we may need to find the minimum of the block and interchange it with the first element of the block. [11]Proved that each of the steps from 2 to 6 can be 2 performed in π( √π) time. step 1 iterates over the entire list of 2 size n, and hence steps 1 to 6 can be performed in π(π √π) time. step 0 can be performed in π(π) time. Therefore, the 2 entire algorithm can be performed in π(π √π) time in the worst case. 3. IMPROVED SELECTION SORT The introduction of the idea of πΏ0 blocks in [11] led tothe 2 better bound of π(π √π)in selection sort. Such an idea may not be improved by increasing the number of blocks or the size of a block. However, the technique can be enhanced further by introducing another level of blocks: the πΏ1 blocks. Definition: given an array, A[1..n], holding n elements, 1. We assume that the sorted list is built starting from lower indexed items going towards the higher indexed items. We will refer to the higher indexed elements as p- ISSN: 2320-9348 Page 37 Mirza Abdulla, et al International Journal of Computer and Electronics Research [Volume 5, Issue 3, June 2016] 2. 3. 4. 5. 6. 7. appearing to the right of the lower indexed elements of the array. We shall refer to all consecutive locations in the array of length π = ⌊π1/3 ⌋starting from locationπ − ππ + πas the level 0 (or πΏ0 ) block. Thus we have ππ such blocks. Moreover, all πΏ0 blocks start after the locations 1 to π − ππ in the input array. In other words the blocks are right aligned in the array, when the lower indexed elements start from the left. Locations 1 to π − ππ mayconstitute a πΏ0 block of length less than π, which we shall refer to as partial πΏ0 block (or π0 block). We shall refer to all consecutive locations in the array of length π = π’2 starting from location π − ππ as the level 1 (or πΏ1 ) block. Thus each πΏ1 block contains πofπΏ0 blocks, and there areπ suchπΏ1 blocks. A block (πΏ0 or πΏ1 ) that has the minimum of that respective block in the first position of the block is called block rule compliant. The first element after all the elements of the sorted lists in the selection sort algorithm, or the leftmost element of the unsorted list is referred to as the currentelement and theπΏ0 orπΏ1 block to which it belongs is called the currentπΏ0 or πΏ1 block respectively. All elements in the current πΏ0 or πΏ1 block but not yet in the sorted list are referred to as rightelements to the current element. All πΏ1 blocks to the right of the current element but in the same πΏ0 block are referred to as rightπΏ1 blocks. All πΏ0 blocks to the right of the current block are referred to as rightπΏ0 blocks. The index of the minimum of the unsorted list is referred to as the minIndex, and the block in which the minimum resided is referred to as the min block. Therefore, theπΏ0 orπΏ1 block to which min item belongs is called the minπΏ0 or πΏ1 block respectively. π0 = π1 ππππ1/3 //size of partial πΏ0 block 0. for i = 1+π1 to n step πΏ1 { //make all πΏ1 blocks contain min in 1st position min = i; forj = i to π + πΏ1 − 1 if a[j] < a[min] then j=min; swap a[i] and a[j]; } fori = 1+π0 to n step πΏ0 { //make all πΏ0 blocks contain min in 1st position min = i; forj = i to π + πΏ0 − 1 if a[j] < a[min] then j=min; swap a[i] and a[j]; } 1. for i = 1 to n { 2. ifi< = π0 then π0 = 1 + π0 //location of next πΏ0 block to the right. elseπ0 = 1 + ⌈ 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 17. Improved Algorithm: π1 = πππππ2/3 partial πΏ1 block ©http://ijcer.org πΏ0 = π1/3 πΏ1 = π2/3 //size of e- ISSN: 2278-5795 πΏ0 ⌉ ∗ πΏ0 + π1 ifi< = π1 then π1 = 1 + π1 πΏ1 block to the right. elseπ1 = 1 + ⌈ 15. 16. Figure 1: Level 1 blocks contain level 0 blocks which contain the elements of the array. (π−π1) 18. 19. 20. 21. 22. 23. 24. //location of next (π−π1) πΏ1 ⌉ ∗ πΏ1 + π1 min= i for j=1+ i to π0 − 1 if a[j] <min then min = j; for j=π0 to π1 − 1 step πΏ0 if a[j] <min then min = j; for j=π1 to π step πΏ1 if a[j] <min then min = j; if i ≠ min then swap a[i] and a[min]; else{ if π0 <min<π1 then { // min is in one of πΏ0 blocks in current πΏ1 block min1 = min; for j=minto π0 − 1 if a[j] <min1 then min1 = j; if min1≠ min then swap a[min1] and a[min]; } If min ≥ π1 then // min is in one of πΏ1 to the right of current block min1 = min; for j=minto min +πΏ0 − 1 if a[j] <min1 then min1 = j; for j=min +πΏ0 to min +πΏ1 − 1 step πΏ0 if a[j] <min1 then min1 = j; if min1≠ min then swap a[min1] and a[min]; If min1>min+πΏ0 − 1then { min2 = min1; for j= min2 to min2 +πΏ0 − 1 if a[j] <min2 then min2 = j; swap a[min1] and a[min2]; } p- ISSN: 2320-9348 Page 38 Mirza Abdulla, et al International Journal of Computer and Electronics Research [Volume 5, Issue 3, June 2016] 25. make that πΏ0 block rule compliant. This action is performed in these steps. } Explanation of the pseudo code: ο· ο· ο· Variables: π³π is the size of πΏ0 block, π³π is the size of πΏ1 block, π·π is the size of partial πΏ0 block, and π·π is the size of partial πΏ1 block Step 0: This step is necessary to make the array block rule compliant. The idea is to repeat thru the array in steps equal to the size of πΏ1 find the minimum in each block and swap it with the first location of that block. We repeat again thru the array in steps equal to the size of πΏ0 by finding the minimum in each such block and swap it with the first location of that block. Step 1: We iterate thru the unsorted list, and each time: o Steps 2 and 3: The location of the first πΏ0 block to the right of the current item (π0 ), and the location of the first πΏ1 block to the right of the current item (πΏ1 ), are found. o Steps 4 and 5: The minimum of the current partialπΏ0 block is found. o Step 6: The minimum of the current partial πΏ0 block is found, and becomes the minimum so far o Step 7: The minimum of the right πΏ1 blocks is found and becomes the minimum. o Step 8: if the current item is not the minimum of the unsorted list the minimum found is swapped with the current item. This way the minimum is found and is added to the sorted list. o Step 9: if the location of the minimum was in the current partial πΏ1 block but not in the current partial πΏ0 block, then the πΏ0 block from which it was obtained may not be min block compliant. So ο§ Steps 10 and 11: The minimum of that πΏ0 block is found, and ο§ Step 12: we swap the first item of that πΏ0 block with the minimum of that block. o Step 14: on the other hand if the minimum of the unsorted list was obtained from an πΏ1 block to the right of the current πΏ1 block: ο§ Steps 15and 16: Find the minimum of the first πΏ0 block in that πΏ1 block, and ο§ Step 17: Find the minimum of the remaining πΏ0 blocks in that πΏ1 block. ο§ Steps 18 and 19: the minimum found in step 17 is the minimum of the πΏ1 and is swapped with the first item in that πΏ1 block. ο§ Steps 20 and 23: if the minimum found in step 17 was obtained from a block other than the first πΏ0 block in that πΏ1 block, we would have to ©http://ijcer.org e- ISSN: 2278-5795 Correctness of algorithm Proposition 1. The first element in a πΏ1 block is also the first element in the first πΏ0 block within the πΏ1 block, and it is the minimum of both blocks. Proof. Follows form the definition of the blocks. Proposition 2. The minimum element of the unsorted list prior to any iteration in step 1 of the algorithm is one of the elements of the unsorted part of the current partialπΏ0 block, the right πΏ0 blocks in the current πΏ1 block and the right πΏ1 blocks. Proof. The minimum element of the unsorted list prior to any iteration in step 1of the algorithm is one of the elements of the unsorted part of the current πΏ1 block and the right πΏ1 blocks .However, the first position of the current πΏ1 block may have already been inspected and added to the sorted list. It follows therefore that it should not be considered in the search of the minimum of the unsorted list. Nevertheless, each of the πΏ0 right blocks in the current partialπΏ1 block contains the minimum of the block in its first position. Therefore, these blocks and the not yet sorted elements in the current partialπΏ0 block must have between them the minimum of unsorted elements in the current partialπΏ1 block. This minimum can be compared with the minimum of the right πΏ1 blocks to obtain the overall minimum of the unsorted list. It follows therefore that when a swap is made between the minimum element in the unsorted list and the current item, the current position will contain the minimum of the unsorted list and the swapped item may be in either 1. 2. 3. the current partialπΏ0 block, or the first position in one of the right πΏ0 blocks within the current πΏ1 block or The first position in one of the right πΏ1 blocks. In the first case, no violation to our restriction of having the first element of a block having the minimum of that block, since now the first position of the block already contains the minimum of the block. In the second and third cases, we have a violation of the first position containing the min of a block rule and it needs to be dealt with. Dealing with the second case all that needs to be performed is finding the minimum of the block and swap it with item in the first position, after of which the min rule is maintained for the entire array. Similarly, for the third case we find the minimum of all the elements in the πΏ1 block where the swapped item was moved to, and the min of all the right πΏ1 blocks to that block with their p- ISSN: 2320-9348 Page 39 Mirza Abdulla, et al International Journal of Computer and Electronics Research [Volume 5, Issue 3, June 2016] πΏ0 block. Again the min rule is maintained after this operation, and this is exactly what steps 7 to 16 of the algorithm perform. Therefore, we have: Proposition3. At the end of each iteration, and prior to the start of a new iteration, the current position contains the minimum of the unsorted list and the min rule is maintained. Lemma 1. The algorithm correctly sorts the entire list. Proof. Prior to the start of the main loop of the algorithm a scan is made over the entire array of elements to make sure that the min rule is maintained. The algorithm iterates over all elements of the array and by proposition 5, each time finds the minimum and places it in the current position as well as make sure that the min rule is maintained prior to the start of a new iteration. Time Complexity Analysis of algorithm. In step 0, and prior to the start of the main loop of the algorithm a scan is made on the array to block rule compliant. The scan is performed in πΏ(πΏ) time. Step 1 is repeated n times , and during each pass: Steps 2 to 4 take πΏ(1) or constant time Step 5 is repeated for the remainder of the current πΏ0 and thus performs πΏ(|πΏ0 |) operations at the most. Step 6 is repeated a maximum of πΏ(|πΏ0 |) since there are a maximum of that many πΏ0 blocks in any πΏ1 block. Step 7 is repeated a maximum of πΏ(|πΏ0 |) since the number of πΏ1 blocks in the array is |πΏ0 |. Steps 8 to 10 require πΏ(1) or constant time. Step 11 can be performed in at most πΏ(|πΏ0 |) time since the loop makes that many iterations Steps 12 to 15 require πΏ(1) or constant time. Step 16 can be performed in at most πΏ(|πΏ0 |) time since the loop makes that many iterations Step 17 is repeated a maximum of πΏ(|πΏ0 |) since there are a maximum of that many πΏ0 blocks in any πΏ1 block. Steps 18 to 21 require πΏ(1) or constant time. Steps 22 can be performed in at most πΏ(|πΏ0 |) time since the loop makes that many iterations Step 23 requires πΏ(1) or constant time. It follows, therefore, that the body of the main loop performs πΏ(|πΏ0 |) at the most, from which it follows that the improved selection sort algorithm runs in πΏ(πΏ|πΏ0 |) = πΏ(πΏ. πΏ1/3 ) time in the worst case. 1 Theorem 1. The ISS2 algorithm runs in πΏ(πΏ. πΏ3 ) time. 4. CONCLUSIONS 1 In this paper we presented anπΏ(πΏ. πΏ3 ) worst case running 1 time selection sort variant, which is an improvement of πΏ(πΏ3 ) factor on the classical selection sort technique. This substantial in the asymptotic performance improvement of ©http://ijcer.org e- ISSN: 2278-5795 selection sort was obtained by restricting the search of the minimum item in each pass of the sort to a small subset of the unsorted list. Thus we avoid making unnecessary comparisons to most of the elements in the unsorted list. REFERENCES [1] JehadAlnihoud and Rami Mansi, An Enhancement of Major Sorting Algorithms, The International Arab Journal of Information Technology, Vol. 7, No. 1, January 2010, pp. 55-62. [2] Md. Khairullah, Enhancing Worst Sorting Algorithms, International Journal of Advanced Science and Technology Vol. 56, July, 2013, pp.13-26. [3] Muhammad Farooq Umar, EhsanUllahMunir, Shafqat Ali Shad and Muhammad WasifNisar, Enhancement of Selection, Bubble and Insertion Sorting Algorithm, Research Journal of Applied Sciences, Engineering and Technology 8(2): 263-271, 2014, Published: July 10, 2014, ISSN: 20407459; e-ISSN: 2040-7467. [4] Ankit R. Chadha, RishikeshMisal, TanayaMokashi, AmanChadha, ARC Sort: Enhanced and Time Efficient Sorting Algorithm, International Journal of Applied Information Systems (IJAIS) – ISSN : 2249-0868, Foundation of Computer Science FCS, New York, USA, Volume 7– No. 2, April 2014 – www.ijais.org [5] JyotiDua, Enhanced Bidirectional Selection Sort, International Journal of Computer, Electrical, Automation, Control and Information Engineering Vol:8, No:7, 2014 [6] Md. Khairullah, An Enhanced Selection Sort Algorithm, SUST Journal of Science and Technology, Vol. 21, No. 1, 2014; P:9-15 [7] SurenderLakra , Divya, Improving the performance of selection sort using a modified double-ended selection sorting, International Journal of Application or Innovation in Engineering & Management (IJAIEM), Web Site: www.ijaiem.org Email: editor@ijaiem.org, editorijaiem@gmail.com Volume 2, Issue 5, May 2013 ISSN 2319 – 4847. [8] Mrs.P.Sumathi, Mr.V.V.Karthikeyan, A New Approach for Selection Sorting, International Journal of Advanced Research in Computer Engineering & Technology (IJARCET) Volume 2, Issue 10, October 2013. [9] Mirza Abdulla, Marrying Inefficient SortingTechniques Can Give Birth to aSubstantially More Efficient Algorithm, International Journal of Computer Science and Mobile Applications, Vol.3 Issue. 12, December- 2015, pg. 15-21. [10] Mirza Abdulla, Optimal Time and Space Sorting Algorithm on Hierarchical Memory with Block Transfer, International Journal of Electrical & Computer Sciences IJECS-IJENS Vol:16 No:01, Feb-2016, Pg. 10-13. [11] Mirza Abdulla,Selection Sort with improved asymptotic time bounds, The International Journal of Engineering and Science (THE IJES), To Appear May-2016. [12] Knuth, D., "The Art of Computer programming Sorting and Searching", 2nd edition, vol.3. Addison- Wesley, 1998. p- ISSN: 2320-9348 Page 40 Mirza Abdulla, et al International Journal of Computer and Electronics Research [Volume 5, Issue 3, June 2016] [13] J. L. Bentley and R. Sedgewick, 1997, "Fast Algorithms for Sorting and Searching Strings", ACM-SIAM SODA β97,360-369. [14] T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, 2001,."Introduction to Algorithms".MIT Press, Cambridge,MA, 2nd edition. [15] Flores, I., Jan 1961, "Analysis of Internal Computer Sorting". J.ACM 8, 41-80. ©http://ijcer.org e- ISSN: 2278-5795 [16] V.Estivill-Castro and D.Wood, 1992,"A Survey of Adaptive Sorting Algorithms", Computing Surveys, 24:441-476. [17] Cocktail Sort Algorithm or Shaker Sort Algorithm, http://www.codingunit.com/cocktail-sort-algorithm-orshaker-sortalgorithm. p- ISSN: 2320-9348 Page 41