STACK
STACK
STACK
© 2018 Author(s).
Queue and Stack Sorting Algorithm Optimization and
Performance Analysis
Mingzhu Qiana), Xiaobao Wangb)
Huanggang Normal University No. 146, Xinggang second Road, Huanggang District, Hubei provincec, China
a)
112184972@qq.com
b)
11162634@qq.com
Abstract. Sorting algorithm is one of the basic operation of a variety of software development, in data structures course
specializes in all kinds of sort algorithm. The performance of the sorting algorithm is directly related to the efficiency of
the software. A lot of excellent scientific research queue is constantly optimizing algorithm, algorithm efficiency better as
far as possible, the author here further research queue combined with stacks of sorting algorithms, the algorithm is mainly
used for alternating operation queue and stack storage properties, Thus avoiding the need for a large number of exchange
or mobile operations in the traditional sort. Before the existing basis to continue research, improvement and optimization,
the focus on the optimization of the time complexity of the proposed optimization and improvement, The experimental
results show that the improved effectively, at the same time and the time complexity and space complexity of the algorithm,
the stability study corresponding research. The improvement and optimization algorithm, improves the practicability.
Keywords: Stack structure; Queue structure; Time complexity; spatial complexity; Stability.
INTRODUCTION
In the basic course of computer science, "data structure" in the study of many classical sorting algorithms, such as
insertion sort, exchange sort, selection sort, etc. The author studies a kind of new sorting algorithm in the realization
and performance analysis of the sort algorithm based on the combination of the queue and the stack. Now we continue
to research, improve and optimize the algorithm, focusing on the optimization of its time complexity, so that the
algorithm is more perfect.
PURPOSE OF SORTING
Whether the system software or application software, in many functions, there are two basic functions will exist,
one is the sort, the other is to find. Sort of figure too much, such as Baidu's competitive ranking, the stock market
rankings, etc. The purpose of the order is to adjust a set of "disorder" sequence to "order" of the recorded sequence [1].
By sorting can improve the data intuitive, effective, comparability, and to provide data for the convenience of the search,
improve the efficiency of search.
Time complexity
Time complexity is a mathematical method to describe the running time of the algorithm, and it can be used to
understand the operation efficiency of the algorithm. With mathematical computation time frequency t (n), where n
denotes the problem of scale, when n is constantly changing, time frequency t (n) will constantly change, if using an
040144-1
auxiliary function f (n), making when n tends to infinity and the limit of T (n) / F (n) value is not equal to zero constant,
f (n) is a (n) t order function. Denoted as T (n) =O (f (n)), O (f) ((n)) for the time complexity of the algorithm [2].
Space Complexity
Spatial complexity (Space Complexity) is an algorithm for the operation of the temporary storage space in the size
of the measure, remember to do S (n) =O (f (n)) [3]. An algorithm in computer memory occupied storage space,
including storage algorithm itself occupies storage space, occupied by the algorithm of input and output data storage
and algorithm in the running process of the temporary occupation of storage space the three. The space complexity of
the algorithm is mainly studied in the space of the temporary space occupied by the algorithm in the process of
operation [4].
Stability
Assumed in the records to be sort of sequence, the existence of multiple records with the same key, if after sorting,
the relative order of the records remains unchanged, namely in the original series, ri=rj and RI before RJ, and in the
sorted sequence, RI is still before the RJ is called this kind of sorting algorithm is stable; otherwise known for
instability [5].
TABLE 1. reference table for the performance of the basic sorting algorithm
In order to intuitively analyze various sorting algorithms, the time complexity changes, here are several orders of
magnitude of the time complexities of the time complexity of function diagram (Figure 1), from the figure is not
difficult to see that the with the n value increase of T (n) of two different time complex degree of different, obviously,
changes of T (n) = O (nlog2n) [6] to slowly. Such as heap sort than bubble sort.
040144-2
FIGURE 1. Time complexity of various orders of magnitude
040144-3
}
Running result
Algorithm 1 Can complete the sorting sorts, the only case of this algorithm with the help of a queue and stack to
complete data store operation alternately, each one of the most or minimum number of large number, the queue with
the data in the stack is to reduce a, for the next operation to reduce a CPU time. And this algorithm can't solve key
repeat sort operations. To do technical adjustment algorithm, designed the following algorithm.
Algorithm 2:
public void sort (Object a []) throws Exception {
// The improved algorithm, solving the key word repetition
int n=a.length;
ArrayStack S=new ArrayStack(n);
ArrryQueue Q=new ArrryQueue(n);
Object max,x;
boolean flag=false;// Monitoring elements whether the sequence order
/* When the data elements to repeat, to complete the algorithm implementation, increase the secondary variables k,
m */
int k=0;// The statistics of the number of the same element
int m=0;
for (int i=0; i<n;i++)
S.push(a[i]);// Will stay row data elements into the stack
For (int j=0; j<n;j++ )
{k=0;
max=S.getTop(); //The stack data elements
while (!S.isEmpty())
{x= S.getTop();
Q.enQueue(x);// Let the data elements into the queue
S.pop();//The stack elements pop
if((Integer)max<(Integer)x) max=x;
}
while(Q.notEmpty())
/*Process the data elements in the same situation, and the same number of statistics, endures in k */
{x=Q.getFront();
if((Integer)max==(Integer)x) // To improve the key statements
k++;
else S.push(x);
Q.deQueue();
}
// To improve the key statements
040144-4
if(k==0) a [m++] =max;
/* When there is no repeat one of the biggest data elements, you directly to the data elements in the array */
Else {
for (int l=0; l<k;l++)
/* When there is a repeated maximum data elements, will be the largest element in turn into an array */
a[m++] =max;}
n=n-k+1;// Then change the number of the sort}
}
Running result:
Algorithm 2 is mainly aimed at the data of the key word repetition of the situation to improve, the original algorithm
1 can be uniquely identified and deleted directly to the operation of the adjustment for the deletion of the same data and
save the problem to improve. Operation results from Figure 4 can be seen algorithm 2 to achieve a set of data elements
to sort the operation.
However, there are still a small disadvantage of algorithm 2, it did not consider the ordered sequence, if the given
sequence is ordered by the algorithm, will be a great waste of CPU time. Therefore, to the algorithm to optimize,
algorithm 3.
Algorithm 3:
public void sort (Object a []) throws Exception {// Optimization algorithm
int n=a.length;
ArrayStack S=new ArrayStack(n);
ArrryQueue1 Q=new ArrryQueue1(n);
Object max,x;
int flag=0; // Monitoring elements whether the sequence order
for(int j=0;j<n;j++)
{k=0;
max=S. get Top ();
while (!S. is Empty ())
{
x= S. get Top ();
Q. en Queue (x);
S. pop ();
If((Integer)max<(Integer)x) max=x;}
if(flag==n-1) break;// Key optimization
while (Q. not Empty ())
}
}
040144-5
Running result:
The combination of the queue and the stack of sorting algorithm skillfully use of the storage characteristics of the
alternate operation, thus avoiding the need for a large number of traditional sorting exchange or mobile operation.
Computing time complexity of the algorithm: if any given a set of data, a trip into the queue and with a number of
pressure stack operation, the required number of keywords to compare, the need for n sort. First order to compare n
keyword, then n times into the queue and n - 1 time into the stack. Second order to n - 1 keyword comparison, and
then the into queue and n to n - 1-2 times pressure stack. And so on.
Time complexity of the mathematical derivation process is as follows:
Computation time frequency
¦
L
Q L
=2n+2(n-1)+2(n-2)-2(n-3). 2*3+2*2+2*1
=2[(n+n+n+…n)- (1+2+3…n)]=( n2-n)/2
Computational time complexity
Find and time frequency corresponding to the first order function, according to the result of the above formula can
give the corresponding order functions such as f (n) =n2. The mathematical calculation :
T ( n) ( n 2 n) 1
lim t f ( n) = lim t 2 lim t n §1
n !f n !f n2 =1- n !f
So the algorithm for average time complexity O(n2). But the improved optimization algorithm, when to be sorted
as orderly, time complexity is O (n), at the same time, after repeated research, using queue sort and stack, the relative
position of the same keyword did not change, so the algorithm is to stabilize the algorithm. But refer to the algorithm
and stack the two storage structure, will use a lot of secondary storage space, the space complexity O (n).
040144-6
TABLE 2. Comparison of running time before and after optimization of algorithm 3
Algorithm optimization before the Algorithm optimization after the
Running times
implementation of sorting time(us) implementation of sorting time(us)
1 6187 7
2 6189 7
3 6173 8
4 6228 8
5 6220 8
6 6179 8
7 6331 8
8 6270 8
9 6415 8
10 6111 8
11 6129 7
12 6074 8
13 6030 7
14 6053 7
15 6024 7
16 6064 7
17 6010 7
18 6236 7
19 5995 7
20 6347 7
Average running
6163.25 7.45
time(us)
Note: run record n=10000
According to the above time complexity O(n2), the 10000=108 * n=10000 * n can be calculated, and the optimal
time complexity O (n) can be calculated, which can be calculated by n=10000. Is O(n2)/O(n) = 108/104=104
=6163.25/7.45=827.2819=0.8; according to the ratio of the average running time table 2 x 104 = 104. It can be seen
that the time complexity of the theoretical analysis and the calculated value of the actual running time are
approximately equal.
Space complexity
In the queue stack sorting algorithms cited the queue and stack storage structure of the two to store the data, will
use a large number of auxiliary storage space, with the upgrading of computer hardware, memory resources become
more and more abundant, the sorting algorithm of space complexity is not a too big problem [8], the space complexity
of O (n).
Stability
According to theory, the characteristics of the stack structure is advanced, the queue structure is characterized by
advanced first out, in the queue sorting algorithm for data initialization (known as a group of numbers: A1, A2, A3,
and so on, (an), data into the stack (an, an-1), A1, A2), after the operation, and let the data into the queue (an, an-1),... ,
A2, A1), in this process if there is ai=aj, original data AI before AJ, and stack into the queue after AJ appeared in
before the AI, data sorting is to queue structure in the data basis for the operation, which makes the relative positions
of the keywords to the same data change, so this algorithm is not stable algorithm.
SUMMARY
Using the stack structure to complete the sorting operation, the queue and stack technology is fully applied, and a
lot of mobile and switching operations in the traditional sorting algorithm are optimized. Data volume is not
040144-7
particularly large sorting can adopt the algorithm, but the amount of data too much, will occupy an abundance of
auxiliary storage space, increase the memory load, which will in subsequent research continues to improve.
ACKNOWLEDGMENTS
1. In 2017, the teaching reform project at Huanggang Normal University (data structure flipped class project)
2. Research and implementation of the network function of the open CNC system (project number: B201604),
Hubei Provincial Department of education science research program,2016
3. In 2015, Huanggang Normal University research project: Maintenance Strategy Research of CNC machine
tools based on RCM (project number: 2015017403)
4. Research and implementation of the network function of the open CNC system (project number: 2015016603)
Qianmingzhu,1979.7—, female, wuhan,hubei province,
Computer college lecturer, master, The network planner, The research direction: Computer network and teaching
reform, 112184972@qq.com
Wang Xiaobao, 1977.2 -, male, huanggang, hubei province, associate professor of mechanical and electrical
engineering college, master, research direction: the numerical control technology
REFERENCES
1. Chenyan. Data structure [m]. Beijing: higher education press, 2015.101 ~ 108.
2. Chezhanbin, Lizhanbo. The object-oriented data structure (Java) [m]. Zhengzhou: henan science and technology
press, 2013.35 ~ 46.
3. Hoare CAR. Algorithm 64: quichsort. [J]. Communications of the ACM,1961,4 (7):321
4. Knuth D E. The art of comuter programming, Volume 3: sorting and searching [M]. Boston: Addison-
Wesley,2015:138-141.Sannella, M. J. 1994. Constraint Satisfaction and Debugging for Interactive User
Interfaces. Doctoral Thesis. UMI Order Number: UMI Order No. GAX95-09398., University of Washington.
5. Lipschutz S. Theory and problems of data structures, Schaum’s outline series: international edition [M]. New
York: McGraw-Hill,2014:324-325.
6. Yanweimin. Data structure [m]. Beijing: tsinghua university education press, 2014.168 ~ 175
7. Qianmingzhu,Wangxiaobao. Based on the queue with the stack of sorting algorithm implementation and
performance analysis. [J]. Computer and information technology, 2013 [1].
8. Nardelli E, Proietti G. Efficient unbalanced mergesort [J]. Information Scieces, 2006, 176:1321-1337Reference.
040144-8