Professional Documents
Culture Documents
NAME: N.RAJA
CLASS:
DATE:
05/11/2013
Sorting
Sorting algorithm is an algorithm that puts elements of a list in a certain order . The most-used orders are numerical order and lexicographical order. Sorting algorithm classification: Computaional complexcity. Memory utilization Stability-Maintaining relative order of records with equal eys. !o.of comparisions Methods applied li e insertion"exchange""selection"merging etc. Sorting is a process of linear ordering of list of ob#ects Sorting techniques are categoried into: $. %nternal sorting &. 'xternal sorting Internal sorting: %t ta es the place in the main memory of a computer. 'g: (ubble sort"%nsertion sort"Shell sort")uic sort"*eap Sort"etc External sorting: %t ta es the place in secondary memory of a computer"Since the number of ob#ects to be stored is too large to fit in main memory 'g:Merge sort"Multiway Merge"+olyphase merge.
Bubble Sort:
(ubble Sort is a simple sorting algorithm. %t wor s by repeatedly stepping through the list to be sorted" comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed" which indicates that the list is sorted. The algorithm gets its name from the way smaller elements ,bubble, to the top of the list. (ecause it only uses comparisons to operate on elements" it is a comparison sort .
Analysis of bubble sort : Worest case performance O(n2) Best case performance O(n) Average case performance O(n2) Advantages : It is very simple to implement. Disadvantages : If list is very large it take long time to evaluate. Algorithm steps : Step 1: Read the input list si e. Step 2: Read the list elements. Step !: Assign count =" Step #: Repeat the loop until count less than list si e.(for n-1 passes) i) Assign inner$count=count+1 ii) Repeat the loop inner$count less than list si e (for arrange the 1) If list [count]> list [inner$count] (check element value ) 2) temp = list [count] !) list [count]=list[inner$count] #) a[inner$count]=temp
SAMP E I!P"# A!$ %"#P"#: 'nter how many elements you want to sort 5 'nter the numbers 5 @ A & $ +?SS-/$ $ 5 @ A & +?SS-/& $ & 5 @ A +?SS-/A $ & A 5 @ +?SS-/@ $ & A @ 5 >inal sorted list is $ & A @ 5
Algorithm Steps: $. >ind the minimum value in the list &. Swap it with the value in the first position A. Depeat the steps above for the remainder of the list 0starting at the second position and advancing each time3
printf0,7n8d,"?1x234 = insertionEsort0?"n34 printf0,7n7nSBDT'I G%ST7n7n,34 for0x:;4x.n4x<<3 6 printf0,7n8d,"?1x234 = getch034 = Sample Input and %utput: 'nter how many elements you want to sort 5 'nter the numbers 5 @ A & $ +?SS-/$ $ 5 @ A & +?SS-/& $ & 5 @ A +?SS-/A $ & A 5 @ +?SS-/@ $ & A @ 5 (inal sorted list is ) * + , -
J Simple implementation J 'fficient for 0quite3 small data sets J ?daptive" i.e. efficient for data sets that are already substantially sorted: the time complexity is B0 n < d3" where d is the number of inversions J More efficient in practice than most other simple quadratic 0i.e. B 0 n &33 algorithms such as selection sort or bubble sort: the average running time is n &K@" and the running time is linear in the best case J Stable" i.e. does not change the relative order of elements with equal eys J %n-place " i.e. only requires a constant amount B0$3 of additional memory space J Bnline " i.e. can sort a list as it receives it imitations o& insertion sort: %t is relatively efficient for small lists and mostly- sorted list. %t is expensive because of shifting all following elements by one. ?nalysis of insertion sort: Corest case performance B0n&3 (est case performance B0n3 ?verage case performance B0n&3
Algorithm steps: Step $: Dead the input list size. Step &: Dead the list elements. step A: pass:$ step @: Depeat the following steps until pass reach size-$ 0for !-$ passes3 i3 ey:list1+ass2 ii3 swap:pass-$ iii3 Depeat the follwoing steps if this condition is true list1swap2/ ey and swap /: ;.0for comparision3. a3 list 1sawp<$2:list1swap 2 b3 swap-iv3 list 1swap<$2
SAMP E I!P"# A!$ %"#P"#: '!T'D T*' G%M%T : 5 '!T'D T*' 'G'M'!TS B!' (H B!' 5 @ A & $ SBDT'I G%ST :$ & A @ 5
imitation: Dequires More Memory space. Algorithm steps: J +ic an element" called a pivot" from the list. J Deorder the list so that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it 0equal values can go either way3. ?fter this partitioning" the pivot is in its final position. This is called the partition operation. J Decursively sort the sub-list of lesser elements and the sub-list of greater elements.
6 pivot:a1first24 i:first4 #:last4 while0i.#3 6 while0a1i2.:pivot99i.last3 i<<4 while0a1#2/:pivot99#/first3 #--4 if0i.#3 swap0a"i"#34 = swap0a"first"#34 quic 0a"first"#-$34 quic 0a"#<$"last34 = = void swap0int a12"int i"int #3 6 int temp4 temp:a1i24 a1i2:a1#24 a1#2:temp4 =
SAMP E I!P"# A!$ %"#P"#: 'nter the limit : 5 'nter the elements 5 @ A & $ The sorted list is $&A@5
6 if0a1i2.a1#23 b1 2:a1i<<24 else b1 2:a1#<<24 <<4 = while0i.:l$3 b1 <<2:a1i<<24 while0#.:l&3 b1 <<2:a1#<<24 i:f$4 #:;4 while0i.:l& 99 #. 3 a1i<<2:b1#<<24 =
SAMP E I!P"# A!$ %"#P"#: 'nter the limit : 5 'nter the elements 5 @ A & $ Sorted list: $ & A @ 5
Analysis o& the heap sort: Corest case performance B0n log n3 (est case performance B0n log n3 ?verage case performance B0n log n3 Ad'antages o& heap sort: $.%t is efficient for sorting large number of elements. &.%t has the advantages of worst case B0! log !3 running time. imitations: $. %t is not a stable sort. &. %t requires more processing time Alogrithm steps: Step$: Dead the size of the list Step&: Dead the elements of the list StepA: (inary heap construction. $3 Structrue +roperty: >or any element in array position %"the left child is in position &i"the right child is in &i<$"0ie3 the cell after the left child. *2 0eap %rder Property: The value in the parent node is smaller than or equal to the ey value of any of its child node.(uild the heap"apply the heap order propery starting from the right. most non-leaf node at the bottom level. Step @: Ielete min routine is performed. The array elements aer stored using deletemin operation"which gives the elements arranged in descending order.
= void ad#ust0int i"int n3 6 int #"element4 #:&Fi4 element:hsort1i24 while0#.:n3 6 if00#.n3990hsort1#2.hsort1#<$233 #:#<<4 if0element/:hsort1#23 brea 4 hsort1#K&2:hsort1#24 #:&F#4 = hsort1#K&2:element4 = SAMP E I!P"# A!$ %"#P"#: 'nter !o Bf 'lements:5 'nter 'lements To (e Sorted 5 @ A & $ Sorted 'lement : $ & A @ 5
Hashing
We have all used a dictionary, and many of us have a word processor equipped with a limited dictionary, that is a spelling checker. We consider the dictionary, as an ADT. E amples of dictionaries are found in many applications, including the spelling checker, the thesaurus, the data dictionary found in data!ase management applications, and the sym!ol ta!les generated !y loaders, assem!lers, and compilers. "enerally we would want to perform the following operations on any sym!ol ta!le# Determine if a particular name is in the ta!le $etrieve the attri!utes of that name %odify the attri!utes of that name &nsert a new name and its attri!utes Delete a name and its attri!utes 'tatic HashingHash Ta!les &n static hashing, we store the identifiers in a fi ed si(e ta!le called a hash ta!le. We use an arithmetic function, f, to determine the address, or location, of an identifier, , in the ta!le. Thus, f ) * gives the hash, or home address, of in the ta!le. The hash ta!le ht is stored in sequential memory locations that are partitioned into ! !uckets, ht +,-, ., ht +!./-. Each !ucket has s slots. 0sually s 1 /, which means that each !ucket holds e actly one record. We use the hash function f ) * to transform the identifiers into an address in the hash ta!le. Thus, f ) * maps the set of possi!le identifiers onto the integers , through !./. The identifier density of a hash ta!le is the ratio n2T, where n is the num!er of identifiers in the ta!le. The loading density or loading factor of a hash ta!le is a1n 2)s!*.
Definition#
Hashing 3unction#
A hash function, f, transforms an identifier, , into a !ucket address in the hash ta!le. We want to hash function that
is easy to compute and that minimi(es the num!er of collisions. We know that identifiers, whether they represent varia!le names in a program, word in a dictionary, or names in a telephone !ook, cluster around certain letters of the alpha!et. To avoid collisions, the hash function should depend on all the characters in an identifier. &t also should !e un!iased.
%id.square#
The middle of square hash function is frequently used in sym!ol ta!le application. We compute the function fm !y squaring the identifier and then using an appropriate num!er of !its from the middle of the square to o!tain the !ucket address. 'ince the middle !its of the square usually depend upon all the characters in an identifier, there is a high pro!a!ility that different identifiers will produce different hash addresses, even when some of the characters are the same.
Division#
This hash function is using the modulus )4* operator. We divide the identifier !y some num!er % and use the remainder as the hash address of . The hash function is# fD ) * 1 4 % This gives !ucket address that range from , to %./, where % 1 the ta!le si(e. The choice of % is critical.
3olding#
&n this method, we partition the identifier into several parts. All parts, e cept for the last one have the same length. We then add the parts together to o!tain the hash address for . There are two ways of carrying out this addition. This method is known as shift folding. The second method, know as folding at the !oundaries, reverses every other partition !efore adding.
Digit Analysis#
The last method we will e amine, digit analysis, is used with static files. A static file is one in which all the identifiers are known in advance. 0sing this method, we first transform the identifiers into num!ers using some radi , r. We then e amine the digits of each identifier, deleting those digits that have the most skewed distri!utions.
5verflow Handling #
There are two methods for detecting collisions and overflows in a static hash ta!le6 each method using different data structure to represent the hash ta!le.
Tow %ethods#
7inear 5pen Addressing )7inear pro!ing* 8haining
<efore inserting any elements into this ta!le, we must initiali(e the ta!le to represent the situation where all slots are empty. This allows us to detect overflows and collisions when we insert elements into the ta!le. The o!vious choice for an empty slot is the empty string since it will never !e a valid key in any application.
To insert a new element into the hash ta!le we convert the key field into a natural num!er, and then apply one of the hash functions discussed in Hashing 3unction. The function transform )!elow* uses this simplistic approach. To find the hash address of the transformed key, hash )!elow* uses the division method. short transform )char Dkey * ? short num!er 1 ,6 while )Dkey* num!er B1 DkeyBB6 return @ num!er6
short i, hash;value6 hash;value 1 hash )item.key*6 i 1 hash;value6 while )strlen )ht +i-.key* ? if )F strcmp )ht +i-.key, item.key* ? printf) GDuplicate entry FHnG*6 e it )/*6 @ i 1 )iB/* 4 TA<7E;'&=E6
8haining#
7inear pro!ing and its variations perform poorly !ecause inserting an identifier requires the comparison of identifiers with different hash values. 'ince we would not know the si(es of the lists in advance, we should maintain them as linked chains. 9define %A:;8HA$ /, 9define TA<7E;'&=E /> 9define &';3077 )ptr* )F)ptr** struct element ? char key+%A:;8HA$-6 @6 typedef struct list Dlis;pointer6 struct list ? element item6 list;pointer link6 @6 list;pointer hash;ta!le+TA<7E;'&=E-6 The function chain;insert )!elow* implements the chaining strategy. The function first computes the hash address for the identifier. &t then e amines the identifiers in the list for the selected !ucket. &f the identifier is found, we print an error message and e it.
&f the identifier is not in the list, we insert it at the end of the list. &f list was empty, we change the head node to point to the new entry.
short hash;value 1 hash )item.key*6 list;pointer ptr, trail 1 C077, lead 1 ht +hash;value-6 for) 6 lead6 trail 1 lead, lead 1 lead.Ilink* ? if )Fstrcmp)lead.Iitem.key, item.key** ? printf)GThe key is in the ta!le HnG*6 e it )/*6 @ @ ptr 1 new struct list6 if )&';3077 )ptr** ? printf) GThe memory is full HnG*6 e it )/*6 @ ptr.Iitem 1 item6 ptr.Ilink 1 C0776 if )trail* trail.Ilink 1 ptr6 else ht +hash;value- 1 ptr6 @ pu!lic static long hash)'tring name, int ta!le'i(e* ? 22 get !ottom J !its of first char
int tmp 1 name.charAt),* 4 KLM6 22 now multiply !y ne t two J.!it chars tmp 1 tmp D )name.charAt)/* 4 KLM*6 tmp 1 tmp D )name.charAt)K* 4 KLM*6 22 Cow return an inde within ta!leNs !ounds return )tmp 4 ta!le'i(e*6