C/C++ How Can I Convert Quicksort from Java to C++?

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    C++ Java
AI Thread Summary
The discussion centers on converting a quicksort algorithm from Java to C++. The original Java code provided has some syntax errors, such as incorrect assignment operators and misplaced brackets. The correct Java implementation uses a do/while loop and properly declares variables. Key differences between Java and C++ include array declaration syntax—Java uses type[] name while C++ uses type name[]—and variable declaration flexibility, where Java allows declarations anywhere before use, unlike C++. The conversation also notes that while the provided quicksort implementation is correct, there are more efficient versions available. Overall, the conversion process is straightforward with attention to these differences.
needOfHelpCMath
Messages
70
Reaction score
0
I have never done Java but my professor says it is similar to c++. I am trying to convert quicksort in java and covert it to c++.
I don't know if this is correct or not. Here is the code my professor gave us.

Code:
 .....Java......

public static void quicksort(char[], int left, int right) 
{
    int i, j;
    i - left; j - right;
    x = items[(left+right) / 2];

do
{
    while([items[i] < x) && (i <right)] i++;
    while[(x < items[j]) && (i > left)] j--;

    if (i <= j) {
       y = items[i];
       items[i] = items[j];
       items[j] = y;
       i++; j--; }
}
    while(i<=j);
    if (left < j) quicksort(items, left, j);
    if (i < right)quicksort(items,i,right);
    }
Code:
 ......C++......

void QuickSort(int[] nums, int left, int right) {
	int i, j,;
	int x, y;
	i - left;
	j-right;
	x = nums[(left+right)/2];
	
	while(nums[i] < x && i < right) {
		++i;
		while(x < num[j] && j > left) {
			j--;
			
			if(i <= j) {
				y = nums[i];
				nums[i] = nums[j];
				nums[j] = y;
				i++; j--;
			}
			}
		}
		}
	
	while(i <= j) {
		if(left < j) {
			QuickSort(nums, left);
		}
		if(i < right) {
			QuickSort(nums,i,right);
		}
	}
 
Technology news on Phys.org
I assume your instructor gave you correct Java code and you didn't faithfully copy his code. Here's his correct quicksort:

Code:
   public static void quicksort(char[] items, int left, int right) {
      int i, j;
      i = left;
      j = right;
      char x = items[(left + right) / 2], y;
      do {
         while ((items[i] < x) && (i < right)) {
            i++;
         }
         while ((x < items[j]) && (j > left)) {
            j--;
         }
         if (i <= j) {
            y = items[i];
            items[i] = items[j];
            items[j] = y;
            i++;
            j--;
         }
      } while (i <= j);
      if (left < j) {
         quicksort(items, left, j);
      }
      if (i < right) {
         quicksort(items, i, right);
      }
   }

One small difference between Java and C++, Java arrays are declared with type[] name, but C++ arrays are type name[]. Example: Java char[] items in C++ is char items[]. One other small difference: Java allows you to declare variables anyplace before they are used. You should know about the do/while loop in C++ so a C++ implementation should be almost immediate.

FYI: There are many different ways to write quicksort. The given implementation is certainly correct and easy, but there are much better implementations. The wikipedia article https://en.wikipedia.org/wiki/Quicksort may be a little advanced, but it's pretty good.
 
johng said:
I assume your instructor gave you correct Java code and you didn't faithfully copy his code. Here's his correct quicksort:

Code:
   public static void quicksort(char[] items, int left, int right) {
      int i, j;
      i = left;
      j = right;
      char x = items[(left + right) / 2], y;
      do {
         while ((items[i] < x) && (i < right)) {
            i++;
         }
         while ((x < items[j]) && (j > left)) {
            j--;
         }
         if (i <= j) {
            y = items[i];
            items[i] = items[j];
            items[j] = y;
            i++;
            j--;
         }
      } while (i <= j);
      if (left < j) {
         quicksort(items, left, j);
      }
      if (i < right) {
         quicksort(items, i, right);
      }
   }

One small difference between Java and C++, Java arrays are declared with type[] name, but C++ arrays are type name[]. Example: Java char[] items in C++ is char items[]. One other small difference: Java allows you to declare variables anyplace before they are used. You should know about the do/while loop in C++ so a C++ implementation should be almost immediate.

FYI: There are many different ways to write quicksort. The given implementation is certainly correct and easy, but there are much better implementations. The wikipedia article https://en.wikipedia.org/wiki/Quicksort may be a little advanced, but it's pretty good.

Thanks! that will help me alot. Appreciate it
 
johng said:
One small difference between Java and C++, Java arrays are declared with type[] name, but C++ arrays are type name[]. Example: Java char[] items in C++ is char items[].
And it is still possible to use C++ array syntax in Java as well.

johng said:
One other small difference: Java allows you to declare variables anyplace before they are used.
Is it different in C++?

Your Java program compiles as a C++ program if one changes the array declaration and the method declaration. So in this example Java and C++ are basically the same.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top