Comparison of Sorting Algorithms Based On Input Sequences: Ashutosh Bharadwaj Shailendra Mishra
Comparison of Sorting Algorithms Based On Input Sequences: Ashutosh Bharadwaj Shailendra Mishra
7
International Journal of Computer Applications (0975 – 8887)
Volume 78 – No.14, September 2013
comparison is 10000. Obviously, this algorithm is rarely used divide-and-conquer technique. We state each subproblem as
except in education. sorting a subarray A[p... r]. Initially, p = 1 and r = n, but these
values change as we recurse through subproblems. To sort
Algorithm A[p ... r]:
1. For I=1 to N-1 (for pass) Divide Step- If a given array A has empty, simply return; then
2. For k=1 to N-I (for comparison) it is already sorted. Otherwise it breaks A[p .. r] into two
subarrays A[p .. q] and A[q + 1 .. r], each containing about
3. If A[K]>A[K+1] half of the elements of A[p .. r]. That is, q is the halfway point
4. Swap [A(k) , A(k+1)] of A[p .. r].
Conquer Step-= Conquer by recursively sorting the two
Following are the procedure to sort a given set of element {8, subarrays A[p .. q] and A[q + 1 .. r].
7, 1, 2}. Combine Step- Combine the elements back in A[p .. r] by
merging the two sorted subarrays A[p .. q] and A[q + 1 .. r]
Data 8 7 1 2 into a sorted order. By executing this step, we will introduce a
Pass 1 7 8 1 2 procedure MERGE (A, p, q, r). Note that the recursion
7 1 8 2 bottoms out when the subarray has just one element, so that it
7 1 2 8 is trivially sorted. Advantage of merge sort is that they are
Pass 2 1 7 8 2 well suited for large data set. Disadvantage of merge sort is
1 7 2 8 that At least twice the memory requirements than other sorts.
Pass 3 1 2 7 8
Algorithm
MERG-SORT (A, p, r)
Here there are 4 elements so number of 1. IF p < r // Check for base case
comparison=8+7+1+2=19 2. THEN q = FLOOR[(p + r)/2] // Divide step
And the number of pass=4-1=3. 3. MERGE (A, p, q) // Conquer step.
4. MERGE (A, q + 1, r) // Conquer step.
2.3 SELECTION SORT 5. MERGE (A, p, q, r) // Conquer step.
It is among the most intuitive of all sorts. In selection sort we
find out the smallest elements in each pass and placed it in MERGE (A, p, q, r )
proper location. After that these steps are repeated until all the 1. n1 ← q − p + 1
list of element is sorted. This is the simplest method of 2. n2 ← r − q
sorting. In this method, to sort the data in increasing order, the 3. Create arrays L[1 ... n1 + 1] and R[1 .. . n2 + 1]
first element is compared with all the elements. If first 4. For i ← 1 TO n1
element is greater than smallest element than interchanged the 5. do L[i] ← A[p + i − 1]
position of elements. So after the first pass, the smallest 6. For j ← 1 TO n2
element is placed at the first position. The same procedure is 7. do R[j] ← A[q + j ]
repeated for 2nd element and so on until the element of list is 8. L[n1 + 1] ← ∞
9. R[n2 + 1] ← ∞
sorted.
10. i ← 1
Algorithm 11. j ← 1
1. for I=1 to N-1 12. For k ← p TO r
13. do IF L[i ] ≤ R[ j ]
2. min=A [I] 14. THEN A[k] ← L[I ]
3. for K=I+1 to N 15. i←i+1
16. ELSE A[k] ← R[j]
4. if (min>A [I]) 17. j← j + 1
5. min=A [K], Loc=K Following are the procedure to sort a given set of element {7,
6. Swap (A [Loc],A[I]) 4, 1, 3, 2, 5, 9, 7}.
7. Exit Data 6 4 1 3 2 5 9 7
Following are the procedure to sort a given set of element {8,
Pass 4 6 1 3 2 5 7 9
7, 1, 2}
1
Data 8 7 1 2 Pass 1 3 4 6 2 5 7 9
2
Pass 1 7 8 2 Pass 1 2 3 4 5 6 7 9
1 3
Pass 1 2 8 7
2 2.5 INDEX SORT
Pass 1 2 7 8 This sorting algorithm is the most simple and easy to use .This
3 algorithm performs working from lower index as well as
higher index and counting the elements from lower index to
higher index vice versa and fixing the elements indexes.
2.4 MERGE SORT
The operation that combines the element of two list in a one In this sorting algorithm first element of lower index is
single sorted list is called Merging. Merging operation is used checked with all smaller element with itself and if found and
in Merge sort for sorting an array. Merge sort is based on the count all small elements and add the lower index in count.
8
International Journal of Computer Applications (0975 – 8887)
Volume 78 – No.14, September 2013
Then from opposite count the sum (lower index +count) and Data 8 7 1 2
put it swaps the value with first element if smaller elements
are not found then fixed their position. While we taking Pass 1 2 7 1 8
element from upper index we count greater elements then we
subtract from upper index and if greater element is not found Pass 2 7 2 1 8
the fixed their position.
Pass 3 1 2 7 8
Algorithm
arraysize= n;
MyArray = rand(arraysize,1);
4. PERFORMANCE ANALYSIS
Bubble Sort, Selection Sort, Merge Sort, Insertion Sort Index
MyArray= [1020, 940, 80, 370, 60, 503, 40, 330, 20, 410]; Sort were applied in MATLAB and we test the random
sequence input of length 10, 100, 1000, 10000 to check the
low_ind=1;
performance. All the five sorting algorithms were executed on
up_ind=arraysize; machine with 32-bit Operating System having Intel(R) Core 2
Duo processor @ 2.13 GHz, 2.13 GHz and installed memory
counter1=0; (RAM) 2.00 GB. The times taken by the CPU at execution for
counter2=0; Different inputs are shown in the table. The Plot of length of
input and CPU time taken (m sec) is shown in figure. Result
while(low_ind<up_ind) shows that for all small length of input elements sequence the
performance of all the five techniques is all most same, but for
z=0;
the large input element sequence Merge sort is faster than
for j=low_ind+1 to up_ind Bubble sort, Sort and insertion Sort, Selection Sort and Index
Sort. From the results it can be concluded that Index Sort
if(MyArray(low_ind)>MyArray(j)) algorithm is working well for all length of input values. It
counter1=counter1+1; takes less CPU time than the Insertion Sort, Bubble Sort,
Selection Sort, and Merge Sort for small inputs but for larger
end inputs it takes greater time then Insertion Sort, Selection Sort,
end Merge Sort but takes less time than in case of Bubble Sort.
else
Bubble .000027 .000491 .034503 2.540110
low_ind=low_ind+1; Sort
end
j=up_ind-1; Selection .000029 .000147 .012923 1.60680
Sort
while (j>=low_ind)
if(MyArray(up_ind)<MyArray(j)) Merge Sort .000011 .000126 .01116 1.04148
counter2=counter2+1;
Index Sort .000012 .000128 .022339 2.348677
end
j=j-1;
end
if(counter2>0)
t=MyArray(up_ind);
MyArray(up_ind)=MyArray(up_ind-counter2);
MyArray(up_ind-counter2)=t;
counter2=0;
z=1;
else
up_ind=up_ind-1;
end
end
9
International Journal of Computer Applications (0975 – 8887)
Volume 78 – No.14, September 2013
2.5 6. REFERENCES
[1] Comparison of Sorting Algorithms (On the Basis of
Average Case) Pankaj Sareen.
2 insertion [2] A Comparison Based Analysis of Four Different Types of
Sorting Algorithms in Data Structures with Their
Bubble Performances.
1.5 [3] Min-Max Select Bubble Sorting Algorithm.
Selection
Merge [4] CSCE 3110Data Structures & Algorithm Analysis.
1 [5] Assortment of different sorting algorithms. Amardeep
Index Singh, Monika, Vandana, Sukhnandan Kaur.
0.5 [6] Robustness versus Performance in Sorting and
Tournament Algorithms by Wilfried Elmenreich, Tobias
Ibounig, István Fehérvári.
0 [7] Seymour Lipschutz (2009) Data Structure with C, Schaum
10 100 100010000 Series, and Tata McGraw-Hill Education.
[8] Merge sort:- Merge sort algorithm, C. BronTechnological
Figure1- X axis- time (m sec) Y axis input sequence (10, Univ., Eindhoven, The Netherlands, Communications of
100, 1000, 10000 the ACM Volume 15 Issue 5, May 1972, ACM New
York, NY, USA .
5. CONCLUSION AND FUTURE SCOPE
From the results it can be concluded that Index Sort algorithm [9] Review on sorting algorithms A comparative study on two
is working well for all length of input values. It takes less sorting algorithms By Pooja Adhikari.
CPU time than the Insertion Sort, Bubble Sort, Selection Sort
and Merge Sort for small inputs and it takes greater time in [10] An Enhancement of Major Sorting Algorithms Jehad
case of Insertion sort, Selection Sort Merge sort but takes less Alnihoud and Rami Mansi.
time than in case of Bubble Sort for larger inputs. In the future [11] Introduction to Algorithms by Thomas H. Cormen,
work more effective sorting algorithm can be proposed with Charles E. Leiserson, Ronald L. Rivest, fifth Indian
running time. printing (Prentice Hall of India private limited), New
Delhi-110001
6. ACKNOWLEDGMENTS
Our thanks to Dr. Shailendra Kumar Mishra (Head of
Department) of Computer Science, Principal, BTKIT
Dwarahat, for providing necessary infrastructure for the
IJCATM : www.ijcaonline.org 10