Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 18

MODULE 2 Searching and Sorting Techniques

2.1 Searching Searching is the process of finding the location of a given element in a set of elements. The search is said to be successful if the given element is found i.e. the element does e!ist in the collection "such as an arra#$% other&ise it is unsuccessful. T&o simple approaches to searching are' a$ Linear search' This method traverses a list sequentiall# to locate the search (e#.

b$ )inar# search' This method &or(s on sorted lists b# progressivel# ma(ing better guesses to find the location of a search (e#. 2.2 Linear Search This method traverses a list sequentiall# to locate the search (e#. The algorithm that one chooses generall# depends on the organi*ation of the arra# elements. +f the elements are in random order then one should choose the linear search technique. 2.2 (a) Algorithm linearsearch "a n item loc$ ,ere -a. is an arra# of the si*e n. This algorithm finds the location of the element -item. in the arra# -a.. +f search item is found it sets loc to the inde! of the element% other&ise it sets loc to /0 )egin for i12 to "n/0$ b# 0 do if "a3i4 1 item$ then set loc1+ e!it endif endfor set loc /0 end 2.2 (b)C Implementation of the Algorithm int linearsearch "int a34 int n int (e#$ 5 int i% for"i12%i6n%i77$ 5

if"a3i411(e#$ return i% 8 return /0% 8

2.2 (c) Analysis of Linear Search +n the best possible case the item ma# be found at the first position. +n that case the search operation terminates in success &ith 9ust one comparison.The &orst case occurs &hen the item is present at the last position or it is missing from the arra#. +n the former case the search terminates in success &ith n comparisons. +n the latter case the search terminates in failure &ith n comparisons. Thus &e find that in the &orst case linear search carries out n operations.
:;<' Linear search in a sorted arra#. ;: =include 6stdio.h> =include 6conio.h> void main" $ 5 int arr3024 1 5 0 2 ? @ 00 0? 0A 2B BA @2 8 % int i num % clrscr" $ % printf " CEnter number to search' C $ % scanf " CDdC Enum $ %

for " i 1 2 % i 61 @ % i77 $ 5 if " arr3@4 6 num FF arr3i4 >1 num $ 5 if " arr3i4 11 num $ printf " CThe number is at position Dd in the arra#.C i $ % else printf " CGumber is not present in the arra#.C $ % brea( % 8 8 getch" $ % 8

2.3 Binary search This method &or(s on sorted lists b# progressivel# ma(ing better guesses to find the location of a search (e#. Illustration <onsider the follo&ing arra#' ? 02 0B 22 ?B H2 I2 Suppose &e &ant to search the element C0BC 0$ Je ta(e beg 1 2 end 1 I and compute the location of the middle element as

2$ Je then compare the search (e# &ith mid i.e. a3mid41a3?4 is not equal to 0B. Since beg6end &e start the ne!t iteration.

?$ Ks a3mid4122>0B therefore &e ta(e end 1 mid/0 1 ?/0 1 2 &hereas beg remains the same.. Thus H$Since a3mid4 i.e. a30410260B therefore &e ta(e beg1mid70107012 &hile end remains the same. B$ Go& beg1end. <ompute the mid element' Since a3mid4 i.e. a32410B the search terminates on success. 2.3 (a) Algorithm )inar#search"a n item loc$ )egin set beg12 set end1n/0 set mid1"beg7end$:2 &hile""beg61end$ and"a3mid4L1item$ do if"item6a3mid4$ then set end1mid/0 else set beg1mid70 endif set mid1"beg7end$:2 end&hile if"beg>end$ then set loc1/0 else set loc1mid endif end

2.3 (b) C Implementation int binar#search"int a34 int n int (e#$ 5 int beg end mid% beg12% end1n/0% mid1"beg7end$:2% &hile""beg61end$EE"a3mid4L1(e#$$ 5 if"(e#6a3mid4$ end1mid/0% else beg1mid70% mid1"beg7end$:2% 8 if"beg>end$ return /0% else return mid% 8

2.3 (c) ! C" Binary search in a sorte# array. ! =include 6stdio.h> =include 6conio.h> void main" $ 5 int arr3024 1 5 0 2 ? @ 00 0? 0A 2B BA @2 8 %

int mid lo&er 1 2 upper 1 @ num flag 1 0 % clrscr" $ % printf " CEnter number to search' C $ % scanf " CDdC Enum $ % for " mid 1 " lo&er 7 upper $ : 2 % lo&er 61 upper % mid 1 " lo&er 7 upper $ : 2 $ 5 if " arr3mid4 11 num $ 5 printf " CThe number is at position Dd in the arra#.C mid $ % flag 1 2 % brea( % 8 if " arr3mid4 > num $ upper 1 mid / 0 % else lo&er 1 mid 7 0 % 8 if " flag $ printf " CElement is not present in the arra#.C $ % getch" $ % 8 2.$ Sorting Sorting is the process of arranging elements in some logical order. Sorting methods are classified into the follo&ing categories'

%&ternal sorting" This deals &ith sorting of data stored in e!ternal files. This method is used &hen the volume of data is ver# large and cannot be held in a computerMs NKM. Internal sorting" This deals &ith sorting of data held in the NKM of a computer

2.$.1 Sorting 'etho#s The follo&ing are lin(s to tutorials on some of the most popular sorting methods'

)ubble sort )uc(et sort +nsertion sort Merge sort Ouic( sort Selection sort

2.$.1 (a)Bubble Sort


+t requires n/0 passes to sort an arra#. +n each pass ever# element a3i4 is compared &ith a3i704 for i12 to "n/(/0$ &here ( is the pass number and if the# are out of order i.e. if a3i4>a3i704 the# are s&apped. This &ill cause the largest element to move up or bubble up. Thus after the end of the first pass the largest element in the arra# &ill be placed in the last or nth position and on the ne!t pass the ne!t largest element &ill be placed at position "n/0$. This continues for each successive pass until the last or "n/0$th pass &hen the second smallest element &ill be placed at the second position.

(ass1. Step 0. if a324>a304 then s&ap a324 and a304. Step 2. if a304>a324 then s&ap a304 and a324. P Step n/0. if a3n/24>a3n/04 then s&ap a3n/24 and a3n/04.

(ass2. Step 0. if a324>a304 then s&ap a324 and a304. Step 2. if a304>a324 then s&ap a304 and a324. P Step n/2. if a3n/?4>a3n/24 then s&ap a3n/?4 and a3n/24. ) (ass *. Step 0. if a324>a304 then s&ap a324 and a304. Step 2. if a304>a324 then s&ap a304 and a324. P Step n/(. if a3n/"(70$4>a3n/(4 then s&ap a3n/"(70$4 and a3n/(4. ) (ass n+1 Step 0. if a324>a304 then s&ap a324 and a304. (A) Algorithm )ubblesort"a n$ )egin for (10 to "n/0$ b# 0 do for 912 to "n/(/0$ b# 0 do if"a394>a39704$ then set temp1394 set a3941a39704 set a3941temp endif endfor endfor end

(B) ! C" Bubble sort. ! =include 6stdio.h> =include 6conio.h> void main" $ 5 int arr3B4 1 5 2B 0A ?0 0? 2 8 % int i 9 temp % clrscr" $ % printf " C)ubble sort.QnC $ % printf " CQnKrra# before sorting'QnC$ % for " i 1 2 % i 61 H % i77 $ printf " CDdQtC arr3i4 $ % for " i 1 2 % i 61 ? % i77 $ 5 for " 9 1 2 % 9 61 ? / i % 977 $ 5 if " arr394 > arr39 7 04 $ 5 temp 1 arr394 % arr394 1 arr39 7 04 % arr39 7 04 1 temp % 888 printf " CQnQnKrra# after sorting'QnC$ % for " i 1 2 % i 61 H % i77 $ printf " CDdQtC arr3i4 $ % getch" $ % 8

2.$.2 'erge Sort

Merging means combining elements of t&o arra#s to form a ne& arra#. The simplest &a# of merging t&o arra#s is to first cop# all the elements of one arra# into a ne& arra# and then append all the elements of the second arra# to the ne& arra#. +f #ou &ant the resultant arra# to be sorted #ou can sort it b# an# of the sorting techniques. +f the arra#s are originall# in sorted order the# can be merged in such a &a# as to ensure that the combined arra# is also sorted. This technique is (no&n as merge sort. The technique of merge sort "i.e. sorting during merging$ is much more efficient than sorting after merging for arra#s that are alread# sorted. S,%( 1' Let us consider t&o arra#s sa# K3A4 and )3B4 to be merged to form a ne& arra#. The ne& arra# sa# < &ill be having A7B102 elements. S,%( 2' <ompare K324 and )324% if K3246)324 "sa#$% move K324 to <324. +ncrement the pointers of arra# K and arra# < "the pointer of that arra# is incremented &hose element is moved in the third arra#$. S,%( 3' Go& compare the elements of K and ) &here the pointers are pointing. That is compare K304 and )324. Suppose that &e find )3246K304 so &e move )324 to <304 and increment )Ms pointer to point to the ne!t element in arra# ).
2.$.2 (a) ! C" 'erge Sort. ! =include 6stdio.h> =include 6conio.h> void main" $ 5 int a3B4 1 5 00 2 @ 0? BA 8 % int b3B4 1 5 2B 0A 0 @2 ? 8 % int c3024 % int i 9 ( temp % clrscr" $ %

printf " CMerge sort.QnC $ % printf " CQnRirst arra#'QnC $ % for " i 1 2 % i 61 H % i77 $ printf " CDdQtC a3i4 $ % printf " CQnQnSecond arra#'QnC $ % for " i 1 2 % i 61 H % i77 $ printf " CDdQtC b3i4 $ % for " i 1 2 % i 61 ? % i77 $ 5 for " 9 1 i 7 0 % 9 61 H % 977 $ 5 if " a3i4 > a394 $ 5 temp 1 a3i4 % a3i4 1 a394 % a394 1 temp % 8 if " b3i4 > b394 $ 5 temp 1 b3i4 % b3i4 1 b394 % b394 1 temp % 8 8 8 for " i 1 9 1 ( 1 2 % i 61 @ % $ 5 if " a394 61 b3(4 $ c3i774 1 a39774 % else c3i774 1 b3(774 % if " 9 11 B FF ( 11 B $ brea( % 8

for " % 9 61 H % $ c3i774 1 a39774 % for " % ( 61 H % $ c3i774 1 b3(774 % printf " CQnQnKrra# after sorting'QnC$ % for " i 1 2 % i 61 @ % i77 $ printf " CDdQtC c3i4 $ % getch" $ % 8

2.$.3 Selection Sort Selection sort requires "n/0$ passes to sort an arra#. +n the first pass &e find the smallest element from a324 a304 a324 P a3n/04 and s&ap it &ith the first element i.e. a324. +n the second pass &e find the smallest element from a304 a324 a3?4P.a3n/04 and s&ap it &ith a304 and so on. (ass1. 0. Rind the location loc of the smallest element in the entire arra# i.e. a324 304 a324Pa3n/04. 2. +nterchange a324 E a3loc4. Then a324 is triviall# sorted. (ass2. 0. Rind the location loc of the smallest element in the entire arra# i.e. a304 a324Pa3n/04. 2. +nterchange a304 E a3loc4. Then a324 a304 are sorted. (ass *. 0. Rind the location loc of the smallest element in the entire arra# i.e. a3(4 a3(704 a3(724Pa3n/04.

2. +nterchange a3(4 E a3loc4. Then a324 a304 a324 Pa3(4 are sorted. (ass n+1. 0. Rind the location loc of the smaller of the elements a3n/24 a3n/04. 2. +nterchange a3n/24 E a3loc4. Then elements a324 a304 a324P.a3n/04 are sorted.

2.4.3 (a) Analysis of Selection Sort


The first pass requires n/0 comparisons to find the location of the smallest element. The second pass requires n/2 comparisons. The (th pass requires n/( comparisons. The last pass requires onl# one comparison.

Therefore the total number of comparisons are' R"n$1"n/0$7"n/2$7PP7"n/($7P?7270 1n"n/0$:2


2.$.3(b)Selection Sort Algorithm

Selectionsort"a n$ ,ere a is the linear arra# &ith n elements. This algorithm sorts elements into ascending order. +t uses a temporar# variable temp to facilitate the e!change of t&o values and variable i is used as a loop control variable
)egin for i10 to "n/0$ b# 0 do call Smallestelement"a n + loc$ set temp1a3i/04 set a3i/041a3loc4 set a3loc41temp endfor end

2.$.3 (c) ! C" Selection sort. !


=include 6stdio.h> =include 6conio.h> void main" $ 5 int arr3B4 1 5 2B 0A ?0 0? 2 8 % int i 9 temp % clrscr" $ % printf " CSelection sort.QnC $ % printf " CQnKrra# before sorting'QnC$ % for " i 1 2 % i 61 H % i77 $ printf " CDdQtC arr3i4 $ % for " i 1 2 % i 61 ? % i77 $ 5 for " 9 1 i 7 0 % 9 61 H % 977 $ 5 if " arr3i4 > arr394 $ 5 temp 1 arr3i4 % arr3i4 1 arr394 % arr394 1 temp % 888 printf " CQnQnKrra# after sorting'QnC$ % for " i 1 2 % i 61 H % i77 $ printf " CDdQtC arr3i4 $ % getch" $ % 8 2.$.$ ! C" -eap Sort. ! =include 6stdio.h>

=include 6conio.h> void ma(eheap " int 3 4 int $ % void heapsort " int 3 4 int $ % void main" $ 5 int arr3024 1 5 00 2 @ 0? BA 2B 0A 0 @2 ? 8 % int i % clrscr" $ % printf " C,eap Sort.QnC $ % ma(eheap " arr 02 $ % printf " CQn)efore Sorting'QnC $ % for " i 1 2 % i 61 @ % i77 $ printf " CDdQtC arr3i4 $ % heapsort " arr 02 $ % printf " CQnKfter Sorting'QnC $ % for " i 1 2 % i 61 @ % i77 $ printf " CDdQtC arr3i4 $ % getch" $% 8 void ma(eheap " int !3 4 int n $ 5 int i val s f % for " i 1 0 % i 6 n % i77 $ 5 val 1 !3i4 % s1i% f1"s/0$:2% &hile " s > 2 EE !3f4 6 val $ 5 !3s4 1 !3f4 % s1f%

f1"s/0$:2% 8 !3s4 1 val % 8 8 void heapsort " int !3 4 int n $ 5 int i s f ivalue % for " i 1 n / 0 % i > 2 % i// $ 5 ivalue 1 !3i4 % !3i4 1 !324 % f12% if " i 11 0 $ s 1 /0 % else s10% if " i > 2 EE !324 > !304 $ s12% &hile " s >1 2 EE ivalue 6 !3s4 $ 5 !3f4 1 !3s4 % f1s% s12;f70% if " s 7 0 61 i / 0 EE !3s4 6 !3s 7 04 $ s77 % if " s > i / 0 $ s 1 /0 % 8 !3f4 1 ivalue % 8 8

You might also like