Java: Insertion Binary Sort

Okay, this might be 5 o' clock in the morning talking, but I cannot seem to get this right. It does not sort the numbers at all. Thank you for any help.

The program is made up of three classes: A which has the sort, Test which test it and Interval which is basically a simple class used to hold two dates.

Please let me know if it won't compile, I just took out the important bits of my program and pasted them here.

A:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package abc;
import java.util.*;

class A {
	ArrayList<Interval> dates;
	
	public void addDate(Interval i) {//addDate method
			if(dates.isEmpty()) {
				dates.add(i);
				return;
			}
			int high=dates.size()-1,
				low=0,
				midpoint=low+((high-low+1)/2);
			while(high>low) { //while
				if(dates.get(midpoint).start_date.before(i.start_date)) {//if
					low=midpoint+1;
				}//if 
				else {//else
					high=midpoint;
				}//else
			}//while
			dates.add(high, i);
			System.out.println(high);
			for(int j=0; j<dates.size(); ++j) {
				System.out.println(dates.get(j).start_date);
			}
			System.out.println("++++++++++++++");
		}//addDate method
}


Test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package abc;
import java.text.*;

public class Tester {
	public static void main(String[] args) {
		try {
			A r=new A("View", 5, 4, 1000);
			SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yy");
			r.addDate(new Interval(sdf.parse("01/10/13"), sdf.parse("05/10/13")));
			r.addDate(new Interval(sdf.parse("10/10/13"), sdf.parse("15/10/13")));
			r.addDate(new Interval(sdf.parse("06/10/13"), sdf.parse("10/10/13")));
			r.addDate(new Interval(sdf.parse("15/10/13"), sdf.parse("20/10/13")));
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
}


Interval:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package abc;
import java.util.*;

public class Interval {
	public Date start_date, end_date;
	
	public Interval(Date start_date, Date end_date) 
	throws IllegalArgumentException{
		if(start_date.before(end_date)) {
			this.start_date=start_date;
			this.end_date=end_date;
		} else {
			throw new IllegalArgumentException("Start date must be before end date");
		}
	}
}
Sorting implies moving things around in the array. I don't see you doing that.
Also, I don't quite know what you are trying to do with a "binary insertion sort", unless you are trying to optimize the insert with a binary search for the proper place.

http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/insertion-sort/

By the way, if you plan to sort something you should create a separate method specifically for it.

Hope this helps.
Last edited on
Sorting implies moving things around in the array. I don't see you doing that.

The reason I chose insertion sort, is because it allows me to sort elements one by one, because basically my program will not be getting all the data at once. i.e: I get one date, it then gets inserted, next time I run the program (obviously there are files being used to serialize data) another date is added, so on and so forth. If I was getting all the data at once I would have used a much more efficient sorting technique.
EDIT: I just read the linked article, this is what I am doing:
Lists can be sorted as you receive new elements.


Also, I don't quite know what you are trying to do with a "binary insertion sort", unless you are trying to optimize the insert with a binary search for the proper place.

Yes, that is exactly it, a quick Google search would have given you the answer as well.

By the way, if you plan to sort something you should create a separate method specifically for it.

I will try that, but do you have any idea on why my code is not working?
Last edited on
Yes, that is exactly it, a quick Google search would have given you the answer as well.


Get some sleep and try again, because you are being rather rude. I've done more googling on sorting methods than you have.

It should be fairly obvious that I was so tired that I missed the connection, seeing as I wrote it down in my response but still missed it. Maybe I was tired, no? You will also notice that the FAQ I linked for you specifically recommends using a binary search to find the insertion point.

but do you have any idea on why my code is not working?


Sorting implies moving things around in the array. I don't see you doing that.

Try actually moving some elements in your array.

I'm not at a PC I can use to play with your code ATM, but I'll see if I can get a chance later.
Get some sleep and try again, because you are being rather rude. I've done more googling on sorting methods than you have.

I was not meaning to be rude, sorry, emotions and tone do not translate well into text.

[Maybe I was tired, no?/quote]
Sorry again, I was not trying to be rude.

[quote]Try actually moving some elements in your array.

I technically am, the ArrayList<T>'s add(int, T) method does this for me.

I'm not at a PC I can use to play with your code ATM, but I'll see if I can get a chance later.

No need I have solved it and it works now, thank you. Here is a link to the site that helped me: http://learn.hackerearth.com/tutorial/sorting-algorithm/67/binary-insertion-sort/

New A:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package abc;
import java.util.*;

class A{
	ArrayList<Interval> dates;
	public void addDate(Interval i) {//addDate method
			if(dates.isEmpty()) {//if
				dates.add(i);
				return;
			}//if
			int high=dates.size(),
				low=0,
				midpoint=low+((high-low)/2); //sets parameters for binary search
			while(high>low) { //while
				if(i.start_date.before(dates.get(midpoint).start_date)) {//if
					high=midpoint;
				}//if 
				else {//else
					low=midpoint+1;
				}//else
				midpoint=low+((high-low)/2);
			}//while
			dates.add(high, i);
		}//addDate method
}
Topic archived. No new replies allowed.