Algorithms
Algorithms
STEP 1: START
STEP 2: SET lines= 10
STEP 3: SET space= (lines*2)-2
STEP 4: SET i=1 REPEAT STEP 5 to STEP 18 UNTIL i <= (lines/2)
STEP 5: SET flagl=0.
STEP 6: SET l=1. REPEAT STEP 7 and 8 UNTIL l <= i.
STEP 7: IF flagl is not true PRINT '*' and INCREMENT flagl by 1
ELSE PRINT "" WITH "*"
STEP 8: SET l=l+1
STEP 9: SET l=1
STEP 10: REPEAT STEP 11 UNTIL l <= space
STEP 11: PRINT "" and SET l=l+1
STEP 12: DECREMENT space by 4.
STEP 13: SET flagr=0
STEP 14: SET l=1. REPEAT STEP 15 to STEP 16 UNTIL l <= i
STEP 15: IF flag is not true PRINT * and increment flag by 1.
ELSE PRINT BLANK SPACE " " WITH * .
STEP 16: SET l=l+1
STEP 17: PRINT new line.
STEP 18: SET i=i+1.
STEP 19: INCREMENT space by 4
STEP 20: SET i=lines/2
STEP 21: REPEAT STEP 22 to STEP 35 UNTIL i >=1
STEP 22: SET flagl=0
STEP 23: SET l=1. REPEAT STEP 24 and 25 UNTIL l <= i
STEP 24: IF flag is not true PRINT * and INCREMENT flag by 1 ELSE PRINT "" + *
STEP 25: SET l=l+1
STEP 26: SET l=1. REPEAT STEP 27 and 28 UNTIL l <= space
STEP 27: PRINT ""
STEP 28: SET l = l + 1
STEP 29: INCREMENT space by 4.
STEP 30: SET flagr =0.
STEP 31: SET l=1. REPEAT STEP 32 to STEP 33 UNTIL l<=i.
STEP 32: IF flagr is not true then PRINT * and INCREMENT flagr by 1
else
PRINT "" with *
STEP 33: SET l=l+1
STEP 34: PRINT new line.
STEP 35: SET i= i-1.
STEP 36: END
STEP 1: START
STEP 2: DEFINE string str = "acbdfghybdf"
STEP 3: SET String lrs = " "
STEP 4: CALCULATE length.
STEP 5: SET i =0. REPEAT STEP 6 to STEP 10 UNTIL i<n.
STEP 6: SET j =i+1. REPEAT STEP 7 to STEP 9 UNTIL j<n.
STEP 7: CALL lcp() in String x.
STEP 8: if(x.length()>lrs.length()) then lrs =x
STEP 9: j =j + 1
STEP 10: i =i +1
STEP 11: PRINT lrs.
STEP 12: END
lcp(String s, String t)
STEP 1: START
STEP 2: SET n = Math.min(s.length(), t.length())
STEP 3: SET i =0. REPEAT STEP 4 to STEP 5 UNTIL i<n
STEP 4: if(s.charAt(i) != t.charAt(i)) then RETURN s.substring(0, i)
STEP 5: i= i+1
STEP 6: RETURN s.substring(0,n)
STEP 7: END
STEP 1: START
STEP 2: DEFINE string str = "ABC".
STEP 3: len = str.length().
STEP 4: PRINT "All the permutations of the string are:"
STEP 5:CALL generatePermutation(str, 0, len).
STEP 6: END
STEP 1: START
STEP 2: if(start==end-1)
PRINT str
else go to STEP 3
STEP 3: SET i = start. REPEAT STEP 4 to STEP 7 UNTIL i<end.
STEP 4: str = swapstring(str, start, i).
STEP 5: generatePermutation(str, start + 1, end).
STEP 6: str = swapstring(str, start, i).
STEP 7: i=i+1
STEP 8: END
STEP 1: START
STEP 2: char[] b = a.toCharArray()
STEP 3: DEFINE char ch
STEP 4: ch =b[i]
STEP 5: b[i] = b[j]
STEP 6: b[j] = ch
STEP 7: RETURN String.ValueOf(b)
STEP 8: END
Algorithm to find the number of the words in the given text file
STEP 1: START
STEP 2: DEFINE String line
STEP 3: SET count =0
STEP 4: USE File Reader to open file in read mode.
STEP 5: READ line from file
STEP 6: REPEAT STEP 7 to STEP 8 UNTIL reach the end of file
STEP 7: SPLIT lines into words and STORE in array string words[].
STEP 8: count = count + words.length
STEP 9: PRINT count.
STEP 10: END
STEP 1: START
STEP 2: DEFINE String string = "Wow you own kayak "
STEP 3: DEFINE word = " ", smallPalin = " ", bigPalin = " "
STEP 4: DEFINE String words[]
STEP 5: SET temp = 0, count = 0
STEP 6: CONVERT the string into lowercase
STEP 7: string = string + " "
STEP 8: SET i=0. REPEAT STEP 9 to STEP 11 UNTIL i<string.length()
STEP 9: SPLIT the string into words.
STEP 10: IF(string.charAt(i) != ' ') then
word = word + string.charAt(i)
else
words[temp]= word
temp = temp+1
word = " "
STEP 11: i=i+1
STEP 12: SET i=0. REPEAT STEP 13 to STEP 17 UNTIL i<temp
STEP 13: IF( isPalindrome(words[i]) ) then
count = count + 1
goto STEP 14
STEP 14: IF(count==1)
STEP 15: IF length of smallPalin is greater than the length of words[i] then
smallPalin = words[i]
STEP 16: IF length of bigPalin is lesser than the length of words[i] then
bigPalin = words[i]
STEP 17: i=i+1
STEP 18: IF(count==0) then PRINT "No palindrome is present in the given string "
else
PRINT smallPalin, bigPalin
STEP 19: END
isPalindrome(String a)
STEP 1: START STEP 2: SET flag = true STEP 3: SET i=0. REPEAT STEP 4 to STEP 5 UNTIL
i<a.length()/2 STEP 4: IF(a.charAt(i) != a.charAt(a.length()-i-1) then
flag = false
break STEP 5: i=i+1 STEP 6: RETURN flag STEP 7: END
Algorithm for Linear Search in Java
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
Pseudocode
procedure linear_search (list, value)
end procedure
Algorithm
begin BubbleSort(list)
return list
end BubbleSort
Pseudocode
Pseudocode of BubbleSort algorithm can be written as follows −
loop = list.count;
end for
end for
Selection Sort
Algorithm
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
Pseudocode
procedure selection sort
list : array of items
n : size of list
for i = 1 to n - 1
/* set current element as minimum*/
min = i
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
end procedure
Insertion Sort
INSERTION-SORT (A)
1 for j <- 2 to length[A]
2 do key <- A[j]
3 Insert A[j] into the sorted sequence A[1 . . j - 1].
4 i <- j - 1
5 while i > 0 and A[i] > key
6 do A[i + 1] <- A[i]
7 i <- i - 1
8 A[i + 1] <- key
2. Loop over the input until it is empty, "removing" the first remaining (leftmost) element.
3. Compare the removed element against the current result, starting from the highest
(rightmost) element, and working left towards the lowest element.
4. If the removed input element is lower than the current result element, copy that value into
the following element to make room for the new element below, and repeat with the next
lowest result element.
5. Otherwise, the new element is in the correct location; save it in the cell left by copying
the last examined result up, and start again from (2) with the next input element.