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

Experimental Study On The Five Sort Algorithms: You Yang, Ping Yu, Yan Gan

The document summarizes an experimental study that compares five sorting algorithms: bubble sort, selection sort, insertion sort, merge sort, and quicksort. It describes each algorithm, outlines their time and space complexities, and discusses how they were implemented and tested. The experiments found that insertion sort and selection sort perform well for small input sizes, while insertion sort and bubble sort work best for ordered sequences. Merge sort and quicksort scale better to large input sizes, with quicksort being the most efficient overall.

Uploaded by

angki_ang
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)
222 views4 pages

Experimental Study On The Five Sort Algorithms: You Yang, Ping Yu, Yan Gan

The document summarizes an experimental study that compares five sorting algorithms: bubble sort, selection sort, insertion sort, merge sort, and quicksort. It describes each algorithm, outlines their time and space complexities, and discusses how they were implemented and tested. The experiments found that insertion sort and selection sort perform well for small input sizes, while insertion sort and bubble sort work best for ordered sequences. Merge sort and quicksort scale better to large input sizes, with quicksort being the most efficient overall.

Uploaded by

angki_ang
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

Experimental Study on the Five Sort Algorithms

You Yang, Ping Yu, Yan Gan


School of Computer and Information Science
Chongqing Normal University
Chongqing, 400047, China
[email protected], {40465742, 790147687}@qq.com

Abstract - Sorting algorithm is one of the most basic research


fields in computer science. It’s goal is to make record easier to
A. Bubble Sort
search, insert and delete. Through the description of five sort Bubble sort algorithm[2] used in the experiments below was
algorithms: bubble, select, insert, merger and quick, the time and described by C language as:
space complexity was summarized. Furthermore, two categories of
O ( n 2 ) and O ( n log n) could be found out. From the aspects of
template<class Type>
void BubbleSort(Type *R,int n)
input sequence scale and input sequence random degree, some {int i,j; Type temp;
results were obtained based on the experiments. When the size of for(i=0;i<n-1;i++)
records is small, insertion sort or selection sort performs well. for(j=0;j<n-i-1;j++)
When the sequence is ordered, insertion sort or bubble sort if(R[j]>R[j+1])
performs well. When the size of records is large, quick sort or
{temp=R[j];R[j]=R[j+1];R[j+1]=temp;}}
merge sort performs well. Different application could select
appropriate sort algorithm according to these rules. In the best case, the input sequence is positive, the algorithm
needs n − 1 comparisons, and its time complexity is O ( n) In
Keywords - sort algorithm; bubble sort; select sort; insert sort; the worst case, the input sequence is negative, the algorithm
merger sort; quick sort 2

I. INTRODUCTION
needs ¦ (i − 1) = n(n − 1) / 2
i =n
comparisons, and its time

Sort is an important operation in computer programming. complexity is O(n 2 )  Whatever, the space complexity of
For any sequence of records or data, sort is an ordering
procedure by a type of keyword. The sorted sequence is benefit bubble sort is O (1) 
for record searching, insertion and deletion. Thus enhance the
efficiency of these operations. B. Select Sort
Two categories of sort algorithms were classified according Select sort algorithm[3] used in the experiments below was
to the records whether stored in the main memory[1]. One described by C language as:
category is the internal sort which stores the records in the main template<class Type>
memory. Another is the external sort which stores the records in
the hard disk because of the records’ large space occupation. In void SelectSort(Type R[]ˈint n)
fact, by utilizing the splitting and emerging, the external sort {int i,j,k;
could be converted to internal sort. Therefore, only internal sort for(i=1;i<n;i++)
algorithms such as bubble, select, insertion, emerging and quick {k=i;for(j=i+1;j<=n;j++)
sort were discussed bellow. if(R[j] <R[k]) k=j;
if(k!=i){R[0]=R[i];R[i]=R[k];
For the convenience, we make two assumptions bellow. R[k]=R[0];}}}
One is the sequence ordering ascending in default. Another is all
the records of the sequence were stored in the continuous It’s easy to prove: the algorithm needs n( n − 1) / 2
address memory cells. In this situation, the order of records was comparison operations whatever the input sequence is. That is
determined by the position which stored in the memory. The sort the algorithm’s time complexity is O(n 2 ) . Additionally, its
is the move operation of records.
space complexity of select sort is O (1) .
II. FIVE SORT ALGORITHMS
C. Insert Sort
Five sort algorithms were selected to do the experiments.
They were the sort algorithms of bubble, select, insert, merge Insert sort algorithm[4] used in the experiments below was
and quick. They were the popular sort algorithms in described by C language as:
applications. Because of that there were many variations of these template<class Type>
algorithms, therefore the algorithms were defined firstly in this void
section, then the performances of the algorithms were given by InsertSort(Type R[],int left,int right)
experiments in the next section.
{Type temp;int i,j;
for(i=left+1;i<right;i++)

978-1-4244-9439-2/11/$26.00 ©2011 IEEE


1314
Authorized licensed use limited to: Ingrid Nurtanio. Downloaded on October 01,2020 at 02:12:59 UTC from IEEE Xplore. Restrictions apply.
if(R[i]<R[i-1]){temp=R[i];j=i-1; int paritition(Type R[],int p,int r)
do{R[j+1]=R[j];j--;} {int i=p,j=r+1;Type x=R[p],temp;
while(j>=left&&temp<R[j]); while(1)
R[j+1]=temp;}} {while(R[++i]<x && i<r);
The time complexity of insert sort algorithm described while(R[--j]>x); if(i>=j) break;
above is O ( n 2 ) and its space complexity is O (1) . When the temp=R[i];R[i]=R[j];R[j]=temp;}
R[p]=R[j];R[j]=x;return j;}
input sequence is positive, the algorithm needs n − 1 comparison void quicksort(Type R[],int p,int r)
operations without any move operation. Otherwise, when the {if(p<r)
input sequence is negative, the algorithm needs {int q=paritition(R,p,r);
(n + 2)(n − 1) / 2 comparison operations while quicksort(R,p,q-1);quicksort(R,q+1,r);}}
(n + 4)(n − 1) / 2 move operations needed. The time complexity of this quick sort is O n ( ) . When the
2

input sequence is ordered (positive or negative), the algorithm


D. Merger Sort n −1

Merger sort algorithm[5,6] used in the experiments below was needs ¦ (n − i) =O(n ) 2
comparison operations. When the
described by C language as: i =1

template<class Type> input sequence is random, the time T ( n ) needed to sort n


void merge(Type R1[],Type R2[],int l,int records satisfied equation (1).
m,int r)
{int i=l,j=m+1,k=l; III. EXPERIMENTAL STUDY
while((i<=m) && (j<=r))
if(R1[i]<=R1[j]) R2[k++]=R1[i++]; In order to compare the performance of the five sort
else R2[k++]=R1[j++]; algorithms above, we use a desktop computer (AMD Sempron
if(i>m) Dual Core Processor 2100ˈ1.81GHzˈ2.87GB RAM, Windows
for(int q=j;q<=r;q++) R2[k++]=R1[q]; XP operating system) to do a serial experiments. Under vs6.0,
else for(int q=i;q<=r;q++) using C language, the programs test the performances of these
R2[k++]=R1[q];} five algorithms from input scale size by utilizing random function
void mergesort(Type R[],int left,int calling and time function calling.
right)
{if(left<right){int i=(left+right)/2; A. Experiments and Results
mergesort(R,left,i); When the input sequence is produced by a random function,
mergesort(R,i+1,right);int b[M]; and the input scale varied from 2000 to 128000, five sort
merge(R,b,left,i,right); algorithms’ time costs were demonstrated by table 1 and figure 1.
copy(R,b,left,right);}}
The time complexity of merge sort is O ( n log n ) and space TABLE 1. FIVE SORT ALGORITHMS’ TIME COST UNDER DIFFERENT INPUT SCALE

complexity is O (1) . Two operations named merge and records bubble select insert merge quick
2000 0.016 0.032 0.016 0.000 0.000
copy could be accomplished in O ( n ) . In the worst case, the 4000 0.063 0.125 0.047 0.016 0.000
8000 0.219 0.484 0.219 0.031 0.000
algorithm needs T ( n ) to calculate: 16000 0.844 1.921 1.016 0.078 0.000
32000 3.125 7.656 3.219 0.906 0.016
64000 12.000 30.235 14.594 3.703 0.016
­O (1) , n ≤1
T ( n) = ® (1) 128000 48.391 123.344 54.797 50.125 0.032
¯2T (n / 2) + O(n), otherwise From the table and the figure above, we got: when the scale
of input sequence was small, the difference of time cost between
From equation above, we could get T ( n ) = O ( n log n ) . these five algorithms was small. But with the scale of input
sequence becoming larger and larger, the difference became lager
and larger. Among these algorithms, the quick sort was the best,
E. Quick Sort
then the merge sort, traditional bubble sort and insert sort. The
Quick sort algorithm[7,8] used in the experiments below was worst was the select sort. When the number of the records was
described by C language as: greater than 62000, the time cost of the traditional bubble sort
template<class Type> and the insert sort was almost the same. From the number of this
point, the select sort cost more and more time than other.

1315
Authorized licensed use limited to: Ingrid Nurtanio. Downloaded on October 01,2020 at 02:12:59 UTC from IEEE Xplore. Restrictions apply.
Whatever, the time cost curve of quick sort was almost a line. It’s When the input sequence is negative, and the input scale
the slowest changing with the input scale increasing. varied from 1000 to 10000, five sort algorithms’ time costs were
demonstrated by table 3 and figure 3.
Five kinds of sorting algorithm spengding-time curve
140
Bubble From table 3 and figure 3, we could get some results bellow.
120
Select The first result was that the quick sort was the best and the select
Insert
Merger sort was the worst. The second result was that the time cost
100
Quick curves of the select sort and the traditional bubble sort were
almost the same. These two curves were exponential with the
input records increasing. The third result was that the time cost
time/s

80
cost (seconds)

curve of quick sort was linear, especially when the number of the
Spending

60 input records was less than 5000. The final result was that the
curves of the merge sort and insert sort were exponential also, but
Time

40
they were lower sensitive to the input scale increasing than the
select’s and the traditional bubble’s.
20

0
0 2 4 6 8 10 12 14
TABLE 3. FIVE SORT ALGORITHMS’ TIME COST WITH NEGATIVE INPUT SEQUENCE
The size of input 4
x 10 records bubble select insert merge quick
Fig.1 Time cost comparison of five sort algorithms under different input scale 1000 0.000 0.000 0.000 0.000 0.016
2000 0.016 0.031 0.015 0.000 0.016
When the input sequence is positive, and the input scale 3000 0.063 0.078 0.032 0.000 0.031
4000 0.109 0.125 0.063 0.000 0.047
varied from 1000 to 10000, five sort algorithms’ time costs were 5000 0.188 0.188 0.109 0.000 0.078
demonstrated by table 2 and figure 2. 6000 0.250 0.266 0.156 0.015 0.110
7000 0.344 0.359 0.219 0.015 0.156
8000 0.437 0.485 0.265 0.031 0.203
TABLE 2. FIVE SORT ALGORITHMS’ TIME COST WITH POSITIVE INPUT SEQUENCE
9000 0.562 0.609 0.359 0.031 0.265
records bubble select insert merge quick 10000 0.719 0.765 0.438 0.031 0.328
1000 0.000 0.000 0.000 0.000 0.000 g g p g g
0.8
2000 0.015 0.031 0.000 0.000 0.000 Bubble
3000 0.032 0.078 0.000 0.000 0.031 0.7
Select
Insert
4000 0.047 0.125 0.000 0.015 0.047 Merger
5000 0.078 0.188 0.000 0.000 0.078 0.6 Quick
6000 0.125 0.250 0.000 0.015 0.109
7000 0.156 0.344 0.000 0.015 0.156 0.5
time/s
cost (seconds)

8000 0.203 0.454 0.000 0.015 0.203


Spending

0.4
9000 0.265 0.594 0.000 0.015 0.266
10000 0.313 0.719 0.000 0.031 0.312
Time

0.3
Five kinds of sorting algorithm spengding-time curve
0.8
Bubble 0.2
Select
0.7
Insert 0.1
Merger
0.6 Quick
0
1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
0.5 The size of input
Number of records
time/s
cost (seconds)

Fig.3 Time cost comparison of five sort algorithms with negative input
Spending

0.4

sequences
Time

0.3

0.2
B. Performances Evaluation
0.1 Two criteria to evaluate the sort algorithms: time and space.
0
The time related to the comparison operations and move
1000 2000 3000 4000 5000
The size
6000
of input
Number
7000
of records
8000 9000 10000
operations of records in the algorithms. The space may dependent
or independent to the input sequence scale. If the additional space
Fig.2 Time cost comparison of five sort algorithms with positive input needed in the algorithm is independent to the input, its space
sequences
complexity is O (1) . Otherwise, its space complexity is O ( n ) .

1316
Authorized licensed use limited to: Ingrid Nurtanio. Downloaded on October 01,2020 at 02:12:59 UTC from IEEE Xplore. Restrictions apply.
Let N denote the number of input records, in which there are cost little, their space complexity is O (1) , independent to the
n elements were ordered. Then we could define K , called input scale.
ordered factor.
For the application, appropriate sort algorithm is selected
n according to the attributes of input sequence. If the input scale is
K= (2)
N small, insert and select algorithm is a good choice. If some
patterns or rules could be found in the input sequence, insert and
K ∈ [ 0,1] reflects the sort degree of a random sequence. K is bubble sort is a proper choice. But when the input scale is large,
bigger, more ordered exists in the sequence. Otherwise, K is merge sort and quick sort is the necessary choice essentially.
smaller, more random exists in the sequence. Let KCN represent
the number of comparison operation, and RCN represent the ACKNOWLEDGMENT
number of remove operation, T ( n ) and S ( n ) represent the This work is partially supported by Doctor Fund of
algorithm time complexity and space complexity respectively. Chongqing Normal University (10XLB006) to You Y. and
When K → 1 , then RCN → 0 and T ( n ) become lesser. When Researching Fund of Chongqing Teaching Bureau (KJ100623) to
Ping Y.
K → 0 , then RCN and T ( n ) become bigger. According to
S ( n ) whether independent or dependent to the input scale, its REFERENCES
value is O (1) or O ( n ) . [1] Knuth D. E. The Art of Computer Programming – Sorting and Searching.
Addison Wesley Publishing Company, Inc., 1973, 3:145-158.
[2] Debosiewicz W. An Efficient Variation of Bubble Sort. Information
Processing Letters. 1980, 11(1): 5-6.
IV. CONCLUSION [3] Iraj H., Afsari M. H. S., Hassanzadeh S. A New External Sorting
Based on the analysis above, we could obtain a summative Algorithm with Selecting the Record List Location. USEAS Transactions
result table below. Two types of the algorithms could be on Communications. 2006, 5(5):909-913.
[4] Kumari A., Chakraborty S. Software Complexity: A Statistical Case Study
classified. One is the type of time complexity O ( n 2 ) . They are through Insertion Sort. Applied Mathmatics and Computation. 2007,
190(1): 40-50.
the sort algorithms of traditional bubble, select and insert. [5] Jafarlou M. Z., Fard P. Y. Heuristic and Pattern Based Merge Sort.
Another is the type of time complexity O ( n log n ) . They are the Procedia Computer Science. 2011, 3: 322-324.
[6] Nardelli E., Proietti G. Efficient Unbalanced Merge-Sort. Information
merge sort and the quick sort. Science. 2006, 176(10):1321-1337.
[7] Feng H. Analysis of the Complexity of Quick Sort for two Dimension
Table. Jisuanji Xuebao. In Chinese. 2007, 30(6):963-968.
TABLE 4. FIVE SORT ALGORITHMS’ TIME COMPLEXITY COMPARISON
[8] Yueying P., Shicai L., Miao L. Quick Sorting Algorithm of Matrix. The 8th
Algorithms Average Worst Space International Conference on Electronic Measurement and Instruments.
Auguest 16-18, 2007. Xian, China. 2007, 2601-2605.
Bubble
O ( n2 ) O ( n2 ) O (1)
Select
O ( n2 ) O ( n2 ) O (1)
Insert
O ( n2 ) O ( n2 ) O (1)
Merge O ( n log n ) O ( n log n ) O (n)
Quick O ( n log n ) O (n 2
) O ( log n )
From the average time algorithms cost, the quick sort and
merge sort are superior to other three algorithms’. But in the
worst situation, the quick sort cost too much more time than the
merge sort.
When the input scale isn’t big, time cost of five sort
algorithms has not an obvious difference. But with the input scale
increasing, the quick sort has an obvious advantage over other
four algorithms.
For the space occupation, the quick sort and merge sort cost
two much than others’, their space complexity is O ( log n ) and
O ( n ) , dependent to the input scale. Other three sort algorithms

1317
Authorized licensed use limited to: Ingrid Nurtanio. Downloaded on October 01,2020 at 02:12:59 UTC from IEEE Xplore. Restrictions apply.

You might also like