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 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.
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.
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
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition of the function numOnes(n), which counts the number of 1s in bit-string of length n, Make sure to define the function for the base case, numOnes(0).
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Suppose that a “word” is any string of six letters. Repeated letters are allowed. For our...
Suppose that a “word” is any string of six letters. Repeated letters are allowed. For our purposes, vowels are the letters a, e, i, o, and u. a) How many words are there? b) How many words begin with a vowel? c) How many words begin with a vowel and end with a vowel? d) How many words have no vowels? e) How many words have exactly one vowel? A professor teaching a Discrete Math course gives a multiple choice...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT