Professional Documents
Culture Documents
Sorting
Definitions of an Algorithm
An algorithm is a step by step procedure, which defines a set
of instructions to be executed in a certain order to get the
desired output.
(Or)
An algorithm is a finite set of instructions which, if followed
accomplish a particular task.
Characteristics of an Algorithm
Analysis of an Algorithm
There are two primary methods for analyzing algorithms formally
i) Correctness: The primary method of validity for algorithms is using a
proof of correctness.
This is corrected through verification of the algorithm accomplishing its
purpose formally, and terminating in k finite steps.
ii) Complexity: An algorithm may be analyzed in the context of its
complexity.
Asymptotic analysis of an algorithm refers to defining the mathematical
foundation/framing of its un-time performance. Using asymptotic
analysis, we can very well conclude the best case, average case and worst
case scenario of an algorithm
This is typically via measures of run-times using big-O, or big-Omega, or
big-theta notation.
Asymptotic Notations
Big Oh Notation,
The n is the formal way to express the upper bound of an algorithm's running
time. It measures the worst case time complexity or longest amount of time an
algorithm can possibly take to complete. It is asymptotic upper bound.
O(g(n)) = {f(n) : positive constants c and n0, such that n n0, we have 0
f(n) cg(n) }
Omega Notation,
The n is the formal way to express the lower bound of an algorithm's
running time. It measures the best case time complexity or best amount of
time an algorithm can possibly take to complete
(g(n)) = {f(n) : positive constants c and n0, such that n n0, we have 0
cg(n) f(n)}
Theta Notation,
The n is the formal way to express both the lower bound and upper bound of an
algorithm's running time. It is represented as follows
(g(n)) = {f(n) : positive constants c1, c2, and n0, such that n n0, we have 0
c1g(n) f(n) c2g(n)}
(g(n))
From data structure point of view, the data in the data structures are
processed by the below mentioned operations are:
Searching Algorithm to search an item in a data structure.
Sorting Algorithm to sort items in a certain order.
Insertion Algorithm to insert a new item in a data structure.
Updating Algorithm to update an existing item in a data structure.
Deletion Algorithm to delete an existing item from a data structure.
Traversing Algorithm to visit each item in a data structure at least
once.
Merging- Algorithm to merge one item with another in a data
structure.
Sorting
A process that organizes a collection of data into either ascending or
descending order.
Classification of sorting
a. External sorting
b. Internal sorting
c. Stable Sorting
External sorting
External sorting is a process of sorting in which large blocks of datas are
stored in storage devices are moved to the main memory and then sorted. I.e. a
sorting can be external if the records that it is sorting are in auxiliary storage.
Internal sorting
Internal sorting is a process of sorting the datas in the main memory. I.e. a
sorting can be internal if the records that it is sorting are in main memory.
Stable sort
A sorting technique is called stable if for all records i and j such that k[i]
equals k[j], if r[i] precedes r[j] in the original file, r[i] precedes r[j] in the sorted
file. That is, a stable sort keeps records with the same key in the same relative order
that they were in before the sort.
Insertion Sort
In pass p, move the pth element left until its correct place is found among the first p
elements.
Example: Card Players (As they pick up each card, they insert it into the proper
sequence in their hand
Steps
Let A be the array of n numbers.
Our aim is to sort the numbers in ascending order.
Scan the array from A[1] to A[n-1] and find A[R] where R=1,2,3(N-1) and
insert into the proper position previously sorted sub-array, A[1],A[2]..A[R-1].
If R=1, the sorted sub-array is empty, so A[1] is sorted itself.
If R=2, A[2] is inserted into the previously sorted sub-array A[1], i.e., A[2] is
inserted either before A[1] or after A[1].
If R=3, A[3] is inserted into the previously sorted sub-array A[1],A[2] i.e., A[3] is
inserted either before A[1] or after A[2] or in between A[1] and A[2].
We can repeat the process for(n-1) times, and finally we get the sorted array.
Example: the sorts through a list of six numbers. Sorting these data
requires five sort passes. Each pass moves the wall one element to the right
as an element is removed from the unsorted sub-list and inserted into the
sorted list.
L1.20
L1.21
L1.22
L1.23
L1.24
L1.25
L1.26
L1.27
L1.28
L1.29
L1.30
done
https://courses.cs.vt.edu/csonline/Algorithms/Lessons/InsertionCardSort/in
sertioncardsort.swf
Selection sort
Example 1
Example 2
The best case, the worst case, and the average case of the selection
sort algorithm are same. All of them are O(n2).
This means that the behavior of the selection sort algorithm does not
depend on the initial organization of data.
Since O(n2) grows, so rapidly, the selection sort algorithm is
appropriate only for small n.
Although the selection sort algorithm requires O(n 2) key comparisons,
it only requires O(n) moves.
A selection sort could be a good choice if data moves are costly, but
key comparisons are not costly (sort keys, long records).
Disadvantages of selection
sort
Shell Sort
Example
We take the below example to have an idea, how shell sort works? We
take the same array we have used in our previous examples. For our
example and ease of understanding we take the interval of 4. And make
a virtual sub list of all values located at the interval of 4 positions. Here
these values are {35, 14}, {33, 19}, {42, 27} and {10, 44}
We compare values in each sub-list and swap them (if necessary) in the
original array. After this step, new array should look like this
Then we take interval of 2 and this gap generates two sub lists - {14,
27, 35, 42}, {19, 10, 33, 44}
We compare and swap the values, if required, in the original array.
After this step, this array should look like this {14, 27, 35, 42}, {10,
19, 33, 44}
And finally, we sort the rest of the array using interval of value 1. Shell
sort uses insertion sort to sort the array {10, 14, 19, 27, 33, 35, 42, and
44}.
The step by step depiction is shown below
We see that it required only four swaps to sort the rest of the array.
Example 2
0
5 6
9 10
11 12 13 14 15
Bubble Sort
Another well known sorting method is bubble sort. It differs from the
selection sort in that instead of finding the smallest record and then
performing an interchange two records are interchanged immediately
upon discovering that they are of out of order.
When this approach is used there are at most n-1 passes required. During
the first pass k1and k2 are compared, and if they are out of order, then
records R1 and R2 are interchanged; this process is repeated for records
R2 and R3, R3 and R4 and so on .this method will cause with small keys
to bubble up.
After the first pass the record with the largest key will be in the nth
position. On each successive pass, the records with the next largest key
will be placed in the position n-1, n-2,n respectively, thereby
resulting in a sorted table.
After each pass through the table, a check can be made to determine
whether any interchanges were made during that pass. If no interchanges
occurred then the table must be sorted and no further passes are required.
Example 1
One pass
Example 2
Original text
After pass 1
After pass 2
After pass 3
After pass 4
Quick Sort
Like merge sort, Quick sort is also based on the divide-andconquer paradigm. But it uses this technique in a somewhat
opposite manner, as all the hard work is done before the
recursive calls.
It works as follows:
First, it partitions an array into two parts,
Then, it sorts the parts independently,
Finally, it combines the sorted subsequences by a simple
concatenation.
QUICK_SORT(K,LB,UB)
Given a table K of N record, this recursive procedure sorts the
table, as previously described, in ascending order.
A dummy record with key K [N+1] is assumed where
K[I]<=K[N+1] for all 1<=<=N . The Integer parameters LB
and UB denote the lower and upper bounds of the current sub
table being processed.
The indices I and J are used to select certain keys during the
processing of each sub table. KEY contains the key value which
is being placed in its final position within the sorted sub table.
FLAG is a logical variable which indicates the end of the process
that places a record in its final position.
When FLAG becomes false, the input sub table has been
partitioned into two disjointed parts.
Variables used
K Array to hold elements
LB,UB Denotes the lower and upper bounds of the current
sub table
I,J Used to select certain keys during processing
KEY Holds the key value of the final position
FLAG Logical variable to indicate the end of process
Partitioning places the pivot in its correct place position within the array.
Arranging the array elements around the pivot p generates two smaller
sorting problems.
Sort the left section of the array, and sort the right section of the array.
When these two smaller sorting problems are solved recursively, our
bigger sorting problem is solved.
First, we have to select a pivot element among the elements of the given
array, and we put this pivot into the first location of the array before
partitioning.
Partition Function
Invariant for the partition algorithm
QuickSort Algorithm
Using pivot algorithm recursively we end-up with smaller
possible partitions. Each partition then processed for quick sort.
We define recursive algorithm for quicksort as below
Step 1 - Make the right-most index value pivot
Step 2 partition the array using pivot value.
Step 3 quicksort left partition recursively
Step 4 quicksort right partition recursively
Quicksort is slow when the array is sorted and we choose the first element as the pivot.
Although the worst case behavior is not so good, its average case behavior is much better
than its worst case.
So, Quicksort is one of best sorting algorithms using key comparisons.
Merge Sort
It is a recursive algorithm.
Divides the list into halves,
Sort each halve separately, and
Then merge the sorted halves into one sorted array.
Example 1:
Example 2
Example 3
Radix Sort
The first node is pointed by a pointer called FIRST the vectors T and B is the
pointers to store the address of the rear and front of the queue. In particular,
these denoted as T[I] and B[I] , are the top and bottom of the records.
The pointer R is used to denote the current record. Next is the pointer to
denote the next record. Prev pointer is used to combine the queue. D is used
to examine the digit.
Variables Used
- First Pointer to point the First Node in the table.
- TDenotes Top of the queue.
- BDenotes the bottom of the Stack.
- J Pass Index
- PPocket index to point to the temporary table.
- R Pointer to store the address of the current record being handled.
- NEXTPointer which has the address of the next record in the table.
- PREV Pointer to combine the pockets.
- DCurrent digit being handled in the current Key field.
Example 1:
mom, dad, god, fat, bad, cat, mad, pat, bar, him original list
(dad,god,bad,mad) (mom,him) (bar) (fat,cat,pat) group
strings by rightmost letter
dad,god,bad,mad,mom,him,bar,fat,cat,pat
combine
groups
(dad,bad,mad,bar,fat,cat,pat) (him) (god,mom) group strings
by middle letter
dad,bad,mad,bar,fat,cat,pat,him,god,mom
combine
groups
(bad,bar) (cat) (dad) (fat) (god) (him) (mad,mom) (pat) group
strings by first letter
bad,bar,cat,dad,fat,god,him,mad,mom,par
groups (SORTED)
combine
Example 2: