Problem Solving Techniques – 2nd PUC

Unit 2. Problem Solving Techniques Unit 2.1. Search, Sort, to find max, min An example array,

A[0] A[1] A[2] .

.

.

A[n-1]

SEARCHING: The process of finding the location of an element in the given array is called Searching. The different types of searching techniques are, a. Linear Search b. Binary Search

Linear Search The simplest of all forms of searching is the linear search. Linear Search is also known as sequential search. It compares every element of an array one at a time in sequence until a match is found. If the search element is found in the array, its location is returned. If the search element is not found, search is said to be unsuccessful. Advantage: A sorted as well as an unsorted can be searched using Linear Search technique. Disadvantage: It is time consuming. Linear Search-Algorithm:

Step Step Step Step Step

1 2 3 4 5

Step Step Step Step

6 7 8 9

Step 10

Nawab Pasha

Loc=-1 For I=0 to n-1 do If (ele=A[I] Then LOC=I Goto Step 6 [End If] [End of for loop] If (LOC>=0) then Print ele, “Found in location”, LOC Else Print ele, “Not found” [End If] Exit

www.npscience.com

1

Problem Solving Techniques – 2nd PUC E.g. 1: To search the element 6 in the array 54

34

5

6

98

34 A[1]

5 A[2]

23

Solution: Let the array be AÆ 54 A[0]

Then, the search element, ele=6, n=6 I 0 to n-1 A[I] (0 to 5) 54 0 34 1 5 2 3

6 A[3]

Compare ele=6 with A[I] Not equal Not equal Not equal

6

98 A[4]

23 A[5]

LOC Initially -1

Result

-1 -1 -1 3

Element found in location 2

LOC Initially -1

Result

equal Exit

E.g. 2: To search the element 25 in the array 23

5

67

Solution: Let the array be AÆ 23 A[0]

5 A[1]

67 A[2]

Then, the search element, ele=25, n=3 I 0 to n-1 (0 to 2) 0 1 2

23 5

Compare ele=25 with A[I] Not equal Not equal

67

Not equal

A[I]

-1 -1 -1

Element not found

Exit

Binary Search: Binary Search is a technique for finding a particular value in a sorted list. The steps involved in binary search are 1. Find the mid element of the array by adding the lower and upper bounds and dividing the sum by 2. 2. Compare the search element with the mid element of the array

Nawab Pasha

www.npscience.com

2

Problem Solving Techniques – 2nd PUC a. If the given element is lesser than the mid then the search element lies in the left half of the array. Hence repeat the steps 1 and 2 in the left half. b. If the given element is greater than the mid then the search element lies in the right half of the array. Hence repeat the steps 1 and 2 in the right half. c. If the search element is equal to the mid, then the search is successful. 3. Repeat the above steps until the element in search is found or until there is no portion of the array left un searched. Advantages: 1. It is Superior than the Linear Search. 2. Number of comparisons may be less in the worst case.

Disadvantages: Only a sorted array can be searched. Hence any given array needs first to be sorted.

Binary Search – Algorithm:

Step Step Step Step Step Step Step

1 2 3 4 5 6 7

Step Step Step Step

8 9 10 11

Step Step Step Step

12 13 14 15

Step 16

Nawab Pasha

LOW=0 HIGH=n-1 LOC=-1 While (LOW<=HIGH) DO MID=(LOW+HIGH)/2 If (ele=A[MID]) Then LOC=MID GO TO step 12 If (ele<A[MID]) Then HIGH=MID-1 Else LOW=MID+1 [End If] [End of While loop] If (LOC>=0) Then Print ele, “Found in location”, LOC Else Print ele, “Not found” [End If] Exit

www.npscience.com

3

Problem Solving Techniques – 2nd PUC E.g. 1: To search the element 6 in the array 2

6

15

27

45

67

Solution: Let the array be AÆ 2 A[0]

6 A[1]

15 A[2]

27 A[3]

45 A[4]

67 A[5]

Then, the search element, ele=6, n=6

LOW

HIGH

0 0

5 MID-1=1

MID+1=1

Compare ele=6 MID A[MID] with A[MID] (0+5)/2=2 A[2]=15 6< A[2] (0+1)/2=0 A[0]=2 6> A[0]

1

(1+1)/2=1

A[1]=6

LOC (Initially -1)

Result

-1 -1 1

Element found in location 1

LOC (Initially -1)

Result

6= A[1]

Exit

E.g. 2: To search the element 6 in the array 2

7

15

27

45

67

2 A[0]

7 A[1]

15 A[2]

27 A[3]

45 A[4]

67 A[5]

Solution: Let the array be AÆ

Then, the search element, ele=7, n=6

LOW

HIGH

0 0 MID+1=1

5 MID-1=1 1

1

MID-1=0

Compare ele=6 MID A[MID] with A[MID] (0+5)/2=2 A[2]=15 6<A[2] (0+1)/2=0 A[0]=2 6>A[0] (1+1)/2=1 A[1]=7 6<A[1]

-1 -1 -1 Element not found

Exit since LOW is greater than HIGH Nawab Pasha

www.npscience.com

4

Problem Solving Techniques – 2nd PUC

Sorting The rearrangement of the array elements in an ascending or descending order is called sorting. Objectives: 1. Complexity of the algorithm must be in proportion to the level of sorting 2. The movement of data between secondary storage and the main memory should be as least as possible. Or must be in the form of large blocks. 3. It is better to retain the data in the main memory for the easy access.

The different techniques of sorting are, 1. Bubble Sort 2. Selection Sort and 3. Insertion Sort

1. Bubble Sort: Bubble sort is also known as “Exchange Sort”. Like air bubbles moving to the surface of a liquid, the larger elements move to the higher positions of the array during Bubble Sort. This process involves the exchange of larger elements in the lower positions of the array with the smaller elements in the higher positions of the array, hence this technique is also known as Exchange Sort. Bubble Sort – Algorithm:

Step 1 Step 2 Step 3 Step 4 Step 5 Step 6

Step 7

Nawab Pasha

For I=1 to n-1 Do For J=0 to n-I-1 Do [Compare adjacent elements] If (A[J]>A[J+1]) Then [Exchange the values] Temp=A[J] A[J]=A[J+1] A[J+1]=Temp [End If] [End of Step 2 For loop] [End of Step 1 For loop] Exit

www.npscience.com

5

Problem Solving Techniques â€“ 2nd PUC

Example:1 Sort the given array using bubble sort technique.

23

3

4

36 31

Solution: I 1 to N-1

J 0 to NI-1 0 1

1 2 3

0 2

1 2

0 3 1

4

0

Nawab Pasha

Compare A[I] & A[I+1]

A[0] & A[1] i.e., 23 & 3 A[1] & A[2] i.e., 23 & 4 A[2] & A[3] i.e., 23 & 36 A[3] & A[4] i.e., 36 & 31 A[0] & A[1] i.e., 3&4 A[1] & A[2] i.e., 4 & 23 A[2] & A[3] i.e., 23 & 31 A[0] & A[1] i.e., 3&4 A[1] & A[2] i.e., 4 & 23 A[0] & A[1] i.e., 3&4

Result

To do

Resulting Array

Pass

>

interchange

3

23

4

36

31

>

interchange

3

4

23

36

31

<

donot interchange

3

4

23

36

31

>

interchange

3

4

23

31

36

<

donot interchange

3

4

23

31

36

<

donot interchange

3

4

23

31

36

<

donot interchange

3

4

23

31

36

<

donot interchange

3

4

23

31

36

>

interchange

3

4

23

31

36

<

donot interchange

3

4

23

31

36

I

II

III

www.npscience.com

IV

6

Problem Solving Techniques – 2nd PUC

Example:2 Sort the given array using bubble sort technique. 2

56 23

1

15

Solution: I 1 to N-1

J 0 to NI-1

A[0] & A[1] i.e., 2 & 56 A[1] & A[2] i.e., 56 & 23 A[2] & A[3] i.e., 56 & 1 A[3] & A[4] i.e., 56 & 15

0 1 1 2 3

A[0] & A[1] i.e., 2 & 23 A[1] & A[2] i.e., 23 & 1 A[2] & A[3] i.e., 23 & 15

0 2

1 2

A[0] & A[1] i.e., 2&1 A[1] & A[2] i.e., 2 & 15

0 3 1

4

Compare A[I] & A[I+1]

A[0] & A[1] i.e., 1&2

0

Result

To do

Resulting Array

Pass

<

donot interchange

2

56

23

1

15

>

interchange

2

23

56

1

15

>

interchange

2

23

1

56

15

>

interchange

2

23

1

15

56

<

donot interchange

2

23

1

15

56

>

interchange

2

1

23

15

56

>

interchange

2

1

15

23

56

>

interchange

1

2

15

23

56

>

interchange

1

2

15

23

56

<

donot interchange

1

2

15

23

56

I

II

III

IV

ANALYSIS OF BUBBLE SORT:

The total number of comparisons is given the sum of the comparisons in each pass. Total Number of Number of Number of Number of number of = comparisons + comparisons + comparisons + …….. + comparisons comparisons in Pass 1 in Pass 2 in Pass 3 in Pass n =

Nawab Pasha

n-1

+

n-2

+

n-3

www.npscience.com

+ …….. +

1

7

Problem Solving Techniques – 2nd PUC =

n×

=

(n − 1) 2

Ο( n 2 )

2. Selection Sort: This sorting technique involves the insertion of an element in to a sorted array in such a way that the resulting array is also sorted. In this we assume that the first element of an array is in its proper position. Then we take the next element from the unordered array and place it in its proper position, by comparing with one already sorted. This process is continued until the elements are placed into their proper positions in the array.

Algorithm for selection sort:

Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7

Step 8 Step 9 Step 10

Nawab Pasha

For I=0 to n-2 Do [Assume Ith element as smallest] Small=A[I] POS=I [Find the smallest element in the array and its position] For J=I+1 to n-1 Do If (A[J]<small) Then Small=A[J] POS=J [End If] [End of Step 4 For loop] [Exchange Ith element with smallest element A[POS]=A[I] A[I]=small [End of Step 1 For loop] Exit

www.npscience.com

8

Problem Solving Techniques â€“ 2nd PUC

Example:1 Sort the given array using selection sort technique. 34 23

I 0 to N-2

3

J I+1 to N1

1 2 0 3 4

2 1

3 4

3 2 4

Nawab Pasha

4

67

Compare A[J] & small

A[1] & small i.e., 23 & 34 A[2] & small i.e., 3 & 23 A[3] & small i.e., 4 & 3 A[4] & small i.e., 67 & 3

A[2] & small i.e., 34 & 23 A[3] & small i.e., 4 & 23 A[4] & small i.e., 67 & 4

A[3] & small i.e., 23 & 34 A[4] & small i.e., 67 & 23

A[0]

A[1]

A[2]

A[3]

A[4]

34

23

3

4

67

Pos initially I

Small initially A[I]

0

34

<

1

23

<

2

3

>

2

3

>

2

3

3

23

34

4

67

1

23

3

23

34

4

67

>

1

23

<

3

4

>

3

4

3

4

34

23

67

2

34

3

4

34

23

67

<

3

23

Interchange A[Pos] and A[I]

>

3

23

3

4

23

34

67

3

34

3

4

23

34

67

Result

Resulting Array

34

23

3

4

Pass

67

Interchange A[Pos] and A[I] I

Interchange A[Pos] and A[I] II

III

www.npscience.com

9

Problem Solving Techniques â€“ 2nd PUC Interchange A[Pos] and A[I] 3

A[4] & small i.e., 67 & 34

4

>

3

34

3

4

23

34

67

A[0]

A[1]

A[2]

A[3]

A[4]

4

56

2

3

76

IV

Example:2 Sort the given array using selection sort technique. 4

I 0 to N-2

56

2

J I+1 to N1

1 2 0 3 4

2 1

3 4

3 2 4

Nawab Pasha

3

76

Compare A[J] & small

A[1] & small i.e., 56 & 4 A[2] & small i.e., 2 & 4 A[3] & small i.e., 3 & 2 A[4] & small i.e., 76 & 2

A[2] & small i.e., 4 & 56 A[3] & small i.e., 3 & 4 A[4] & small i.e., 76 & 3

A[3] & small i.e., 56 & 4 A[4] & small i.e., 76 & 4

Pos initially I

Small initially A[I]

0

4

>

0

4

<

2

2

>

2

2

>

2

2

2

56

4

3

76

1

56

2

56

4

3

76

<

2

4

<

3

3

>

3

3

2

3

4

56

76

2

4

2

3

4

56

76

2

4

Interchange A[Pos] and A[I]

Result

Resulting Array

4

56

2

3

Pass

76

Interchange A[Pos] and A[I] I

Interchange A[Pos] and A[I]

>

II

III >

2

4

2

www.npscience.com

3

4

56

76

10

Problem Solving Techniques – 2nd PUC 3

56

2

3

4

56

76

Interchange A[Pos] and A[I] 3

4

A[4] & small i.e., 76 & 56

>

3

56

2

3

4

56

76

IV

ANALYSIS OF SELECTION SORT:

The total number of comparisons is given the sum of the comparisons in each pass. Total Number of Number of Number of Number of number of = comparisons + comparisons + comparisons + …….. + comparisons comparisons in Pass 1 in Pass 2 in Pass 3 in Pass n = =

n-1 n×

=

+

n-2

+

n-3

+ …….. +

1

(n − 1) 2

Ο( n 2 )

3. Insertion Sort: In this technique we assume that the first element of an array is in its proper position. Then we take the next element from the unordered array and place it in its proper position, by comparing with one already sorted. This process is continued until the elements are placed into their proper positions in the array.

Insertion Sort – Algorithm:Step Step Step Step Step Step Step

1 2 3 4 5 6 7

Step 8

Nawab Pasha

For I=0 to n-1 Do J=1 While (J>=1) If (A[J]<A[J-1]) then Temp=A[J] A[J]=A[J-1] A[J-1]=Temp [End If] J=J-1 [End of While loop] [End of Step 1 For loop]

www.npscience.com

11

Problem Solving Techniques – 2nd PUC Step 9

Exit

Refer the class notes for examples. ANALYSIS OF INSERTION SORT:

The total number of comparisons is given the sum of the comparisons in each pass. Total Number of Number of Number of Number of number of = comparisons + comparisons + comparisons + …….. + comparisons comparisons in Pass 1 in Pass 2 in Pass 3 in Pass n = =

=

1

n×

+

2

+

3

+ …….. +

n-1

(n − 1) 2

Ο( n 2 )

FINDING THE MAXIMUM IN AN ARRAY

Algorithm Description Assume that the first element in list is the largest. Compare with the remaining elements one at a time if an element which is larger than the first element is found make a note of the element and its position. Repeat the same step to finally output the largest element and its position

Algorithm LARGE = A [0] POS = 0 FOR I = 1 TO N-1 DO IF ( A[I] > LARGE) THEN LARGE = A [I] POS = I END IF END FOR PRINT “Largest =”, LARGE, “Position =”, POS EXIT

Refer the class notes for examples. FINDING THE MAXIMUM IN AN ARRAY

Algorithm Description

Nawab Pasha

www.npscience.com

12

Problem Solving Techniques – 2nd PUC Assume that the first element in list is the smallest. Compare ir with the remaining elements one at a time if an element, which is smaller than the first element is found make a note of the element and its position. Repeat the same step to finally output the smallest element and its position

Algorithm Algorithm SMALL = A[0] POS = 0 FOR I = 1 TO N-1 DO IF( A[I] < SMALL) THEN SMALL = A[I] POS = I END IF END FOR PRINT “Smallest =”, SMALL, “Position =”, POS EXIT

Refer the class notes for examples.

Structured Programming: Structured Programming deals only with logic and code. Structured programming suggests that well structured or organized programs can be written using various programming constructs such as, 1. Sequence: In this type of construct the statements are executed in sequence ( i.e one after the other) 2. Selection: This construct is used in a program whenever there is a need to make decisions. 3. Iteration: This type of construct is used if certain sequence of statements have to be replaced till some condition is satisfied. 4. Modularity: It is a technique where a given problem is divided into a number of self contained independent program segments. Each segment is called a module and it can be called in another program or another module.

Nawab Pasha

www.npscience.com

13

Problem Solving Techniques â€“ 2nd PUC Start

Sequence (Single entry single exit)

Selection (Single entry but two exits)

Iteration (Repetition)

Stop

TOP DOWN ANALYSIS The approach of dividing a problem into sub-problems and then dividing the sub-problems further they can be implemented for a Computer solution is called Top-Down Design. The top-down design is an outline of a solution. The solution should be translated to a computer language. The problem is easily understood and logically organized into parts called modules. Modification or changes can be easily made to the modules. Step-wise refinement can be done to achieve the efficiency. (Step-wise refinement is a process of breaking down the problem at each stage and incorporating the necessary changes). Advantages of Top Down Approach Understandability: Modules are organized in the sequence of their execution of the program, this increases readability of the program and helps in debugging to a large extent. Clear Identification of tasks: Each module created should perform a single task and the name of the module should convey the purpose of the module. Program Maintenance:

Nawab Pasha

www.npscience.com

14

Problem Solving Techniques â€“ 2nd PUC Solution should be organized in such a way that adding new functionalities should not lead any large changes to the original solution. Eliminates duplication in coding: As a task is coded as a module, which is an independent program it can be repeated any number of times in the program. This eliminates the need to repeat the same code in a number of places in the program. Code reusability: Module which have been coded and work properly in one program can used in other programs.

Myself: Nawab Pasha Vice-Principal Al-Ameen PU College, Bangalore. email: nawabpasha@npscience.com np@npscience.com nawabpasha_nak@yahoo.co.in For more logon to:

www.npscience.com To clarify any doubts, to make friends, to discuss about any subject, visit: forum.npscience.com

Nawab Pasha

www.npscience.com

15

Problem Solving Techniques

Published on Jul 14, 2011

This is a study material in brief with some examples for the students of 10+2 in computer science category

Advertisement