public class Permutations
{
// Helper method for outputting an array.
private static void PrintArray(string[] array)
{
foreach (string element in array)
{
Console.Write($"{element} ");
}
Console.WriteLine();
}
// Helper method for invoking Generate.
private static void Generate(string[] array)
{
Generate(array.Length, array);
}
public static void Main(string[] args)
{
}
}
procedure generate(k : integer, A : array of any):
if k = 1 then
output(A)
else
// Generate permutations with kth unaltered
// Initially k == length(A)
generate(k - 1, A)
// Generate permutations for kth swapped with each k-1 initial
for i := 0; i < k-1; i += 1 do
// Swap choice dependent on parity of k (even or odd)
if k is even then
swap(A[i], A[k-1]) // zero-indexed, the kth is at k-1
else
swap(A[0], A[k-1])
end if
generate(k - 1, A)
end for
end if
using System.IO;
using System;
public class Permutations
{
// Helper method for outputting an array.
private static void PrintArray(string[] array)
{
foreach(string element in array)
{
Console.Write($"{element} ");
}
Console.WriteLine();
}
// Helper method for invoking Generate.
private static void Generate(string[] array)
{
Generate(array.Length,array);
}
private static void Generate(int k, string[] array)
{
if(k==1)
PrintArray(array);
else
{
// Generate permutations for kth unaltered
// Initially k == length(array)
Generate(k-1,array);
// Generate permutations for kth swapped with each k-1
initial
for(int i=0;i<k-1;i++)
{
// Swap choice dependent on parity of k(even or
odd)
if(k%2 == 0)
{
swap(array,i,k-1); // zero indexed , the kth is at k-1
}else
{
swap(array,0,k-1);
}
Generate(k-1,array);
}
}
}
private static void swap(string[] array, int i, int j)
{
string temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void Main(string[] args)
{
Generate(args);
}
}
Output:
Input
Command Line arguments : qax wsx edc
Output:
Public class Permutations { // Helper method for outputting an array. private static v...
C# QUESTION Requirements Your task is to write a program that will print out all the s of the command line arguments to a program For example, given the arguments 'gaz, wx,and 'edc, your program should output wsxaazea Your implementation is expected to use Heap's algorithm according to the following pseudocode: procedure generatelk: integer, A: array of any): if k 1 then output A) else // Generate permutations with kth unaltered //Initially k length(A) // Generate permutations for kth swapped...
PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...
in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray (int[] array) that returns an ArrayList of all permutations of the the parameter array. The ArrayList may contain the ar- rays in any order. Example: Suppose array = {4, 7, 1, 2}. Then the ArrayList would contain the following arrays, but in any order: 4 7 12 7 4 1 2 2 1 4 7 1 24 7 4 7 2 1 7 4...
1. Look at the following pseudocode algorithm. package javaapplication292 public class JavaApplication292 ( public static void main(StringlI args) recFun(258); public static void recFun( int x) if (x > 10) recFun (x/10); System.out. printIn( x % 10 ); else System.out.println(x); What is the output of the following statements? a. recFun(258); b. recFun (7);
4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...
(How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...
Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter { /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort */ public static void sort(int[] arr) { // Your...
Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...
a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a collection of n values, where 0 < k < n. Write a program that uses a minheap method to find the kth smallest value in a collection of n values. Use the MinHeap class defined in part a. public final class MinHeap<T extends Comparable<? super T>> implements MinHeapInterface<T> { private T[] heap; // Array of heap entries; ignore heap[0] private int...
import java.util.Arrays; public class lab { public static void main(String args[]) { int arr[] = {10, 7, 8, 9, 1, 5,6,7}; int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; int arr3[] = {1, 3, 5, 3, 2, 6, 20}; quicksort(arr,0,arr.length-1); quicksort(arr2,0,arr2.length-1); quicksort(arr3,0,arr3.length-1); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr3)); } private static int partition(int[] items,int low, int high) { int i=0; int j=0;...