Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 843511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:08:22+00:00 2026-05-15T06:08:22+00:00

In the book Introduction to Algorithms, second edition, there is the following problem: Suppose

  • 0

In the book “Introduction to Algorithms”, second edition, there is the following problem:

Suppose we have some array:

int a[] = {1,2,3,4}

and some random priorities array:

P = {36,3,97,19}

and the goal is to permute the array a randomly using this priorities array.

This is the pseudo code:

PERMUTE-BY-SORTING (A)
1 n ← length[A]
2 for i ← 1 to n
3      do P[i] = RANDOM (1, n 3)
4 sort A, using P as sort keys
5 return A

The result should be the permuted array:

B={2, 4, 1, 3};

I have written this code:

import java.util.*;

public class Permute {

    public static void main (String[] args) {
        Random r = new Random();
        int a[] = new int[] {1,2,3,4};
        int n = a.length;
        int b[] = new int[a.length];
        int p[] = new int[a.length];
        for (int i=0; i<p.length; i++) {
            p[i] = r.nextInt(n*n*n) + 1;
        }

        // for (int i=0;i<p.length;i++){
        // System.out.println(p[i]);
        //}
    }
}

How do I continue?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:08:24+00:00Added an answer on May 15, 2026 at 6:08 am

    I’m not sure which part you’re having trouble with, but essentially this is what happened:

    int[] a = {  1,  2,  3,  4 };
    int[] p = { 36,  3, 97, 19 };
    

    However you think about it, essentially we want to “zip” the elements of these two lists together. So at the abstract level, we have the following:

    Pair<int,int> zipped = { ( 1,36), ( 2, 3), ( 3,97), ( 4,19) };
    

    Now we sort zipped by the second value in the Pair. Whatever sorting algorithm works; it doesn’t really matter.

    zipped = { ( 2, 3), ( 4,19), ( 1,36), ( 3,97) };
    

    We then unzip the pairs to get the permuted a:

    a = {  2,  4,  1,  3 };
    p = {  3, 19, 36, 97 };
    

    How to implement

    The zip-into-Pair-then-unzip works just fine. Otherwise, you can modify the sorting algorithm so that whenever it moves elements of p[i] to p[j], it also moves a[i] to a[j] to keep both arrays “in-sync”.


    Java snippet

    In the following snippet, the priorities array is hardcoded to the above values. You already figured out how to seed it with random numbers.

    import java.util.*;
    
    public class PermuteBySorting {
        public static void main(String[] args) {
            class PrioritizedValue<T> implements Comparable<PrioritizedValue<T>> {
                final T value;
                final int priority;
                PrioritizedValue(T value, int priority) {
                    this.value = value;
                    this.priority = priority;
                }
                @Override public int compareTo(PrioritizedValue other) {
                    return Integer.valueOf(this.priority).compareTo(other.priority);
                }           
            }
            int[] nums = { 1, 2, 3, 4 };
            int[] priorities = { 36, 3, 97, 19 };
            final int N = nums.length;
            List<PrioritizedValue<Integer>> list =
                new ArrayList<PrioritizedValue<Integer>>(N);
            for (int i = 0; i < N; i++) {
                list.add(new PrioritizedValue<Integer>(nums[i], priorities[i]));
            }
            Collections.sort(list);
            int[] permuted = new int[N];
            for (int i = 0; i < N; i++) {
                permuted[i] = list.get(i).value;
            }
            System.out.println(Arrays.toString(permuted));
            // prints "[2, 4, 1, 3]"
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following problem from book introduction algorithm second edition by MIT university problem
I'm reading Introduction to Algorithms book, second edition, the chapter about Medians and Order
Recently I have read about hash-tables in a very famous book Introduction to Algorithms
I am working through the Introduction to Algorithms book by Cormen, and I have
I just read about the breadth-first search algorithm in the Introduction to Algorithms book
While studying the book Introduction to Algorithms by Cormen, I found a strange thing.
I'm looking for a book similar to Introduction to Algorithms by Thomas Cormen geared
I have been reading parts of Introduction to Algorithms by Cormen et al, and
I am reading about string algorithms in Cormen's book Introduction to Algorithms. For Transition
Can anybody recommend a good introduction book on Monte Carlo algorithms in c++? Preferably

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.