Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There is C code below my professor wants me to implement different methodss of g

ID: 3804379 • Letter: T

Question

There is C code below my professor wants me to implement different methodss of generating all N! permutations of N elements in JAVA!! This has to be in java. Could someone please help me convert his c code to java code and add comments for me to understand the steps

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MILLION 1000000L

unsigned N;
char P[27];
unsigned table[12][12] =
{ {0}, {0, 1}, {0, 1, 1}, {0, 1, 2, 3},
    {0, 3, 1, 3, 1},
    {0, 3, 4, 3, 2, 3},
    {0, 5, 3, 1, 5, 3, 1},
    {0, 5, 2, 7, 2, 1, 2, 3},
    {0, 7, 1, 5, 5, 3, 3, 7, 1},
    {0, 7, 8, 1, 6, 5, 4, 9, 2, 3},
    {0, 9, 7, 5, 3, 1, 9, 7, 5, 3, 1},
    {0, 9, 6, 3, 10, 9, 4, 3, 8, 9, 2, 3}};

void init(void){
unsigned i;

P[0] = '*';
for (i=0; i<26; i++)
    P[i+1] = 'A' + i;
}

inline void swap(unsigned i, unsigned j){
char c;

c = P[i];
P[i] = P[j];
P[j] = c;
}

void printIt(void){
static unsigned long count = 0;

//to compare performance, comment out from here
unsigned i;

for (i=1; i<=N; i++)
    printf("%c", P[i]);
printf(" ");
//to here

count++;
}

void permute1(unsigned n){
unsigned i;

if (n == 1)
    printIt();
for (i=1; i<=n; i++){
    swap(i, n);
    permute1(n-1);
    swap(i, n);
}
}

void permute2(unsigned n){
unsigned i;

if (n == 1){
    printIt();
    return;
}
for (i=1; i<=n; i++){
    permute2(n - 1);
    swap(table[n][i], n);
}
}

void permute3(unsigned n){
unsigned i;

if (n == 1){
    printIt();
    return;
}
for (i=1; i<=n; i++){
    permute3(n - 1);
    swap((n & 0x01) ? 1 : i, n);
}
}

void (*algPtr[4])(unsigned) = {NULL, permute1, permute2, permute3};

int main(int argc, char *argv[]){
unsigned alg;
int c;

N = 4;
alg = 1;
while ((c = getopt(argc, argv, "a:n:")) != -1){
    switch (c){
    case 'a': sscanf(optarg, "%u", &alg); break;
    case 'n': sscanf(optarg, "%u", &N); break;
    default: break;
    }
}
N = ((N < 2) || (N > 12)) ? 4 : N;
alg = ((alg < 1) || (alg > 3)) ? 1 : alg;

init();

struct timeval start, stop;
float sec;

gettimeofday(&start, NULL);
algPtr[alg](N);
gettimeofday(&stop, NULL);

sec = (stop.tv_sec - start.tv_sec) +
    (stop.tv_usec - start.tv_usec) / (float)MILLION;
printf("permute%1d, N = %u, %.6f ", alg, N, sec);

return 0;
}

Explanation / Answer

package pinku;

import com.mtsystems.coot.IntContainer;

import com.mtsystems.coot.String8;

public class Pinku convert {
   public static int n_U;

   public static byte[] p = new byte[27];

   public static IntContainer table_U = IntContainer.fromPartialData(12, 12, new int[][] {{0}, {0, 1}, {0, 1, 1}, {0, 1, 2, 3}, {0, 3, 1, 3, 1}, {0, 3, 4, 3, 2, 3}, {0, 5, 3, 1, 5, 3, 1}, {0, 5, 2, 7, 2, 1, 2, 3}, {0, 7, 1, 5, 5, 3, 3, 7, 1}, {0, 7, 8, 1, 6, 5, 4, 9, 2, 3}, {0, 9, 7, 5, 3, 1, 9, 7, 5, 3, 1}, {0, 9, 6, 3, 10, 9, 4, 3, 8, 9, 2, 3}});
}

public class Pinku convert {
   public static void init() {
       String8 p = String8.fromPartialData(0, '*');
       for(int i_U = 0; Integer.compareUnsigned(i_U, 26) < 0; i_U++) {
           p.set(i_U + 1, (byte)('A' + i_U));
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote