-2

I am a beginner in Java trying to create a class that implements the binary insertion sort and sorts out random arrays of the data sizes 50, 500, 5000, 50000, and 500000.

The program works fine when I implemented it as an insertion sort.

public double InsertionSort(long array[]) {
            setType("Insertion Sort");
            long temp;
            int y;
            double numOfSwap = 0, numOfComparisons = 0;
            double startTime = System.nanoTime();
            for (int x = 1; x < array.length; x++) {
                temp = array[x];
                numOfSwap++;
                y = x;
                numOfComparisons++;
                while ((y > 0)) {
                    numOfComparisons++;
                    if ((array[y - 1]) > temp) {
                        array[y] = array[y - 1];
                        numOfSwap++;
                        y = y - 1;
                    } else
                        break;
                }
                array[y] = temp;
                numOfSwap++;
            }
            double endTime = System.nanoTime();
            setSwap(numOfSwap / 3);
            setComparisons(numOfComparisons);
            setTime(endTime - startTime);
            return getTime();

        }

But when I tried to insert a binary search, it did not work anymore.

 public double binaryInsertionSort(long array[], int value, int left, int right) {
            setType("Binary Insertion Sort");
            long temp;
            int y;
            int left, right;
            double numOfSwap = 0, numOfComparisons = 0;
            double startTime = System.nanoTime();
            for (int x = 1; x < array.length; x++) {
                temp = array[x];
                numOfSwqp++;
                int left = y;
                int right = x;
                if (left>right)
                    return -1;
                int middle = (left + right)/2;
                if (array[middle] == value)
                    return middle;
                numOfComparisons++;
                else if (array[middle]>value)
                    return binaryInsertionSort(array, value,left, middle -1);
                numOfComparisons++;
                else
                    return binaryInsertionSort (array, value, middle +1, right);
                numOfComparisons++;
            }
            double endTime = System.nanoTime();
            setSwap(numOfSwap / 3);
            setComparisons(numOfComparisons);
            setTime(endTime - startTime);
            return getTime();
        }

Can someone please help me fix my code?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
chocotella
  • 89
  • 1
  • 9
  • 1
    Have you ever heard about MCVE? It stands for Minimal, Complete and Verifiable Example. I think you should work on your "Minimal". http://stackoverflow.com/help/mcve – Jaroslaw Pawlak May 27 '16 at 13:58
  • @JaroslawPawlak: At least he provided complete code. – Gaurav Mahindra May 27 '16 at 14:01
  • @GauravMahindra It might be a good thing, but saying "I have this 400 lines of code, I changed it to the other 400 lines of code and it stopped working" is not helpful in any way. – Jaroslaw Pawlak May 27 '16 at 14:04
  • It would also be helpful if the code would at least compile. It has numerous syntax errors. Not only is the provided code far from *minimal*, it is also not *verifiable*. – John Bollinger May 27 '16 at 14:07
  • @JaroslawPawlak I formally apologize sir, I had no intention of making nor did I know about an MCVE. I thought providing the full code would be helpful for those who were learning and had similar questions in the future - helping expand this learning community. Thank you for letting me know of my mistake. – chocotella May 27 '16 at 14:16
  • @GauravMahindra I appreciate it sir. I did not intent to commit an MCVE and I have learnt from my mistake. I am just stressed now because it seems that my question will never be answered, and I really wanted to learn. – chocotella May 27 '16 at 14:23
  • @JohnBollinger Thank you, sir. – chocotella May 27 '16 at 14:25
  • @chocotella: It will be answered. Answer will take some time as it is a long code. Do not worry. – Gaurav Mahindra May 27 '16 at 14:26
  • @GauravMahindra I doubt that anyone will go through this 800 lines of code without even knowing what the problem is. chocotella - something for you: http://stackoverflow.com/help/how-to-ask – Jaroslaw Pawlak May 27 '16 at 14:33
  • Can you explain why you rejected my edit? It improved the post, yet you have ignored it and still abuse stack snippets. – bcsb1001 May 27 '16 at 14:56
  • @bcsb1001: Are you talking to me ? I edited it for proper view and understanding. It was java code, not html. – Gaurav Mahindra May 27 '16 at 15:00
  • @GauravMahindra I'm talking to the asker, who rejected my useful edit. – bcsb1001 May 27 '16 at 15:31
  • Got your mail. Reply you by tomorrow eve. Thanks. – Gaurav Mahindra May 28 '16 at 09:38
  • I sent you correct answer. Check your mail box. Acknowledge me by replying. – Gaurav Mahindra May 28 '16 at 11:21

2 Answers2

1

There were mistakes in your code. I corrected them. First of all your left and right variables are defined in this line :-

public double binaryInsertionSort(long array[], int value, int left, int right)

why are you again defining them inside method body. So I removed their double declaration. Second you assigned value of left variable to y which was wrong. Actually you have to assign value of y toleft. Third error in your code was your method calls were wrong. You defined binaryInsertionSort with four parameters and you were calling it by single parameter so I modified your method calls like this:-

sortTime = binaryInsertionSort(sortedArray,10,20,30);

Rest were minor mistakes. Here is corect code of your `binaryInsertionSort method :-

public double binaryInsertionSort(long array[], int value, int left, int right) {

setType("Binary Insertion Sort");

long temp;

int y=0;

//int left, right;

double numOfSwap = 0, numOfComparisons = 0;

double startTime = System.nanoTime();

for (int x = 1; x < array.length; x++) {

temp = array[x];

numOfSwap++;

y=left;

right = x;

if (left>right){
return -1;
}

int middle = (left + right)/2;

if (array[middle] == value){

    numOfComparisons++;
    return middle;

} else if (array[middle]>value){

    numOfComparisons++;
    return binaryInsertionSort(array, value,left, middle -1);

} else{

    numOfComparisons++;    
    return binaryInsertionSort (array, value, middle +1, right);
    }

}

double endTime = System.nanoTime();

setSwap(numOfSwap / 3);

setComparisons(numOfComparisons);

setTime(endTime - startTime);

return getTime();

}
`

I mailed you complete corrected code of your program. Check your mail box. Acknowledge me whether you find my answer useful or not. Happy Coding :)

Gaurav Mahindra
  • 424
  • 2
  • 6
  • 21
  • I apologize for the extremely late reply, but despite the duration I still need reply and thank you for your time! – chocotella Nov 13 '16 at 23:32
0

Your binary insertion sort code contains numerous errors that your compiler must be telling you about. Your first order of business should be understanding and fixing all of those. Alternatively, many of the problems with the binary insertion sort code appear to arise from it having been made by attempting to adapt the standard insertion sort code. I recommend instead implementing the binary version from scratch. Additionally, implement the sort first; add instrumentation (comparison and swap counts) after the sort itself is working.

Furthermore, there are some distinct oddities in your code:

  • it is atypical and not advantageous to implement any version of insertion sort recursively.

  • your specific attempt at a recursive implementation does not make sense anyway: the main loop would run only one iteration, and the code after the loop would be dead.

  • It's hard to be sure, but I think you've got the whole idea of binary insertion sort wrong. You appear to be trying to split the array into pieces to sort those recursively, like a merge sort or quick sort, but that's not how binary insertion sort works. Binary insertion sort differs from standard insertion sort primarily in that it uses a binary search instead of a linear search to find the insertion position for each element.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157