Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Tolls

Thread: Sorting comparable objects?

  1. #1
    Simbaku is offline Member
    Join Date
    Mar 2017
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default 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".

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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.
    Norm likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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[10];
    // 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Sorting comparable objects?

    Quote Originally Posted by Tolls View Post
    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?

  7. #7
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default 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

Similar Threads

  1. Sorting an array of objects.
    By ssk2659 in forum New To Java
    Replies: 13
    Last Post: 02-14-2015, 11:04 PM
  2. Sorting objects in a custom list
    By Parashurama in forum New To Java
    Replies: 17
    Last Post: 09-14-2012, 05:40 PM
  3. Sorting objects
    By zelenkooo in forum New To Java
    Replies: 4
    Last Post: 06-11-2012, 02:43 PM
  4. Replies: 0
    Last Post: 10-04-2011, 10:45 PM
  5. sorting objects in java
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 09:18 AM

Posting Permissions

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