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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Back
Top