Question

In: Computer Science

Give pseudocode for a recursive function that sorts all letters in a string. For example, the...

Give pseudocode for a recursive function that sorts all letters in a string. For example, the string "goodbye" would be sorted into "bdegooy". Java

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in JAVA with code and output shown.
  • For better understanding please read the comments mentioned in the code.
  • In the code below function sort the character array passed in-place recursively and after that prints the sorted string in the main function, for details see the code below.
  • Pseudo code for function is :

Function recursive_sort(array: char, length of array){

if (length== 1) then,

return;

for(i=0;i<n-1;i++){

current_character=array[i];

next_character=array[i+1];

if (current_character >next_character) then,

swap (array[i], array[i+1]);

}

recursive_sort(arr, length-1);

}

  • CODE:

import java.util.*;
public class Solution{
//function to sort a character array recursively
public static void recursive_sort(char arr[],int n){
//base case
//if length of array is 1 then return
if (n==1){
return;
}
  
//to save adjacent character
char ch1,ch2;
for (int i=0;i<n-1;i++){
//adjacent character
ch1=arr[i];
ch2=arr[i+1];
//if character at ith is greater than character at (i+1)th
if(ch1 > ch2){
//then swap them
char temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
//call function recursively with length-1
recursive_sort(arr, n-1);
}
  
  
  
//driver function
public static void main(String []args){
//sample string
String s="goodbye";
  
//before
System.out.println(s);
//string to character array
char[] arr = s.toCharArray();
//call function
recursive_sort(arr,s.length());
//char array to string
s=String.valueOf(arr);
  
//after
System.out.println(s);
  
}
}

  • OUTPUT:

goodbye
bdegooy

  • Below are the screenshot attached for the code and output for better clarity and understanding.

CODE

OUTPUT

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

Give pseudocode for a recursive function that sorts all letters in a string. For example, the...
Give pseudocode for a recursive function that sorts all letters in a string. For example, the string "goodbye" would be sorted into "bdegooy". Python
Create program which sorts letters of a string based on ASCII value. The program will then...
Create program which sorts letters of a string based on ASCII value. The program will then print the sorted string to stdout. Use C programming language. - Only use stdio.h - Input prompt should say "Enter string of your choice: " - Remove any newline \n from input string - Implement sorting operation as a function. Should use selection sort algorithm, but you may use a different algorithm - Output should print sorted string on new line Example:     Enter...
Write pseudocode for a function that translates a telephone number with letters in it (such as...
Write pseudocode for a function that translates a telephone number with letters in it (such as 1-800-FLOWERS) into the actual phone number. Use the standard letters on a phone pad
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a...
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a parameter and evaluates to true iff every character in the string is the same. Note: A string of length 0 or 1 should also evaluate to true.
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;   f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 b) Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
Reversing certain segments of the alphabet, using a recursive function. (C++) 1. Obtain the following string:...
Reversing certain segments of the alphabet, using a recursive function. (C++) 1. Obtain the following string: abcdefghijklmnopqrstuvwxyz (as input or using initialization) 2. Using recursion, write a reverse function that reverses the characters in a string or character array given two indices (starting and ending). The string or the character array should reflect the reversal. 3. Read indices as input 11,18 (i.e. letters 12,19) 4. Call the reverse function to reverse letters: 12-19 5. Read indices as input 4,22 (i.e....
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT