Search This Blog

Monday, July 13, 2009

ALGORITHMS

Algorithm 2.2: (Quadratic Equation) This algorithm inputs the coefficients A, B, C of a quadratic equation and outputs the real solutions, if any.
Step 1: Read A, B, C.
Step 2: Set D = B2 – 4AC.
Step 3: If D>0, then
Set X1 = (-B+√D)/2A and X2 = (-B-√D)/2A.
Write X1, X2.
Else if D = 0, then
Set X = -B/2A.
Write ‘UNIQUE SOLUTIONS ’, X.
Else
Write ‘NO REAL SOLUTIONS’.
[End of if structure]
Step 4: Exit.

Algorithm2.3: (Largest element in Array) Given, a non empty array DATA with N numerical values, this algorithm finds the location LOC and the value MAX of the largest element of DATA.
Step 1: Set K = 1, LOC = 1 and MAX = DATA [1].
Step 2: Repeat Steps 3 and 4 while K ≤ N.
Step 3: If MAX < DATA [K], then
Set LOC = K and MAX = DATA [K].
[End of if structure]
Step 4: Set K = K+1.
[End of step 2 loop]
Step 5: Write LOC, MAX.
Step 6: Exit.

Algorithm 2.4: (Linear search) A linear array DATA with N elements and a specific ITEM of information are given. This algorithm finds the location LOC of ITEM in the array DATA or sets LOC = 0.
Step 1: Set K = 1, LOC = 0.
Step 2: Repeat Steps 3 and 4 while LOC = 0and K≤ N.
Step 3: If ITEM = DATA [K] then Set LOC = K
Step 4: Set K = K+1.
[End of step 2 loop]
Step 5: If LOC = 0 then
Write ITEM is not in the array DATA.
Else
Write LOC is the location of ITEM.
[End of if structure]
Step 6: Exit.

Algorithm 3.1: (Delete) A text T and a pattern P are in memory. This algorithm deletes every occurrence of P in T.
Step 1: Set K = INDEX (T, P).
Step 2: Repeat while K ≠ 0
Set T = DELETE(T, INDEX(T,P),LENGTH(P))
Set K = INDEX (T, P).
Step 3: Write T.
Step 4: Exit.

Algorithm 3.2: (Replacement) A text T, pattern P and Q are in memory. This algorithm replaces every occurrence of P in T by Q.
Step 1: Set K = Index (T, P)
Step 2: Repeat while K ≠ 0.
Set T = REPLACE (T, P, Q).
Set K = INDEX (T, P).
[End of loop]
Step 3: Write T.
Step 4: Exit.

Algorithm 3.3: (Pattern matching) P and T are strings with length R and S, respectively, and are stored as arrays with one character per element. This algorithm finds the INDEX of P in T.
Step 1: Set K = 1 and MAX = S – R + 1.
Step 2: Repeat steps 3 to 5 while K ≤ MAX.
Step 3: Repeat for L = 1 to R.
If P[L] ≠ T[K + L – 1],
Then go to step 5.
[End of inner loop]
Step 4: Set INDEX = K and Exit.
Step 5: Set K = K + 1.
[End of step 2 outer loop]
Step 6: Set INDEX = 0.
Step 7: Exit.

No comments:

Post a Comment