Sorting Algorithms Comments

Comments

  • I utterly & completely do not understand how to deal with the input here.
    Could one please see an example in code?
    I notice my comments haven't been answered, lately, but I really would like this one answered, because I can't move on. Thanks.

  • Here's a simple solution to this problem: my code. You just need to print the sum of each list of numbers.

  • Well it's not actually the solution I was after, but the input method. It says:

    The input for these challenges will follow the format summarized in the sidebar.

    cont...
  • Dear Admin,

    As what i can see from the "Your Output" it is the same as "Correct Output", however the last array is not displayed so im not sure if im getting "Incorrect" due to the last array or something else.

    cont...
  • @David, it displays all the output. Make sure you're printing the final steps too.

  • Thanks Admin, i noticed just now... double checking on my end needs to be improved.

  • Received this runtime error.
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)

    cont...
  • @Rod, sorry, new code to scan the input has been added. You can click "Reset Code" to load it.

  • public static void doStuff(Blob[] ar){
        String[] result = countingSort(ar);
        for(int i=0; i<result.length;i++){
        System.out.print(result[i] + " ");
    
    cont...
  • Your output partitions about the value at the LEFT of the array, not the RIGHT as specified in the challenge description

  • Can anyone tell me what is the problem with the code:

    static void doStuff(int[] ar){
        int pivotValue, storeIndex;
        int left = 0; int right = ar.length - 1;
    
    cont...
  • In this challenge, note the elements should otherwise remain in the same order (see correct output).

  • Thank you for your answer. Can you give me a hint, howto modify the code to keep the lower and higher elements in such order?

  • I've solved it, but in a not too efficient way:

    static void doStuff(int[] ar){
        int pivotValue;
    
        int left = 0; int right = ar.length - 1;
    
    cont...
  • After getting it correct, you can view the featured answer(s) for a possible solution:
    http://www.learneroo.com/modules/4/nodes/44/featured-submissions

    (The sort only becomes efficient in Quick-Sort Advanced.)

  • correct answer for the first input should be 3 1 4 9 5

    public static int partition(int[] a, int lo, int hi) {
        int i = lo + 1;
        int j = hi;
    
    cont...
  • Since this is just the first step in quicksort, the array should otherwise remain in the same order. So since the 5 is before the 9 in the input, it should be before the 9 in the output.

  • static void doStuff(int[] ar){
        int pivot = ar[0];
        int pivotLocation = 0;
        for(int i = 1; i < ar.length; i++){
            int curr = ar[i];
            if(curr < pivot){
    
    cont...
  • static void doStuff(int[] ar){
        int pivot = ar[0];
        int pivotLocation = 0;
        for(int i = 1; i < ar.length; i++){
            int curr = ar[i];
            if(curr < pivot){
    
    cont...
  • static void doStuff(int[] ar){
    partition(ar, 0, ar.length - 1);
    }

    static void partition(int[] ar, int min, int max){
        int pivot = ar[min];
    
    cont...
  • Shouldn't the correct output on the first line be 1 3 2 5 9 8 7? The 8 comes before the 7 in the input.

  • @Kendall, in this version of Quicksort, you don't move numbers that are already in their correct half. Since 7 > 5 (the pivot value), it stays where it is.

  • Got it. Is this method faster than using nested for loops and finding the smallest number in the inner loop and then switching it with the left most element (the outer loop moves where the left most element is)?

  • Quicksort is among the fastest sorts in the average case. Technically, it runs in O(n log n) time (see the next Node for a brief look at it's running time). I'm not sure what you're describing, but it sounds like a Selection Sort which would have a O(n2) running time (like Insertion Sort).

    cont...
  • Thanks. The way I have sorted before is:
    for(I=0 to end)
    min=array(I)
    minIndex=i
    for(j=I to end)
    if(array(I)<min)
    Switch array(I) & min and save minIndex
    end if
    end for
    end for

Contact Us
Sign in or email us at [email protected]