0% found this document useful (0 votes)
67 views4 pages

Comparison of Sorting Algorithms Based On Input Sequences: Ashutosh Bharadwaj Shailendra Mishra

The document compares different sorting algorithms based on input sequences. It describes Insertion Sort, Bubble Sort, Selection Sort and Merge Sort algorithms. For each algorithm, it provides the basic steps/procedure. It then tests the performance of these sorting algorithms by measuring the processing time taken to sort elements in increasing sizes (10, 100, 1000, 10000). The goal is to determine which algorithm sorts elements the fastest based on processing speed and memory usage.

Uploaded by

fazal zubair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views4 pages

Comparison of Sorting Algorithms Based On Input Sequences: Ashutosh Bharadwaj Shailendra Mishra

The document compares different sorting algorithms based on input sequences. It describes Insertion Sort, Bubble Sort, Selection Sort and Merge Sort algorithms. For each algorithm, it provides the basic steps/procedure. It then tests the performance of these sorting algorithms by measuring the processing time taken to sort elements in increasing sizes (10, 100, 1000, 10000). The goal is to determine which algorithm sorts elements the fastest based on processing speed and memory usage.

Uploaded by

fazal zubair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

International Journal of Computer Applications (0975 – 8887)

Volume 78 – No.14, September 2013

Comparison of Sorting Algorithms based on Input


Sequences
Ashutosh Bharadwaj Shailendra Mishra, Ph.D
Dept. of Computer Science & Head of Dept. of
Engineering Computer Science &
BTKIT, Dwarahat Engineering
Almora, Uttarakhand BTKIT, Dwarahat
Almora, Uttarakhand

ABSTRACT 2. Sorting Algorithms


Ordering is a very important for mankind .If anything is in
unordered then it will not easily understand by anyone but if it
2.1 Insertion Sort
This sorting technique is used for small number of elements.
is in order then it will easily understand and used by anyone.
The insertion sort works like playing cards in which each card
So ordering is a very important issue in computer science
is placed at its proper place while playing in hands of a
also. In computer science many programming applications use
person. Cards are placed in an order which is also called a
ordering to solving a problem either it is in ascending or
sorting order .Sorting a hand of playing card is one of the real
descending order. In this paper we discuss four sorting
examples of insertion sort. This algorithm first sorts the first
algorithms which are already existed named as Insertion Sort,
two elements of the array. It then inserts the third element in
Bubble Sort, Selection Sort, Merge Sort and we design a new
its proper place in relation to the first 2 sorted elements. This
sorting algorithm named as index sort also. In this paper we
process continues until all of the remaining elements are
check the performance and comparison of all five sorting
inserted in their proper position. Insertion sort can take
algorithm on the basis of increasing the no of elements in
different running time to sort two input sequences of the same
bulk. We check how much processing time is taken by all four
size of array depending upon how nearly they already sorted.
sorting algorithms with Index Sort and compared them and
finding which sorting algorithm takes less time to sort the It sorts the element of small array very fast but in sorting the
elements like 10, 100, 1000, 10000 . If any algorithm takes elements of big array takes very long time.
less processing time it means that it sorts the element faster Algorithm
than others. The processing time of a sorting algorithm is 1. For I=2 to N
based on the processing speed of a Processor as well as
internal memory (RAM) used by the system. 2. A [I] =item, J=I-1
3. WHILE j>0 and item<A[J]
Keywords
Insertion Sort; Bubble Sort; Selection sort; Merge Sort; Index 4. A[J+1]=A[J]
Sort.
5. J=J-1
1. INTRODUCTION
Sorting is a fundamental operation in computer science. 6. A [J+1]=item
Sorting generally refers to the operation of ordering data in a
given sequence such as increasing sequence or decreasing Following are the procedure to sort a given set of element {8,
sequence. There are many fundamental and advance sorting 7, 1, 2}.
algorithms. All sorting algorithm are problem oriented means
they work well on some special problem and do not work well Data 8 7 1 2
for any kind of a problems. Sorting algorithms are applied on
specific kind of problems. Sorting algorithms are used for Pass 1 7 8 1 2
small number of elements, some sorting algorithms are used
Pass 2 1 7 8 2
for large numbers, some sorting algorithms are used for
floating number of data, and some are used for repeated Pass 3 1 2 7 8
values in a list. We sort data in numerical order or
alphabetical order, arranging the list either in increasing order
or decreasing order and alphabetical value like addressee key. 2.2 BUBBLE SORT
In this paper we discuss four sorting algorithms which are
The sorting algorithm is affectionately named the "bubble"
already exist named as Insertion Sort, Bubble Sort, Selection
sort. This sorting algorithm is perhaps one of the simplest
Sort, Merge Sort and we design a new sorting algorithm
sorting algorithms in terms of complexity. It makes use of a
named as index sort. In this paper we check the performance
sorting method known as the exchange method. This
and comparison of all five sorting algorithm on the basis of
algorithm compares pairs of adjacent elements and makes
increasing the no of elements in bulk. We check how much
exchanges if necessary. The name comes from the fact that
processing time is taken by all four sorting algorithms with
each element "bubbles" up to its own proper position. Here is
Index Sort and compared them and finding which sorting
how bubble sort would sort the integer array 8 7 1 2,It is a
algorithm takes less time to sort the elements like 10, 100,
simplest sorting algorithm used in computer science
1000, 10000 . If any algorithm takes less processing time it
algorithm. If we have 100 elements then the total number of
means that it sorts the element faster than others.

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.

if(counter1>0) Table 1: Processing time (m sec) for different lengths of


input sequences
t=MyArray(low_ind);
Sorting 10 100 1000 10000
MyArray(low_ind)=MyArray(counter1+low_ind);
Techniques
MyArray(counter1+low_ind)=t;
counter1=0; Insertion .000012 .000208 .017085 1.51616
z=1; 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

research work and helping me to write this paper. I would also


like to thanks to all my colleagues who give me a valuable
3 support in writing this paper.

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

You might also like