0% found this document useful (0 votes)
59 views

Algorithms

This document contains summaries of various algorithms related to arrays, strings, matrices, and sorting. It includes algorithms to copy an array, find frequencies in an array, rotate arrays, find duplicates, print elements, find min/max, sort arrays, perform matrix operations, analyze strings, and search/sort arrays. Pseudocode is provided for many of the algorithms.

Uploaded by

aryan mehta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Algorithms

This document contains summaries of various algorithms related to arrays, strings, matrices, and sorting. It includes algorithms to copy an array, find frequencies in an array, rotate arrays, find duplicates, print elements, find min/max, sort arrays, perform matrix operations, analyze strings, and search/sort arrays. Pseudocode is provided for many of the algorithms.

Uploaded by

aryan mehta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Table of Contents

Palindrome number algorithm.................................................................................................................1


Algorithm to copy all elements of one array into another array.................................................................1
Algorithm to find the frequency of each element in the array..................................................................1
Algorithm to left rotate the elements of an array.....................................................................................2
Algorithm to print the duplicate elements of an array..............................................................................2
Algorithm to print the elements of an array.............................................................................................2
Algorithm to print the elements of an array in reverse order...................................................................3
Algorithm to print the elements of an array present on even position......................................................3
Algorithm to print the elements of an array present on odd position.......................................................3
Algorithm to print the largest element in an array...................................................................................3
Algorithm to print the smallest element in an array.................................................................................4
Algorithm to print the number of elements present in an array................................................................4
Algorithm to print the sum of all the items of the array..........................................................................4
Algorithm to right rotate the elements of an array...................................................................................4
Algorithm to sort the elements of an array in ascending order...................................................................5
Algorithm to sort the elements of an array in descending order................................................................5
Algorithm to subtract the two matrices......................................................................................................6
Algorithm to determine whether two matrices are equal...........................................................................6
Algorithm to display the lower triangular matrix........................................................................................7
Algorithm to display the upper triangular matrix........................................................................................7
Algorithm to find the frequency of odd & even numbers in the given matrix.............................................8
Algorithm to find the product of two matrices............................................................................................8
Algorithm to find the sum of each row and each column of a matrix.........................................................9
Algorithm to find the transpose of a given matrix.......................................................................................9
Algorithm to count the total number of characters in a string..................................................................10
Algorithm to draw a pattern......................................................................................................................10
......................................................................................................................10
Algorithm to count the total number of vowels and consonants in a string.............................................11
Algorithm to determine whether two strings are the anagram................................................................12
Algorithm to divide a string in 'N' equal parts...........................................................................................12
Algorithm to find all subsets of a string.....................................................................................................13
Algorithm to find the longest repeating sequence in a string....................................................................13
Algorithm to find all the permutations of a string.....................................................................................14
Algorithm to remove all the white spaces from a string............................................................................14
Algorithm to replace lower-case characters with upper-case and vice-versa...........................................15
Algorithm to replace the spaces of a string with a specific character........................................................15
Algorithm to determine whether a given string is palindrome..................................................................15
Algorithm to determine whether one string is a rotation of another........................................................16
Algorithm to find maximum and minimum occurring character in a string...............................................16
Algorithm to find Reverse of the string.....................................................................................................16
Algorithm to find the duplicate characters in a string...............................................................................17
Algorithm to find the duplicate words in a string......................................................................................17
Algorithm to find the frequency of characters..........................................................................................18
Algorithm to find the largest and smallest word in a string.......................................................................18
Algorithm to find the most repeated word in a text file............................................................................19
Algorithm to find the number of the words in the given text file..............................................................19
Algorithm to separate the Individual Characters from a String.................................................................20
Algorithm to swap two string variables without using third or temp variable..........................................20
Algorithm to print smallest and biggest possible palindrome word in a given string................................20
Algorithm for Linear Search in Java...........................................................................................................21
Pseudocode...........................................................................................................................................21
Binary Search in Java.................................................................................................................................22
BINARY_SEARCH(A, lower_bound, upper_bound, VAL)........................................................................22
Bubble Sort................................................................................................................................................22
Algorithm...............................................................................................................................................22
Pseudocode...........................................................................................................................................22
Selection Sort............................................................................................................................................23
Algorithm...........................................................................................................................................23
Pseudocode.......................................................................................................................................23
Insertion Sort.............................................................................................................................................24

Palindrome number algorithm


 Get the number to check for palindrome
 Hold the number in temporary variable
 Reverse the number
 Compare the temporary number with reversed number
 If both numbers are same, print "palindrome number"
 Else print "not palindrome number"

Algorithm to copy all elements of one array into another array


 STEP 1: START
 STEP 2: INITIALIZE arr1[] ={1, 2, 3, 4, 5}
 STEP 3: CREATE arr2[] of size arr1[].
 STEP 4: COPY elements of arr1[] to arr2[]
 STEP 5: REPEAT STEP 6 UNTIL (i<arr1.length)
 STEP 6: arr2[i] =arr1[i]
 STEP 7: DISPLAY elements of arr1[].
 STEP 8: REPEAT STEP 9 UNTIL (i<arr1.length)
 STEP 9: PRINT arr1[i]
 STEP 10: DISPLAY elements of arr2[].
 STEP 11: REPEAT STEP 12 UNTIL (i<arr2.length)
 STEP 12: PRINT arr2[i].
 STEP 13: END

Algorithm to find the frequency of each element in the array


 STEP 1: START
 STEP 2: INITIALIZE arr[] ={1, 2, 8, 3, 2, 2, 2, 5, 1 }.
 STEP 3: CREATE fr[] of arr[] length.
 STEP 4: SET visited = -1.
 STEP 5: REPEAT STEP 6 to STEP 9 for(i=0;i<arr.length;i++)
 STEP 6: SET count = 1
 STEP 7: REPEAT STEP 8 for(j=i+1;j<arr.length;j++)
 STEP 8: if(arr[i]==arr[j]) then
            count++
            fr[j] =visited
 STEP 9: if(fr[i]!=visited) then
            fr[i]=count
 STEP 10: PRINT "------------"
 STEP 11: PRINT "Element | Frequency"
 STEP 12: PRINT "-------------"
 STEP 13: REPEAT STEP 14 for(i=0;i<fr.length;i++)
 STEP 14: if(fr[i]!=visited) then
            PRINT arr[i] and fr[i]
 STEP 15: PRINT "-------------"
 STEP 16: END

Algorithm to left rotate the elements of an array


 STEP 1: START
 STEP 2: INITIALIZE arr[] ={1, 2, 3, 4, 5 }.
 STEP 3: SET n =3
 STEP 4: PRINT "Original Array"
 STEP 5: REPEAT STEP 6 for(i=0; i<arr.length; i++)
 STEP 6: PRINT arr[i]
 STEP 7: REPEAT STEP 8 to STEP 12 for(i=0; i<n; i++ )
 STEP 8: DEFINE j, first.
 STEP 9: first = arr[0]
 STEP 10: REPEAT STEP 11 for(j= 0; j<arr.length-1; j++)
 STEP 11: arr[j]= arr[j+1]
 STEP 12: arr[j]= first
 STEP 13: PRINT "Array after left rotation"
 STEP 14: REPEAT STEP 15 for(i=0; i<arr.length; i++)
 STEP 15: PRINT arr[i]
 STEP 16: END

Algorithm to print the duplicate elements of an array


 STEP 1: START
 STEP 2: INITIALIZE arr[]= {1, 2, 3, 4, 2, 7, 8, 8, 3}.
 STEP 3: PRINT "Duplicate elements in given array:"
 STEP 4: REPEAT STEP 5 to STEP 7 for(i=0; i<arr.length; i++)
 STEP 5: REPEAT STEP 6 and STEP 7 for(j=i+1; j<arr.length; j++)
 STEP 6: if(arr[i] == arr[j])
 STEP 7: PRINT arr[j]
 STEP 8: END

Algorithm to print the elements of an array


 STEP 1: START
 STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}.
 STEP 3: PRINT "Elements of given array:"
 STEP 4: REPEAT STEP 5 for(i=0; i<arr.length; i++)
 STEP 5: PRINT arr[i]
 STEP 6: END

Algorithm to print the elements of an array in reverse order


 STEP 1: START
 STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}
 STEP 3: PRINT "Original Array:"
 STEP 4: REPEAT STEP 5 for(i=0; i<arr.length ; i++)
 STEP 5: PRINT arr[i]
 STEP 6: PRINT "Array in reverse order"
 STEP 7: REPEAT STEP 8 for(i= arr.length-1; i>=0; i--)
 STEP 8: PRINT a[i]
 STEP 9: END

Algorithm to print the elements of an array present on even position


 STEP 1: START
 STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}
 STEP 3: PRINT "Elements of given array present on even position:"
 STEP 4: REPEAT STEP 5 for(i=1; i< arr.length; i= i+2)
 STEP 5: PRINT arr[i]
 STEP 6: END

Algorithm to print the elements of an array present on odd position


 STEP 1: START
 STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}
 STEP 3: PRINT "Elements of given array present on odd position:"
 STEP 4: REPEAT STEP 5 for(i=0; i< arr.length; i= i+2)
 STEP 5: PRINT arr[i]
 STEP 6: END

Algorithm to print the largest element in an array


1. STEP 1: START
2. STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56}
3. STEP 3: max = arr[0]
4. STEP 4: REPEAT STEP 5 for(i=0; i< arr.length; i++)
5. STEP 5: if(arr[i]>max) max=arr[i]
6. STEP 6: PRINT "Largest element in given array:"
7. STEP 7: PRINT max
8. STEP 8: END

Algorithm to print the smallest element in an array


 STEP 1: START
 STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56}
 STEP 3: min = arr[0]
 STEP 4: REPEAT STEP 5 for(i=0; i< arr.length; i++)
 STEP 5: if(arr[i]<min)
min=arr[i]
 STEP 6: PRINT "Smallest element in given array:"
 STEP 7: PRINT min
 STEP 8: END

Algorithm to print the number of elements present in an array


 STEP 1: START
 STEP 2: INITIALIZE arr = {1,2,3,4,5}
 STEP 3: PRINT arr.length
 STEP 4: EXIT

Algorithm to print the sum of all the items of the array


 STEP 1: START
 STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}
 STEP 3: SET sum = 0
 STEP 4: REPEAT STEP 5 UNTIL i<arr.length
            //for(i=0; i< arr.length; i++)
 STEP 5: sum = sum + arr[i]
 STEP 6: PRINT "Sum of all the elements of an array:"
 STEP 7: PRINT sum
 STEP 8: END

Algorithm to right rotate the elements of an array


 STEP 1: START
 STEP 2: INITIALIZE arr[] ={1, 2, 3, 4, 5 }.
 STEP 3: SET n =3
 STEP 4: PRINT "Original Array"
 STEP 5: REPEAT STEP 6 UNTIL i<arr.length
            //for(i=0; i<arr.length; i++)
 STEP 6: PRINT arr[i]
 STEP 7: REPEAT STEP 8 to STEP 12 UNTIL i<n
            // for(i=0; i<n; i++ )
 STEP 8: DEFINE j, last.
 STEP 9: last = arr[arr.length-1]
 STEP 10: REPEAT STEP 11 UNTIL j>0
            //for(j= arr.length-1;j>0; j--)
 STEP 11: arr[j]= arr[j-1]
 STEP 12: arr[0]= last
 STEP 13: PRINT "Array after right rotation"
 STEP 14: REPEAT STEP 15 UNTIL i<arr.length
            //for(i=0; i<arr.length; i++)
 STEP 15: PRINT arr[i]
 STEP 16: END

Algorithm to sort the elements of an array in ascending order


 STEP 1: START
 STEP 2: INITIALIZE arr[] ={5, 2, 8, 7, 1 }.
 STEP 3: SET temp =0
 STEP 4: PRINT "Elements of Original Array"
 STEP 5: REPEAT STEP 6 UNTIL i<arr.length
            //for(i=0; i<arr.length; i++)
 STEP 6: PRINT arr[i]
 STEP 7: REPEAT STEP 8 to STEP 9 UNTIL i<arr.length
            //for(i=0; i<arr.length; i++ )
 STEP 8: REPEAT STEP 9 UNTIL j<arr.length
            //for(j=i+1;j<arr.length;j++)
 STEP 9: if(arr[i]>arr[j]) then
            temp = arr[i]
            arr[i]=arr[j]
            arr[j]=temp
 STEP 10: PRINT new line
 STEP 11: PRINT "Elements of array sorted in ascending order"
 STEP 12: REPEAT STEP 13 UNTIL i<arr.length
            //for(i=0;i<arr.length;i++)
 STEP 13: PRINT arr[i]
 STEP 14: END

Algorithm to sort the elements of an array in descending order


 STEP 1: START
 STEP 2: INITIALIZE arr[] ={5, 2, 8, 7, 1 }.
 STEP 3: SET temp =0
 STEP 4: PRINT "Elements of Original Array"
 STEP 5: REPEAT STEP 6 UNTIL i<arr.length
            //for(i=0; i<arr.length; i++)
 STEP 6: PRINT arr[i]
 STEP 7: REPEAT STEP 8 to STEP 9 UNTIL i<arr.length
            //for(i=0; i<arr.length; i++ )
 STEP 8: REPEAT STEP 9 UNTIL j<arr.length
            //for(j=i+1;j<arr.length;j++)
 STEP 9: if(arr[i]<arr[j]) then
            temp = arr[i]
            arr[i]=arr[j]
            arr[j]=temp
 STEP 10: PRINT new line
 STEP 11: PRINT "Elements of array sorted in descending order"
 STEP 12: REPEAT STEP 13 UNTIL i<arr.length
            //for(i=0;i<arr.length;i++)
 STEP 13: PRINT arr[i]
 STEP 14: END

Algorithm to subtract the two matrices


 STEP 1: START
 STEP 2: DEFINE rows, cols
 STEP 3: INITIALIZE first matrix a[][] ={{4,5,6},{3,4,1}, {1,2,3}}
 STEP 4: INITIALIZE second matrix b[][] ={{2,0,3}, {2,3,1}{1,1,1}}
 STEP 5: rows = a.length
 STEP 6: cols = a[0].length
 STEP 7: DEFINE diff[][]
 STEP 8: REPEAT STEP 9 to STEP 10 UNTIL i<rows
            //for(i=0;i<rows; i++)
 STEP 9: REPEAT STEP 10 UNTIL j<cols
            //for(j=0;j<cols; j++)
 STEP 10: diff[i][j] =a[i][j] - b[i][j]
 STEP 11: PRINT "Subtraction of two matrices:"
 STEP 12: REPEAT STEP 13 to STEP 14 UNTIL i<rows
            //for(i=0;i<rows; i++)
 STEP 13: REPEAT STEP 14 UNTIL j<cols
            //for(j=0; j<cols; j++)
 STEP 13: PRINT diff[i][j]
 STEP 14: PRINT new line
 STEP 15: END

Algorithm to determine whether two matrices are equal


 STEP 1: START
 STEP 2: DEFINE row1, col1, row2, col2
 STEP 3: INITIALIZE first matrix a[][] ={{1, 2, 3}, {8, 4,6}, {4, 5,7}}
 STEP 4: INITIALIZE second matrix b[][] ={{1 2, 3}, {8, 4, 6}{4, 5, 7}}
 STEP 5: row1 = a.length
 STEP 6: col1 = a[0].length
 STEP 7: row2 =b.length
 STEP 8: col2 = b[0].length
 STEP 9: if(row1!=row2 || col1!=col2)
        then
        PRINT "No"
        else
        go to step 10;
 STEP 10: REPEAT STEP 11 UNTIL i<row1
        //for(i=0; i<row1; i++)
 STEP 11: REPEAT STEP12 UNTIL j<col1
        //for(j=0; j<col1; j++)
 STEP 12: if(a[i][j]=b[i][j]) then
        flag =false
        break
 STEP 13: if(flag)
        then PRINT "Yes"
        else
        PRINT "No"
 STEP 14: END

Algorithm to display the lower triangular matrix


 STEP 1: START
 STEP 2: DEFINE rows, cols
 STEP 3: INITIALIZE matrix a[][] ={{1,2,3},{8, 6, 4}, {4, 5, 6}}
 STEP 4: rows = a.length
 STEP 5: cols = a[0].length
 STEP 6: if(rows!=cols)
        then
        PRINT "Matrix should be a square matrix"
        else
        Go to step 7
 STEP 7: REPEAT STEP 4 to STEP 6 UNTIL i<rows
        //for(i=0; i<rows; i++)
 STEP 8: REPEAT STEP 9 UNTIL j<cols // for(j=0; j<cols; j++)
        If(j>i) then PRINT 0 else PRINT a[i][j]
 STEP 9: PRINT new line
 STEP 10: END

Algorithm to display the upper triangular matrix


 STEP 1: START
 STEP 2: DEFINE rows, cols
 STEP 3: INITIALIZE matrix a[][] ={{1,2,3},{8, 6, 4}, {4, 5, 6}}
 STEP 4: rows = a.length
 STEP 5: cols = a[0].length
 STEP 6: if(rows!=cols)
        then
        PRINT "Matrix should be a square matrix"
        else
        Go to step 7
 STEP 7: REPEAT STEP 8 to STEP 10 UNTIL i<rows
        //for(i=0; i<rows; i++)
 STEP 8: REPEAT STEP 9 UNTIL j<cols // for(j=0; j<cols; j++)
 STEP 9: If(i>j) then PRINT 0 else PRINT a[i][j]
 STEP 10: PRINT new line
 STEP 11: END
Algorithm to find the frequency of odd & even numbers in the given
matrix
 STEP 1: START
 STEP 2: DEFINE rows, cols
 STEP 3: SET countOdd = 0, countEven =0
 STEP 4:INITIALIZE matrix a[][] ={{4,1,3},{3, 5, 7}, {8, 2, 6}}
 STEP 5: rows = a.length
 STEP 6: cols = a[0].length
 STEP 7: REPEAT STEP 8 to STEP 9 UNTIL i<rows for(i=0; i<rows; i++)
 STEP 8: REPEAT STEP 9 UNTIL j<cols
 STEP 9: if(a[i][j]%2 ==0)
        countEven++
        else
        countOdd++
 STEP 10: PRINT "Frequency of odd numbers " by assigning countOdd.
 STEP 11: PRINT "Frequency of even numbers " by assigning countEven
 STEP 12: END

Algorithm to find the product of two matrices


 STEP 1: START
 STEP 2: DEFINE row1, col1, row2, col2
 STEP 3: INITIALIZE matrix a[][] ={{1,3,2},{3,1,1}, {1,2,2}}
 STEP 4: INITIALIZE matrix b[][] ={{2,1,1},{1,0,1}, {1,3,1}}
 STEP 5: row1 = a.length
 STEP 6: col1 = a[0].length
 STEP 7: row2 =b.length
 STEP 8: row2 = b[0].length
 STEP 9: if(col1!=row2)
        PRINT "Matrices cannot be multiplied"
        else
        Go to step 10;
 STEP 10: prod[][] = [row1][col2]
 STEP 11: REPEAT STEP 12 to STEP 14 UNTIL i<row1
        //for(i=0; i<row1; i++)
 STEP 12: REPEAT STEP 13 to STEP 14 UNTIL j<col2 // for(j=0; j<col2; j++)
        If(j>i) then PRINT 0 else PRINT a[i][j]
 STEP 13: REPEAT STEP 14 UNTIL k<row2 // for(k=0; k<row2; k++)
 STEP 14: prod[i][j] = prod[i][j] + a[i][k]*b[k][j]
 STEP 15: REPEAT STEP 16 to STEP 18 UNTIL i<row1
 STEP 16: REPEAT STEP 17 UNTIL j<col2
 STEP 17: PRINT prod[i][j]
 STEP 18: PRINT new line
 STEP 19: END
Algorithm to find the sum of each row and each column of a matrix
 STEP 1: START
 STEP 2: DEFINE rows, cols, sumRow, sumCol
 STEP 3: INITIALIZE matrix a[][] ={{1, 2, 3},{4, 5, 6}, {7, 8, 9}}
 STEP 4: rows = a.length
 STEP 5: cols = a[0].length
 STEP 6: REPEAT STEP 7 to STEP 10 UNTIL i<rows
        // for(i=0; i<rows; i++)
 STEP 7: SET sumRow =0
 STEP 8: REPEAT STEP 9 UNTIL j<cols
 STEP 9: sumRow = sumRow + a[i][j]
 STEP 10: PRINT i+1, sumRow
 STEP 11: REPEAT STEP 12 to STEP 15 UNTIL i<cols
        //for(i=0; i<cols; i++)
 STEP 12: SET sumCol =0
 STEP 13: REPEAT STEP 14 UNTIL j<rows
        //for(j=0; j<rows; j++)
 STEP 14: sumCol =sumCol + a[j][i]
 STEP 15: PRINT i+1, sumCol
 STEP 16: END

Algorithm to find the transpose of a given matrix


 STEP 1: START
 STEP 2: DEFINE rows, cols
 STEP 3: INITIALIZE matrix a[][] ={{1, 2, 3},{4, 5, 6}, {7, 8, 9}}
 STEP 4: rows = a.length
 STEP 5: cols = a[0].length
 STEP 6: t[][] = [cols][rows]
 STEP 7: REPEAT STEP 8 to STEP 9 UNTIL i<cols
        // for(i=0; i<cols; i++)
 STEP 8: REPEAT STEP 9 UNTIL j<rows
        // for(j=0; j<rows; j++)
 STEP 9: t[i][j] = a[j][i]
 STEP 10: PRINT "Transpose of given matrix"
 STEP 11: REPEAT STEP 12 to STEP 14 UNTIL i<cols
        //for(i=0; i<cols; i++)
 STEP 12: REPEAT STEP 13 UNTIL j<rows
        //for(j=0; j<rows; j++)
 STEP 13: PRINT t[i][j]
 STEP 14: PRINT new line
 STEP 15: END
Algorithm to count the total number of characters in a string
 STEP 1: START
 STEP 2: DEFINE String string = "The best of both worlds".
 STEP 3: SET count =0.
 STEP 4: SET i=0. REPEAT STEP 5 to STEP 6 UNTIL i<string.length
 STEP 5: IF (string.charAt(i)!= ' ') then count =count +1.
 STEP 6: i=i+1
 STEP 7: PRINT count.
 STEP 8: END

Algorithm to draw a pattern

 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

Algorithm to count the total number of vowels and consonants in a


string
 STEP 1: START
 STEP 2: SET vCount =0, cCount =0
 STEP 3: DEFINE string str = "This is a really simple sentence".
 STEP 4: CONVERT str to lowercase
 STEP 5: SET i =0.
 STEP 6: REPEAT STEP 6 to STEP 8 UNTIL i<str.length()
 STEP 7: IF any character of str matches with any vowel then
vCount = vCount + 1.
STEP 8: IF any character excepting vowels lies BETWEEN a and z then
cCount = cCount =+1.
 STEP 9: i = i + 1
 STEP 10: PRINT vCount.
 STEP 11: PRINT cCount.
 STEP 12: END

Algorithm to determine whether two strings are the anagram


 STEP 1: START
 STEP 2: DEFINE str1 = "Brag", str2 = "Grab".
 STEP 3: CONVERT str1, str2 to lower-case.
 STEP 4: IF length of str1, str2 are not equal then PRINT "Not Anagram"
else go to Step 5
 STEP 5: CONVERT str1, str2 to character arrays.
 STEP 6: SORT the arrays.
 STEP 7: COMPARE the arrays, IF equal then PRINT "Anagram"
else
PRINT "Not Anagram"
 STEP 8: END

Algorithm to divide a string in 'N' equal parts.


 STEP 1: START
 STEP 2: DEFINE str = "aaaabbbbcccc"
 STEP 3: DEFINE len
 STEP 4: SET n =3
 STEP 5: SET temp = 0.
 STEP 6: chars = len/n
 STEP 7: DEFINE String[] equalstr.
 STEP 8: IF (len%n!=0)
then PRINT ("String can't be divided into equal parts")
else go to STEP 9
 STEP 9: SET i =0.
 STEP 10: REPEAT STEP 11 to STEP 14 UNTIL i<len
 STEP 11: DEFINE substring part.
 STEP 12: equalstr [temp] = part
 STEP 13: temp = temp + 1
 STEP 14: i = i + chars
 STEP 15: PRINT n
 STEP 16: SET i=0. REPEAT STEP 17 to STEP 18 UNTIL i<equalstr.length
 STEP 17: PRINT equalstr[i]
 STEP 18: i = i + 1
 STEP 19: END

Algorithm to find all subsets of a string


 STEP 1: START
 STEP 2: DEFINE string str = "FUN"
 STEP 3: DEFINE len = str.length()
 STEP 4: SET temp =0
 STEP 5: DEFINE String array having length: len*(len + 1)/2
 STEP 6: SET i =0. REPEAT STEP 7 to STEP 11 UNTIL i<len
 STEP 7: SET j =1. REPEAT STEP 8 to STEP 10 UNTIL j<len
 STEP 8: arr[temp] = str.substring(i, j+1)
 STEP 9: temp = temp + 1
 STEP 10: j =j + 1
 STEP 11: i =i +1
 STEP 12: PRINT ("All subsets for given string are: ")
 STEP 13: SET i = 0
 STEP 14: REPEAT STEP 14 UNTIL i<arr.length
 STEP 14: PRINT arr[i]
 STEP 15: END

Algorithm to find the longest repeating sequence in a string


main()

 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

Algorithm to find all the permutations of a string


main()

 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

generatePermutation(String str, int start, int 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

swapString(String a, int i, int j)

 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 remove all the white spaces from a string


 STEP 1: START
 STEP 2: DEFINE string str1 = "Remove white spaces".
 STEP 3: REPLACE all space characters with blank using replaceAll().
 STEP 4: PRINT str1.
 STEP 5: END

Algorithm to replace lower-case characters with upper-case and vice-


versa
 STEP 1: START
 STEP 2: DEFINE string str = "Great Power".
 STEP 3: DEFINE newstr as StringBuffer object .
 STEP 4: SET i=0. REPEAT STEP 5 to STEP 6 UNTIL i<str.length().
 STEP 5: IF lower-case character encountered then CONVERT them in upper-case using built-in
function
else
IF upper-case character encountered then CONVERT them in lower-case characters using built-in
function.
 STEP 6: i=i+1
 STEP 7: PRINT newstr.
 STEP 8: END
Algorithm to replace the spaces of a string with a specific character
 STEP 1: START
 STEP 2: String string = "Once in a blue moon".
 STEP 3: char ch = '-'
 STEP 4: String = string.replace(' ', ch)
 STEP 5: PRINT "String after replacing spaces with given character:"
 STEP 6: PRINT string.
 STEP 7: END

Algorithm to determine whether a given string is palindrome


 STEP 1: START
 STEP 2: DEFINE String string = "Kayak"
 STEP 3: SET flag = true
 STEP 4: CONVERT string into lowercase.
 STEP 5: SET i =0. REPEAT STEP 6 to STEP 7 UNTIL i<="" li="">
 STEP 6: IF (string.charAt(i) != string.charAt(string.length()-i-1))
              then
              SET flag = false
              break
 STEP 7: SET i = i + 1
 STEP 8: IF flag
              then PRINT "Yes"
              else
              PRINT "No"
 STEP 9: END

Algorithm to determine whether one string is a rotation of another


 STEP 1: START
 STEP 2: DEFINE String str1 = "abcde", str2 = "deabc"
 STEP 3: IF length of str1 not equals to str2 then PRINT "No"
              else go to STEP 4
 STEP 4: CONCATENATE str1 with str1.
 STEP 5: IF str2 present in str1 then PRINT "Yes" else PRINT "No".
 STEP 6: END

Algorithm to find maximum and minimum occurring character in a


string
 STEP 1: START
 STEP 2: DEFINE String str = "grass is greener on the other side"
 STEP 3: INITIALIZE minChar, maxChar.
 STEP 4: DEFINE i, j, min, max.
 STEP 5: CONVERT str into char string[].
 STEP 6: SET i =0. REPEAT STEP 7 to STEP 11 UNTIL i<string.length< li=""> </string.length<>
 STEP 7: SET array freq[i] =1
 STEP 8: SET j =i+1. REPEAT STEP 9 to STEP 10 UNTIL j<string.length< li=""> </string.length<>
 STEP 9: IF (string[i] == string[j] && string[i] != ' ' && string[i] != '0')
              then
              freq[i] = freq[i] + 1
              SET string[j] = 0
 STEP 10: j = j +1
 STEP 11: i = i + 1
 STEP 12: SET min = max = freq[0]
 STEP 13: SET i =0. REPEAT STEP 14 to STEP 16 UNTIL i<freq.length< li=""> </freq.length<>
 STEP 14: IF(min>freq[i] && freq[i]!=0) then
              min = freq[i]
              minChar[] = string[i]
 STEP 15: IF max is lesser than freq[i]then
              max = freq[i]
              maxChar[] = string[i]
 STEP 16: i =i +1
 STEP 17: PRINT minChar
 STEP 18: PRINT maxChar
 STEP 19: END

Algorithm to find Reverse of the string


 STEP 1: START
 STEP 2: DEFINE String string = "Dream big"
 STEP 3: DEFINE reversedStr = " "
 STEP 4: SET i =string.length()-1. REPEAT STEP 5 to STEP 6 UNTIL i>=0
 STEP 5: reversedStr = reversedStr + string.charAt(i)
 STEP 6: i = i - 1
 STEP 7: PRINT string.
 STEP 8: PRINT reversedStr.
 STEP 9: END

Algorithm to find the duplicate characters in a string


 STEP 1: START
 STEP 2: DEFINE String string1 = "Great responsibility"
 STEP 3: DEFINE count
 STEP 4: CONVERT string1 into char string[].
 STEP 5: PRINT "Duplicate characters in a given string:"
 STEP 6: SET i = 0. REPEAT STEP 7 to STEP 11 UNTIL i<string.length< li=""> </string.length<>
 STEP 7: SET count =1
 STEP 8: SET j = i+1. REPEAT STEP 8 to STEP 10 UNTIL j<string.length< li=""> </string.length<>
 STEP 9: IF (string[i] == string[j] && string[i] != ' ')
              then
              count = count + 1
              string[j]= 0
 STEP 10: j = j + 1
 STEP 11: i = i + 1
 STEP 12: IF(count>1 && string[i] != 0) then PRINT string[i]
 STEP 13: END

Algorithm to find the duplicate words in a string


 STEP 1: START
 STEP 2: DEFINE String string = "Big black bug bit a big black dog on his big black nose"
 STEP 3: DEFINE count
 STEP 4: CONVERT string into lower-case.
 STEP 5: INITIALIZE words[] to SPLIT the string.
 STEP 6: PRINT "Duplicate words in a given string:"
 STEP 7: SET i=0. REPEAT STEP 8 to 12 STEP UNTIL i<words.length< li=""> </words.length<>
 STEP 8: SET count =1.
 STEP 9: SET j = i+1. REPEAT STEP 10 to STEP 11 UNTIL j<words.length< li=""> </words.length<>
 STEP 10: IF (words[i].equals(words[j])
              then
              count = count + 1
              words[j]= 0
 STEP 11: j = j + 1
 STEP 12: i = i + 1
 STEP 13: IF(count>1 && words[i] != 0) then PRINT words[i]
 STEP 13: END

Algorithm to find the frequency of characters


 STEP 1: START
 STEP 2: DEFINE String str = "picture perfect"
 STEP 3: INITIALIZE freq[] having same size of str.
 STEP 4: DEFINE i, j
 STEP 5: CONVERT str into char string[].
 STEP 6: SET i=0. REPEAT STEP 7 to 11 STEP UNTIL i<str.length< li=""> </str.length<>
 STEP 7: SET freq[i] =1
 STEP 8: SET j = i+1. REPEAT STEP 9 to STEP 10 UNTIL j<str.length< li=""> </str.length<>
 STEP 9: IF (string[i] == string[j]) then
              freq[i]++
              string[j]= 0
 STEP 10: j = j + 1
 STEP 11: i = i + 1
 STEP 12: PRINT "Characters and their corresponding frequencies "
 STEP 13: SET i=0. REPEAT STEP 14 to STEP 15 UNTIL i<freq.length< li=""> </freq.length<>
 STEP 14: IF(string[i] != ' ' && string[i] != '0')               then
              PRINT string[i], freq[i]
 STEP 15: i=i+1
 STEP 16: END

Algorithm to find the largest and smallest word in a string.


 STEP 1: START
 STEP 2: DEFINE String string="Hardships often prepare ordinary people for an extraordinary
destiny"
 STEP 3: DEFINE word = " ", small = " ", large = " ".
 STEP 4: Make object of String[] words.
 STEP 5: SET length =0
 STEP 6: string = string + " "
 STEP 7: SET i=0. REPEAT STEP 8 to 9 STEP UNTIL i<str.length()< li=""> </str.length()<>
 STEP 8: IF(string.charAt(i) != ' ') then
              word =word + string.charAt(i)
              else
              word[length]=word
              length =length + 1
              word = " "
 STEP 9: i=i+1
 STEP 10: small = large =words[0]
 STEP 11: SET k = 0. REPEAT STEP 12 to STEP 14 UNTIL k<length< li=""> </length<>
 STEP 12: IF(small.length() > words[k].length())
              then
              small = words[k]
 STEP 13: IF(large.length() < words[k].length())
              then
              large = words[k]
 STEP 14: k = k + 1
 STEP 15: PRINT small
 STEP 16: PRINT large
 STEP 17: END

Algorithm to find the most repeated word in a text file


 STEP 1: START
 STEP 2: DEFINE String line, word = ""
 STEP 3: SET count =0, maxCount =0
 STEP 4: DEFINE ArrayList<String> words
 STEP 5: USE File Reader to open file in read mode.
 STEP 6: READ line from file
 STEP 7: By looping, CONVERT each line into lower case.
 STEP 8: REMOVE the punctuation marks.
 STEP 9: SPLIT the lines and STORE in array string[].
 STEP 10: ADD all words generated in previous step into words.
 STEP 11: SET i=0.REPEAT STEP 12 to STEP 17 UNTIL i<words.size()
 STEP 12: SET count =1
 STEP 13: SET j=i+1.REPEAT STEP 14 to 15 STEP UNTIL j<words.size()
 STEP 14: IF(words.get(i).equals(words.get(j))) then count = count+1.
 STEP 15: j = j+1
 STEP 16: IF count>maxCount
then
maxCount = count
word =words.get(i)
 STEP 17: i=i+1
 STEP 18: PRINT word
 STEP 19: 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

Algorithm to separate the Individual Characters from a String


 STEP 1: START
 STEP 2: DEFINE String string = "characters "
 STEP 3: PRINT "Individual characters from given string: "
 STEP 4: SET i=0. REPEAT STEP 5 to STEP 6 UNTIL i<string.length()
 STEP 5: PRINT string.charAt(i)
 STEP 6: i=i+1
 STEP 7: END

Algorithm to swap two string variables without using third or temp


variable.
 STEP 1: START
 STEP 2: DEFINE Strings str1 = "Good ", str2 = "morning " to swap
 STEP 3: PRINT "Strings before swapping " str1, str2
 STEP 4: str1 =str1 + str2
 STEP 5: EXTRACT str1 from indexes 0 to length (str1) - (str2) using substring function and store it
in str2.
 STEP 6: EXTRACT str1 from index length(str2) till end using substring function and store it in str1.
 STEP 7: PRINT "Strings after swapping " str1, str2.
 STEP 8: END
Algorithm to print smallest and biggest possible palindrome word in a
given string
main()

 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)

smallPalin = bigPalin = words[i]


else go to STEP 15 and STEP 16

 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)

for each item in the list


if match item == value
return the item's location
end if
end for

end procedure

Binary Search in Java


BINARY_SEARCH(A, lower_bound, upper_bound, VAL)

 Step 1: [INITIALIZE] SET BEG = lower_bound


END = upper_bound, POS = - 1
 Step 2: Repeat Steps 3 and 4 while BEG <=END
 Step 3: SET MID = (BEG + END)/2
 Step 4: IF A[MID] = VAL
SET POS = MID
PRINT POS
Go to Step 6
ELSE IF A[MID] > VAL
SET END = MID - 1
ELSE
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]
 Step 5: IF POS = -1
PRINT "VALUE IS NOT PRESENT IN THE ARRAY"
[END OF IF]
 Step 6: EXIT
Bubble Sort

Algorithm
begin BubbleSort(list)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

return list

end BubbleSort

Pseudocode
Pseudocode of BubbleSort algorithm can be written as follows −

procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do:


swapped = false

for j = 0 to loop-1 do:

/* compare the adjacent elements */


if list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] )
swapped = true
end if

end for

/*if no number was swapped that means


array is sorted now, break the loop.*/

if(not swapped) then


break
end if

end for

end procedure return list

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

/* check the element to be minimum */

for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for

/* swap the minimum element with the current element*/


if indexMin != i then
swap list[min] and list[i]
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

The insertion sort algorithm involves:

1. Start with the result as the first element of the input.

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.

You might also like