An Enhancement of Major Sorting Algorithms
Total Page:16
File Type:pdf, Size:1020Kb
The International Arab Journal of Information Technology, Vol. 7, No. 1, January 2010 55 An Enhancement of Major Sorting Algorithms Jehad Alnihoud and Rami Mansi Department of Computer Science, Al al-Bayt University, Jordan Abstract: One of the fundamental issues in computer science is ordering a list of items. Although there is a huge number of sorting algorithms, sorting problem has attracted a great deal of research; because efficient sorting is important to optimize the use of other algorithms. This paper presents two new sorting algorithms, enhanced selection sort and enhanced bubble Sort algorithms. Enhanced selection sort is an enhancement on selection sort by making it slightly faster and stable sorting algorithm. Enhanced bubble sort is an enhancement on both bubble sort and selection sort algorithms with O(nlgn) complexity instead of O(n 2) for bubble sort and selection sort algorithms. The two new algorithms are analyzed, implemented, tested, and compared and the results were promising . Keywords: Enhanced selection sort, enhanced bubble sort, selection sort, bubble sort, number of swaps, time complexity . Received May 27, 2008; accepted September 1, 2008 1. Introduction the complexity of solving it efficiently. Bubble sort was analyzed as early as 1956 [2]. Information growth rapidly in our world and to search Many researchers considered sorting as a solved for this information, it should be ordered in some problem. Even so, useful new sorting algorithms are sensible order. Many years ago, it was estimated that still being invented, for example, library sort was first more than half the time on many commercial computers published in 2004. Sorting algorithms are prevalent in was spent in sorting. Fortunately this is no longer true, introductory computer science classes, where the since sophisticated methods have been devised for abundance of algorithms for the problem provides a organizing data, methods which do not require that the gentle introduction to a variety of core algorithm data be kept in any special order [9]. concepts [1, 19]. In [1], they classified sorting Many algorithms are very well known for sorting the algorithms by: unordered lists. Most important of them are Heap sort, Bubble sort, Insertion sort and shell sort [17]. As stated • Computational complexity (worst, average and best in [5], sorting has been considered as a fundamental behavior) of element comparisons in terms of list problem in the study of algorithms, that due to many size ( n). For typical sorting algorithms good reasons: behavior is O(n log n) and bad behavior is Ω( n²). Ideal behavior for a sort is O( n). Sort algorithms • The need to sort information is inherent in many which only use an abstract key comparison applications. operation always need Ω( n log n) comparisons in • Algorithms often use sorting as a key subroutine. the worst case. • In algorithm design there are many essential • Number of swaps (for in-place algorithms). techniques represented in the body of sorting • Stability: stable sorting algorithms maintain the algorithms. relative order of records with equal keys (values). • Many engineering issues come to the fore when That is, a sorting algorithm is stable if whenever implementing sorting algorithms. there are two records R and S with the same key Efficient sorting is important to optimize the use of and with R appearing before S in the original list, R other algorithms that require sorted lists to work will appear before S in the sorted list. correctly; it is also often in producing human-readable • Usage of memory and other computer resources. output. Formally, the output should satisfy two major Some sorting algorithms are “in place”, such that conditions: only O(1) or O(log n) memory is needed beyond the items being sorted, while others need to create • The output is in non-decreasing order. auxiliary locations for data to be temporarily stored. • The output is a permutation, or reordering, of the • Recursion: some algorithms are either recursive or input. non recursive, while others may be both (e.g., Since the early beginning of computing, the sorting merge sort). problem has attracted many researchers, perhaps due to • Whether or not they are a comparison sort. A comparison sort examines the data only by 56 The International Arab Journal of Information Technology, Vol. 7, No. 1, January 2010 comparing two elements with a comparison operator. 4 max := array(index) 5 for a:= 0 to size-2 do In this paper, two new sorting algorithms are 6 if array(a) ≥ max then presented. These new algorithms may consider as 7 max := array(a) selection sort as well as bubble sort algorithms. The 8 index := a study shows that the proposed algorithms are more 9 end if efficient, theoretically, analytically, and practically as 10 end for compared to the original sorting algorithms. Section 2 11 if index ≠ size-1 then presents the concept of enhanced Selection Sort (SS) 12 temp := array(size-1) algorithm and its pseudocode. Furthermore, the 13 array(size-1) := max 14 array(index) := temp implementation, analysis, and comparison with 15 end if selection sort are highlighted. Section 3 introduces 16 size := size-1 enhanced bubble sort algorithm and its pseudocode, 17 return Enhanced Selection Sort (array , size) implementation, analysis, and comparison with bubble 18 else sort. Also, a real-world case study for the proposed 19 return array algorithms is presented in section 4. Finally, 20 end if conclusions were presented in section 5. 2.4. Analysis 2. Enhanced Selection Sort For loop, in line 5 iterates n times in the first call then 2.1. Concept n keeps decreasing by one. We may say that: 0 n = 0 T ( n ) = (1) Inserting an array of elements and sorting these n + T ( n − )1 n > 0 elements in the same array (in-place) by finding the maximum element and exchanging it with the last T(n) = n + T(n-1) element, and then decreasing the size of the array by = n + n-1 + T(n-2) one for next call. In fact, the Enhanced Selection Sort = n + n-1 + n-2 + T(n-3) (2) (ESS) algorithm is an enhancement to the SS algorithm = n + n-1 + n-2 + n-3 + T(n-4) in decreasing number of swap operations, making the = … = n + n-1 + n-2 + n-3 + … + algorithm to be data dependent, and in making it stable. (n- k+1) + T(n-k) The differences between ESS and SS algorithms are discussed in section 2.5. n = ∑ i + T ( n − , fkor) n ≥ k i=n−k +1 2.2. Procedures To terminate the recursion, we should have n - k = 0 The procedures of the algorithms can be described as => k = n: follows: n n n+1 • Inserting all elements of the array. ∑ i + T )0( = ∑ i + 0 = n (3) i=1 i=1 2 • Calling the “Enhanced Selection Sort” function with n + 1 T ( n ) = n = O ( n 2 ) passing the array and its size as parameters. So, 2 (4) • Finding the maximum element in the array and swapping it with the last index of the same array. ESS algorithm is easy to analyze compared to other • Decreasing the size of the array by one. sorting algorithms since the loop does not depend on • Calling the “Enhanced Selection Sort” function the data in the array. Selecting the highest element recursively. The size of the array is decremented by requires scanning all n elements (this takes n - 1 one after each call of the “Enhanced Selection Sort” comparisons) and then swapping it into the last function. Operationally, the ( size ) after the first call position. Then, finding the next highest element became ( size-1), and after the second call became requires scanning the remaining n - 2 elements and so (size-2), and so on. on, for ( n-1)+( n-2)+...+2+1 = n(n-1)/2 = O( n2) comparisons. 2.3. Pseudocode The number of swaps in the proposed algorithm may elaborate as follows: In simple pseudocode, enhanced selection sort algorithm might be expressed as: a) In the best-case scenario; if the input array is already sorted (ascending order), then there is no function enhanced selection sort (array , size) need to make swapping, since each element is in 1 if size > 1 then the correct place. 2 var index, temp, max b) In the average-case scenario; if the input array is 3 index := size-1 sorted in reverse (descending order), then the An Enhancement of Major Sorting Algorithms 57 total number of swapping operations is: floor of perform any swap operation, but selection sort (n/2). Since swapping the maximum value with the performs n swap operations. Writing in memory is last element means that the maximum and the more expensive in time than reading, since EBS minimum values are in the correct places. For performs less number of swaps (read/write) then it is example, if we have a descending sorted array as more efficient than selection sort when dealing with follows: an array stored in a secondary memory or in EEPROM (electrically erasable programmable read only 5 4 3 2 1 memory). However, there are many similarities Then the algorithm will swap the first element (max) between ESS and SS algorithms, as shown in Table 1. with the last element, as follows: Table 1. ESS vs SS algorithms. 1 4 3 2 5 Enhanced Selection Criteria Selection Sort Sort Since the last element before swapping was the Best Case O( n2) O( n2) minimum value, it is after swapping got in the correct Average Case O( n2) O( n2) 2 2 place and cannot be the maximum value in any of the Worst Case O( n ) O( n ) Memory O(1) O(1) next comparisons.