Ahmed Adnan
Ahmed Adnan

Reputation: 165

How to convert the codes of reverse function in C++ into Java

I have done the processor emulator assignment in c++ , now I am trying to convert the whole code in Java. The problem that is arising here is that we are shuffling processes by using reverse function in C++. I know there is a reverse function in java as well but I don't know how to use it with the following example.

The shuffle() function code is right below both in c++ and Java!

This is the C++ code:

void processor::shuffle(){

    int swapPriority;
    int swapTime;
    int swapProcessID;
    string swapProcessName;
    int end = used - 1;
    int length = used - 1;
    for (int counter = length - 1; counter>0; counter--){
        for (int index = 0; index<end; index++){
            if (priority[index]>priority[index + 1]){
                //ist work
                swapPriority = priority[index + 1];
                swapTime = timeReq[index + 1];
                swapProcessID = processId[index + 1];
                swapProcessName = processName[index + 1];
                //2nd
                priority[index + 1] = priority[index];
                timeReq[index + 1] = timeReq[index];
                processId[index + 1] = processId[index];
                processName[index + 1] = processName[index];
                //3rd
                priority[index] = swapPriority;
                timeReq[index] = swapTime;
                processId[index] = swapProcessID;
                processName[index] = swapProcessName;
            }


        }end--;

    }
    **The problem is here !!!!**
        //How to convert this below portion into Java code??
    if (priority[0]<priority[1]){
        reverse(priority, priority + length + 1);
        reverse(timeReq, timeReq + length + 1);
        reverse(processId, processId + length + 1);
        reverse(processName, processName + length + 1);
    }
}

This is the Java code:

public void shuffle() {
    int swapPriority;
    int swapTime;
    int swapProcessID;
    String swapProcessName;
    int end = used - 1;
    int length = used - 1;
    for (int counter = length - 1; counter > 0; counter--) {
        for (int index = 0; index < end; index++) {
            if (priority[index] > priority[index + 1]) {
                //1st work
                swapPriority = priority[index + 1];
                swapTime = timeReq[index + 1];
                swapProcessID = processId[index + 1];
                swapProcessName = processName[index + 1];
                //2nd
                priority[index + 1] = priority[index];
                timeReq[index + 1] = timeReq[index];
                processId[index + 1] = processId[index];
                processName[index + 1] = processName[index];
                //3rd
                priority[index] = swapPriority;
                timeReq[index] = swapTime;
                processId[index] = swapProcessID;
                processName[index] = swapProcessName;

            }

        }
        end--;

    }
    if (priority[0] < priority[1]) {



    }
    /* reverse(priority, priority + length + 1);
    reverse(timeReq, timeReq + length + 1);
    reverse(processId, processId + length + 1);
    reverse(processName, processName + length + 1);
    */

}

Upvotes: 0

Views: 266

Answers (1)

gaRos
gaRos

Reputation: 2783

Try this approach, it will reverse the order of X in 3 index range:

List<String> x = new ArrayList<>();
        x.add("1");x.add("2");x.add("3");x.add("4");x.add("5");x.add("6");x.add("7");x.add("8");
Comparator<String> ro = Collections.reverseOrder();  
Collections.sort(x.subList(0, 2),ro);
Collections.sort(x.subList(2, 5),ro);
Collections.sort(x.subList(5, 8),ro);

Upvotes: 1

Related Questions