Professional Documents
Culture Documents
Sorting Methods - Partial
Sorting Methods - Partial
called the radial distance, is one such valid total order relation on the 2dimensional plane that we have been dealing with in Cartesian Geometry.
Ok..so....Given a domain, we may come up with many such total order
relations, using which we can sort the given input subset of that domain. All
points having same value will be called equal w.r.t the defined relation. For
eg. all 2-dimensional points having same Euclidean Distance 'd' i.e all points
on the circumference of a circle of radius 'd' are considered to be equal in
order, if we use Euclidean Distance metric as a basis for sorting. The value of
a real number is another such metric over real number domain which we
generally use for sorting real numbers. The point is that, at least one such
definition should exist which our algorithm should use to decide relative
order among elements of a given domain. Sorting algorithms may sometimes
explicitly use the definition of this total order relation as a part of sorting
strategy. But, usually sorting algorithms you will learn are independent of
these considerations.
So, by this end, you should have acquired enough understanding of what
sorting is generally and that you should have realized by now that the first
thing a sorting algorithm requires is the existence of some comparison
concept (i.e a total order relation) based on which comparisons of elements
of the given domain can be done. Well, even a partially ordered set would
also do for sorting. In such a case, sorting would be a collection of chains
each, individually sorted and each chain is obviously totally ordered.
(2) Space-Time Complexity Analysis---Issues:..suddenly analysis coming in the scene when you have not seen even a single sorting
algorithm!!! why ???
...so, to speak, we don't need to see any such concrete algorithm to discuss the points
which are mentioned below...For example, to start learning Discrete Structures, you do
not need to see an example of a discrete structure so that you start learning.. all you
need at first is to think about what should not be a discrete structure and what in the
world can be a sufficient picture of what a discrete structure really is...then you start
studying examples of discrete structures which confirm to our concerns...
there are different methods of solving a problem..many methods...but some of them
adorned a lot for their sheer beauty and elegance. Be patient enough to see the issues
involved in implementing one of them, namely divide and conquer strategy.
recursive function calls (if the language supports such a feature). Recursive
calls are usually handled by what is called as an active function call stack.
This call stack occupies a certain portion of system memory to track function
calls, recursive or otherwise. This system stack grows and shrinks according
to the number of non-recursive calls or embedded active recursive calls
present at any moment in the runtime. Each active call record provides
enough memory for each function call to keep it's state. Local variables,
return address and local memory references to dynamically allocated
memory are all recorded in this call activation record (How else do you think
such a dynamic information can be stored, to record order and relevance of function
calls??). The amount of system stack dedicated to an algorithm is called the
situations (though not such screaming from the system..system cries out in code
words like
or
segmentation fault
or similar). So
We are still discussing probable issues of recursive method only. Why only recursive
method in discussion here? Are there not other methods where such issues will not
arise, may be?
Yes, there of course are other so-called non-recursive methods..like decrease and
conquer, memoization (not memoRization), greedy, branch-bound, backtrack, heuristicsearch space and alike..currently you do not fully understand what these words mean..
but, for time being, anyway, so far, Divide and Conquer (D&C) is like one of the jewels of
methods for solving a problem...and especially a jewel for sorting in the sense that it
does avoid unnecessary comparisons to establish sorted order...in this sense, it really
deserves space for discussions over the method.
(b) Time Complexity:It is assumed here that you have some idea about calculating time
complexity in terms of what is called as input size. It is also assumed that
know asymptotic analysis notations like O, o, and . So, we analyze the
time complexity of any algorithm, and especially sorting algorithm, from 3
perspectives.
(i) Worst Case Analysis:As is obvious from the words, by looking at the strategy of the
algorithm, we identify input instances which make algorithm
consume the highest possible number of operations. Purpose is to
bound from above the time requirement of the algorithm. There may
Now is the time to discuss case when pre-processing is costly. You can try this
case yourself. As an exercise, you can think about some problem and it's
algorithm and find out worst cases for given problem and algo. Think of some
way of pre-processing the input. Find such a problem and algo for which preprocessing input itself is costlier than algo time complexity. A hint to start with
is that input elements themselves should be so complex or so restrictive in
structure that any processing on them will probably take considerable time.
For example, how about the problem of sorting the singly linked list ???
From above, it is clear that worst case analysis must be done to show
the upper bound and may be it is not that good to label the
algorithms time complexity by it. But anyway, worst case equation is
the upper bound on an algorithm.
(ii) Best Case Analysis:Best cases are those which consume the minimum of resources and
computational steps. Symmetric to worst case analysis, discussion
for best case goes.
(iii) Average Case Analysis:Average case analysis is simply averaging the time complexity over
all input instances. Now, there are many kinds of
averages..arithmetic average, mean squared average and so on. We
have to choose an averaging notion which fits best to the analysis
case. Now input instances are generally large in number. So
obviously it is not feasible to list down all instances and calculate
time complexity for each. Here, using the notions O, o, , of
function classes comes to help. Though there exist as many time
complexity equations as are the instances, these equations usually
lead to only a few function orders. For ex. all functions of the form aX+b
where a and b are real numbers belong to O(X) class order. Though there exist uncountably infinite such functions, they all form just 1 class .
cost= c1
repeated
cost= c2
repeated
cost= c3
repeated
cost= c4
repeated
cost= c5
repeated
According to
case (i) average algo cost = i Average cost of Step(i) = i ci*ni/2
case (ii) average algo cost = x cost of Step(x) * nx, where x 3subset of {1,2,3,4,5} .. because out of 5, on an average 3 are
executed fully. This second case suggests that some 3 steps are
executed and other 2 not. This can be the case when steps are
executed conditionally i.e under if-else or switch kind of condition
clauses. So on an average number of times conditions evaluate to
false.
As you can see, these 2 may evaluate to different cost functions. But
for algorithm A, still the order of both these functions is same i.e they
belong to same function class asymptotically. But this need not be
true generally. What if the cost functions of different steps are of different
order? Then second case gives different time complexity functions
depending on which subset of steps actually got executed .
functions. You will see (and probably know) that merge sort and radix
sort are at least 2 such algorithms you know which are not in-place.
(b) Stable :Meaning, elements which are of same order (relative to sorting)
appear in the sorted order in the output sequence as they appeared
in the input sequence. Meaning, for example, if the integer 5 appears
in the sequence, say 3 times, then 5 should appear in output in the
same order as itself in the input. To clarify further, let us say, for the
sake of discussion, that 3 instances of this 5 are labeled as 5', 5'' and
5''' in the order of appearance in i/p(it is same 5, but 3 times). We
know that these 3 5's are same in order. But we want these 3 5's to
appear in o/p in the same order i.e 5', 5'', 5'''. If we can achieve this
for all elements which have multiple occurrence or the elements
which are order-equivalent w.r.t sorting (remember general magnitude
custom definition we talked about in first section?? total order relation ?? order
equivalence??), then we have achieved stability of the order of
appearance.
Apart from these desirable properties, we of course want an
algorithm to be efficient on various frontiers like time, memory
hierarchy (like cache, primary memory, secondary memory) and
other resources like network bandwidth if we are dealing with a huge
amount of data over computer interconnection network or any
communication network like radio communication and alike.
Now, after all these considerations, we are in a position to list
different strategies for sorting i.e list different algorithms for sorting.
We will discuss, in fair details, every sorting strategy that turns up in
usual B.E curriculum.
input set S like the range of data elements, frequency of occurrence and
probability distribution on the universe from where the data is chosen.
We can decide sorted ordering of S by comparing each element x with
every other element and checking how many elements are less than x and
putting x directly to its proper position using this information. Given n
elements, there are nC2 such pairs. Each pair (a,b) checks if a < b and
results in value 0 or 1 accordingly. This information can be stored and
reused repeatedly to swap elements to their proper positions. At a time
only 1 element needs fixing the position. So at a time only n-1 comparisons
are required. Given this, run time complexity becomes O(n 2) with space
complexity (n). We can summarize this as a result.
Sufficient Upper Bound on Run Time and Space Requirement for
Sorting By Comparison:Given a set of input elements S of size n and absence of further
information about elements of S, O(n2) run time procedure is sufficient
with (n) space complexity, for sorting by comparison.
Equipped with the above result, in later sections, we will see algorithms
having time complexity belonging to the above stated O(n 2) family of
functions and space complexity belonging to O(n) family of functions, not
(n) because this class of forces to require the variable space which is not
the case for many algorithms like Heap sort, Bubble sort, Insertion sort and
many others.
(4)
Below is a list of well studied algorithms which find their way to syllabus
almost everywhere. It is useful to think of an algorithm as a person having its
own mind and words to describe the thought. So every algorithm has a
strategy which is expressed formally as a step-by-step computational
procedure which we see in text. Consider S to be the set of elements to be
sorted. A few points below are to be considered.
(i) An element x S is called ith order statistic if x is ith smallest element in
the sorted order over S.
(ii) Given 2 elements i, j N, with i < j, we say Derangement has occurred if
S[i] > S[j]. This means smaller values occur before higher ones in the
sequence. The performance of sorting algorithms, most of the times, depends
on the magnitude of derangement present in the input sequence. Neatly,
performance of any algorithm is tightly linked to the magnitude of the
derangement in i/p sequence and how fast is any strategy in resolving
(4.1) SELECTION Sort:4.1.1 Strategy:Successively relocate the ith order statistic to location i. This is to be done by
Decrease-and-Conquer approach. Dec-Con approach follows here by
assuming that all i-1 statistics have been relocated to respective proper
positions. Given this, ith order is the 1st order statistic of remaining n-i
elements. Follow above procedure for increasing values of i. Separation
between sorted and unsorted elements is maintained by array.
4.1.2
Math Model:-
and
(1)
f1 = n
(2)
f2 = (n-1)
(3)
f3 = (n-i+1)
(4)
f4 = (n-i)
Begin
min = S[i]
For j from i+1 to n
Begin
If ( min > A[j] )
min= A[j]
(5)
f5 <= (n-i)
End
swap min and A[i]
(6)
f6 = (n-1)
End
4.1.4
I Perm
p(I) * T(I)
As seen from worst and best case analysis, (n 2) <= T(I) <= (n2).
So,
E[ T( Perm ) ] =
I Perm
I Perm
p(I) =
(n2)
T(n) = (n2)
(4.2) BUBBLE Sort:4.2.1 Strategy:Successively relocate the ith order statistic to location i in decreasing
values of i . This is to be done by Decrease-and-Conquer approach. DecCon approach follows here by assuming that all n-i statistics have been
relocated to respective proper positions. Given this, i th order is the(n-i)th order
statistic of remaining n-i elements. Follow above procedure for decreasing
values of i.
ith element is to be relocated to proper position by pair-wise derangement
check and swap(if required). This is very different from Selection sort
strategy. In selection sort, ith element does not find its own way through the
sequence because it is swapped exactly once to reach to its proper location.
Here, ith element finds its own way by moving progressively through the
sequence of swaps whenever required. Separation between sorted and
unsorted elements is maintained by array.
4.2.2
(i)
1 i p and 1 p
<n
= 0 , otherwise
SEQ
such
that
a i = ai , 1 i < k
ak = ak+1
a k+1 = ak
a i = ai , k+1 < i n,
and
SEQ
(restricted over 1 k n)
such that,
x i = fi-1swap ( xi-1 , F(xi-1 , i-1) ) , for all 1< i k
and
(iv)
BS = < f 1 f2 fn-1>
such that
and
x 1 = I/p
and
O/p = f n-1(sn-1, 2)
Description:(1) Bubble Sort is a series of passes. For next pass, size of sequence under
consideration reduces by 1, since in each pass 1 element is put to its
proper position in the sequence.
(2) Pass i=1 function f1 operates on x1 = initial sequence as its first
argument and length n-1+1= n. It fixes 1 st maximum element at position
n. This produces modified sequence with 1 st maximum at proper position.
This output sequence of f1 is given to f2 as x2. After a sequence of n-1
functions, output is the desired O/p sequence.
4.2.3
i=1
2.
3.
4.
increment i
Algo 1. Algorithmic analog of mathematical BS
Going ahead, each fi is a fpass function operating on sequence Si of length n-i+1. fpass
is itself a repeated evaluation of function f swap for n-i+1 times. So, going ahead, step
3 itself can be expressed algorithmically as another Repeat statement. This Repeat
is done for each execution of step 2. So extending Algo1,
1.
i=1
2.
3.
4.
5.
6.
7.
k=1
Algo 2.
Further, step 5 in Algo 2 is a swap function which will swap only after evaluating
function F. If function F returns 0 then no action should be taken because sequence
will be left unchanged for that point of execution. This conditional behavior can be
expressed algorithmically as an if-else structure. We dont require else part since,
as said above, no action to be taken if F returns 0. So, naturally evaluation of
function F becomes if test condition. So, extending Algo2,
1.
i=1
2.
3.
k=1
4.
5.
6.
7.
Increment k
8.
increment
i
Algo 3.
3.
4.
s= s
5.
k=1
6.
7.
If ( k = F(s,k) ) then
8.
9.
10.
Increment k
11.
increment
..(4)
f1 = n
For(k=1 to n-i)
(3)
f2 = n-i+1
If( a k > ak+1)
(1)
f3 = n-i
Swap a k with ak+1 (2)
f4 <= n-i
4.2.4
4.2.4.1 Space :=
Space(n) =
(1)
4.2.4.2 Time :4.2.4.2.1 Worst Case Analysis :- is when step labeled(2) executes to
the upper bound.
T(n) = i=1n j=1i ( (1) + (1) ) = ( n (n+1)2) = ( n 2)
This is the case when input sequence comes in reverse sorted order.
T(n) =
(n )
4.2.5 Discussion :Discussion here goes along the same lines as that for Selection sort.
There are a few changes, however . Owing to the fact that the i th element
finds its way through the sequence by progressive swaps, the number of
swaps is comparatively lot more than that in Selection sort. As observed,
number of swaps here obey
0 <= #swaps <= n *(n-1) 2 = O(n 2)
Difference is not only in number of swaps. Number of swaps here depends on
how bad a permutation has been received. In best case, #swaps = 0.
Tracking #swaps from pass to pass can surely be utilized to decide whether
the next pass is even necessary. If in the ith pass #swaps=0 then this directly
implies that array has been already sorted, since the highest i-1 statistics
have been repositioned properly and #swaps=0 for remaining n-i elements.
This provides a way to optimize the sort to respond to the best case
and cases nearly best case instance.
Tracking #swaps for a pass requires (1) extra space for tracking variable
and adds only (1) to time complexity per pass to set the tracking variable if
required. Extra condition of checking tracking variable for zero, before
initiating the next pass, requires (1) time per pass.
All of the above analysis implies that addition to the space and run time
complexity is only a constant and the lower bound i.e best case time
complexity reduces to (n). Thus, modified bubble sort would have following
run time analysis.
As a result of above analysis, if T(n) is the time
complexity of the sort then
(n) <= T(n) <= O(n2)