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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top