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

  • C/C++
  • Thread starter needOfHelpCMath
  • Start date
  • Tags
    C++ Java
In summary, the conversation discusses the similarities between Java and C++ and the process of converting a quicksort algorithm from Java to C++. The correct implementation of quicksort in Java is provided, along with some differences between Java and C++, such as array declaration syntax and variable declaration rules. The conversation also mentions that there are many different ways to write quicksort and provides a resource for further learning.
  • #1
needOfHelpCMath
72
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
  • #2
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.
 
  • #3
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
 
  • #4
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.
 

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

Can Java code be directly converted into C++?

No, Java code cannot be directly converted into C++. While both languages are object-oriented and have similar syntax, they differ in many ways such as memory management, libraries, and file handling. A manual translation of the code is required to convert Java into C++.

What are the benefits of converting Java into C++?

There are a few potential benefits of converting Java into C++. One major benefit is the potential for improved performance, as C++ is a lower-level language and offers more control over memory management. Additionally, C++ has a larger variety of libraries and frameworks available, which can be useful for certain applications.

Are there any drawbacks to converting Java into C++?

Yes, there are some drawbacks to converting Java into C++. One potential issue is the time and effort required to manually translate the code, which can be significant for large projects. Additionally, C++ can be more complex and prone to errors due to its lower-level nature, so it may not be suitable for all applications.

Can I use automated tools to convert Java into C++?

There are some automated tools available that claim to be able to convert Java code into C++, however, their results may not be reliable or efficient. Manual translation is still the most recommended method for converting Java into C++, as it allows for more control and better optimization of the code.

Is it possible to convert Java into C++ without losing any functionality?

While it is possible to convert Java into C++ without losing any functionality, it is not guaranteed. As mentioned earlier, the two languages have some differences, so some code may not be directly translatable. Additionally, certain Java features may not have an equivalent in C++, so some functionality may need to be rewritten in a different way.

Similar threads

Replies
6
Views
2K
Replies
22
Views
3K
Replies
25
Views
2K
Replies
3
Views
1K
Replies
4
Views
2K
Replies
1
Views
1K
Replies
3
Views
3K
Back
Top