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

  • Context: C/C++ 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    C++ Java
Click For Summary
SUMMARY

This discussion focuses on converting the Quicksort algorithm from Java to C++. The original Java implementation provided by the professor is correct and includes key differences such as array declaration syntax and variable declaration placement. The corrected Java code is presented, emphasizing the need for precise syntax in C++. Additionally, the discussion highlights that while the provided implementation is valid, there are more efficient versions of Quicksort available.

PREREQUISITES
  • Understanding of Quicksort algorithm
  • Familiarity with Java syntax and C++ syntax
  • Knowledge of array declaration differences in Java and C++
  • Basic understanding of control structures like do/while loops
NEXT STEPS
  • Study the differences in array syntax between Java and C++
  • Learn about advanced Quicksort implementations and optimizations
  • Explore C++ variable declaration rules and scope
  • Review the Wikipedia article on Quicksort for deeper insights
USEFUL FOR

Students learning programming, software developers transitioning from Java to C++, and anyone interested in algorithm optimization and implementation.

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 a lot. 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.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
848
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K