1. Member Join Date
Mar 2017
Posts
1
Rep Power
0

## Sorting comparable objects?

*I AM NOT ASKING ANYONE TO CODE ANYTHING FOR ME, IM JUST CONFUSED*

So basically Im taking a data structures class rn and long story short the teacher teaches in Java, but I only know C++.

My Task : "To implement object type quick sorter class that can sort Comparable objects. Demonstrate the class in a program that sorts an array of string objects"

Basically I want to know what the code below actually does but in simpleton terms so I can use that information to translate this to C++.

Java Code:
```/**
The IntQuickSorter class provides a public static
method for performing a QuickSort on an int array.
*/

public class IntQuickSorter
{
/**
The quickSort method calls the doQuickSort method
to sort an int array.
@param array The array to sort.
*/

public static void quickSort(int array[])
{
doQuickSort(array, 0, array.length - 1);
}

/**
The doQuickSort method uses the QuickSort algorithm
to sort an int array.
@param array The array to sort.
@param start The starting subscript of the list to sort
@param end The ending subscript of the list to sort
*/

private static void doQuickSort(int array[], int start, int end)
{
int pivotPoint;

if (start < end)
{
// Get the pivot point.
pivotPoint = partition(array, start, end);

// Sort the first sub list.
doQuickSort(array, start, pivotPoint - 1);

// Sort the second sub list.
doQuickSort(array, pivotPoint + 1, end);
}
}

/**
The partiton method selects a pivot value in an array
and arranges the array into two sub lists. All the
values less than the pivot will be stored in the left
sub list and all the values greater than or equal to
the pivot will be stored in the right sub list.
@param array The array to partition.
@param start The starting subscript of the area to partition.
@param end The ending subscript of the area to partition.
@return The subscript of the pivot value.
*/

private static int partition(int array[], int start, int end)
{
int pivotValue;    // To hold the pivot value
int endOfLeftList; // Last element in the left sub list.
int mid;           // To hold the mid-point subscript

// Find the subscript of the middle element.
// This will be our pivot value.
mid = (start + end) / 2;

// Swap the middle element with the first element.
// This moves the pivot value to the start of
// the list.
swap(array, start, mid);

// Save the pivot value for comparisons.
pivotValue = array[start];

// For now, the end of the left sub list is
// the first element.
endOfLeftList = start;

// Scan the entire list and move any values that
// are less than the pivot value to the left
// sub list.
for (int scan = start + 1; scan <= end; scan++)
{
if (array[scan] < pivotValue)
{
endOfLeftList++;
swap(array, endOfLeftList, scan);
}
}

// Move the pivot value to end of the
// left sub list.
swap(array, start, endOfLeftList);

// Return the subscript of the pivot value.
return endOfLeftList;
}

/**
The swap method swaps the contents of two elements
in an int array.
@param The array containing the two elements.
@param a The subscript of the first element.
@param b The subscript of the second element.
*/

private static void swap(int[] array, int a, int b)
{
int temp;

temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}```
If its as simple as organizing strings based on content that's not a problem but I feel like there's more too it than that.

Appreciate ANY help!

In case its not clear I know absolutely NOTHING about Java, I just figured this was a better place than "new to java".
Last edited by Simbaku; 03-09-2017 at 11:59 AM.  Reply With Quote

2. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Sorting comparable objects?

This is doing a quick sort.
I'm not sure where your confusion lies, based on that code?

Are you unsure how to turn that into something that can quick sort Comparables?  Reply With Quote

3. Senior Member Join Date
Sep 2014
Location
MA, USA
Posts
399
Rep Power
7

## Re: Sorting comparable objects?

Looks to me that the code has nothing to do with the task on hand. Since the objects have to implement the Comparable Interface you would expect a call to the compareTo method of that object. Furthermore the OP is asked to sort Strings, but the code sorts Integers. I thought that the code was well documented in the comments, so I am not sure why OP asks us "what the code does".  Reply With Quote

4. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Sorting comparable objects?

Well, the logic is the same...but instead of any comparisons using <, > or == with the int you would use the int returned by compareTo.  Reply With Quote

5. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Sorting comparable objects?

The code supplied is nothing special (even if you don't know Java, it is closely related to basic C as it is functionally written).

The only things you need to know are the following:

Java Code:
```int [] a; // is an array type of int
int b; //is an integer.
//to declare an array of 10 values do
int[] a = new int;
// arrays are zero based

public static int method(int[] a, b, c) {
// code here
}
// is a method (subroutine) call that takes an array, and two integers as
// arguments and returns an int value.```
A method with a return type of void returns nothing.

Regards,
Jim  Reply With Quote

6. Senior Member Join Date
Sep 2014
Location
MA, USA
Posts
399
Rep Power
7

## Re: Sorting comparable objects? Originally Posted by Tolls Well, the logic is the same...but instead of any comparisons using <, > or == with the int you would use the int returned by compareTo.
correct, but I guess that's what they are looking for. Why else would they reference the Comparable Interface?  Reply With Quote

7. Member Join Date
Jan 2017
Posts
46
Rep Power
0

## Re: Sorting comparable objects?

If you do a find/replace on int[]/Comparable[] and then recompile, you will get compilation errors. Look at the API docs Comparable (Java Platform SE 8 ) and you should be able to fix that. Your class assignment should then be complete  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•