Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Insertion points in Array using Binary Search using Comparable

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Insertion points in Array using Binary Search using Comparable

    Can't figure out what's wrong. Operator is undefined for argument type. Error is located at the end of the binarysearch method array[position] < key

    import java.util.Arrays;
     
    public class binarySearch {
     
        public static <T extends Comparable<T>> int binarysearch(T key, T[] array) {
            int start = 0;
            int end = array.length - 1;
            int position =-1;
            while (start <= end && position == -1) {
                int mid = start + (end - start) / 2;
                if (array[mid].compareTo(key) > 0) {
                end = position - 1;
                } else if (array[mid].compareTo(key) < 0) {     
                    start = position + 1;
                } else {
                	position = mid;
                }
            } 
     
            if (position == -1) { //if the target is not found, find the position to insert
            	position = start;
            while (position < end && array[position] < key) { //error is here
            	position ++;
            	position --; //insert after position
            }
        }
            return position;
        }
     
     
        public static void main(String[] Args) {
     
            int key;
            Integer[] array = {2, 3, 6, 22, 23, 31, 32, 34, 41, 43};
            key = 1; //integer being inserted
            System.out.println("\nTestcase 1\nelement inserted to the left and not already present");
            System.out.println("Array : "+Arrays.toString(array));
            System.out.println("Value to be inserted "+ key);
            System.out.println("The Key can has to be inserted at index " + binarysearch(key, array));
    }
    }


    --- Update ---

    figured it out, thanks.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Insertion points in Array using Binary Search using Comparable

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

Similar Threads

  1. Problem with a binary insertion sort method
    By kert666 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 29th, 2013, 12:54 PM
  2. Sorting Array & Binary Search Help
    By xJavaLover in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2013, 09:53 PM
  3. Binary Search for an array of random ints - Can't find the target value
    By mju516 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2012, 11:25 AM
  4. Using Comparable in Binary Search
    By Shaybay92 in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 12th, 2011, 03:23 AM
  5. Binary Search Help
    By OwsumEmam in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 31st, 2011, 11:01 PM