Results 1 to 9 of 9
Thread: Elementary Sorts
- 06-13-2011, 05:35 PM #1
Elementary Sorts
Hey Guys,
I was just going through a book that is teaching elementary sorts so I created a program called OrderedArray in order to test them. However, I am not getting the results I want. When I time the 3 types of sorts: bubble, selection, and insertion, Bubble Sort seems dominant. This should not be true as the bubble sort is SUPPOSED to be the simplest and longest algorithm.
Can one of you guys please look at my code and make sure that I am sorting them out correctly? I just need to know whether the sort code is following the algorithm properly and not doing extra steps.
This is the OrderedArray Class:
Here is the program that uses the OrderedArray Class:Java Code:public class OrderedArray { int [] array; int num_elements = 0; int size = 0; public OrderedArray (int size) { this.size = size; array = new int [size]; } public int search (int key) { int lowerBound = 0; int higherBound = num_elements - 1; int binary = 0; int count = 0; while (true) { binary = (higherBound + lowerBound) / 2; if (count > num_elements) return -1; else if (higherBound == lowerBound && array[higherBound] == key) return higherBound; else if (array[binary] == key) return binary; else if (array[binary] < key) lowerBound = binary + 1; else if (array[binary] > key) higherBound = binary - 1; count += 1; } } public void bubblesort () { for (int i = 0; i < num_elements - 1; i++) { if (array[i] > array[i + 1]) { int temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; } } } public void selectionsort () { int temp; for (int i = 0; i < num_elements - 1; i++) { temp = i; for (int j = i + 1; j < num_elements; j++) if (array[j] < array[temp]) temp = j; int temp2 = array[i]; array[i] = array[temp]; array[temp] = temp2; } } public void insertionsort () { for (int i = 1; i < num_elements; i++) { int temp = array[i]; for (int j = i; j > 0; j--) { if (temp < array[j - 1]) array[j] = array[j - 1]; else { array[j] = temp; break; } } } } public void insert (int x) { if (num_elements < size) { array[num_elements] = x; num_elements += 1; } else System.out.println("Too many items!"); } public void delete (int x) { int target_element = search(x); if (target_element == -1) System.out.println("Number cannot be found!"); else { array[target_element] = 0; for (int i = target_element; i < num_elements - 1; i++) array[i] = array[i + 1]; array[num_elements - 1] = 0; num_elements -= 1; } } public void print (String s) { long startTime; if (s.equals("bubblesort")) { startTime = System.nanoTime(); bubblesort(); System.out.println("Bubble Sort Time: " + (System.nanoTime() - startTime)); } else if (s.equals("selectionsort")) { startTime = System.nanoTime(); selectionsort(); System.out.println("Selection Sort Time: " + (System.nanoTime() - startTime)); } else if (s.equals("insertionsort")) { startTime = System.nanoTime(); insertionsort(); System.out.println("Insertion Sort Time: " + (System.nanoTime() - startTime)); } if (num_elements != 0) { System.out.print("["); for(int i = 0; i < num_elements - 1; i++) System.out.printf("%d, ", array[i]); System.out.printf("%d]\n", array[num_elements - 1]); } else { System.out.print("["); for(int i = 0; i < array.length - 1; i++) System.out.printf("%d, ", array[i]); System.out.printf("%d]\n", array[array.length - 1]); } } public void clear () { for (int i = 0; i < num_elements; i++) { array[i] = 0; } num_elements = 0; } }
Thanks!Java Code:public class OrderedArrayApp { public static void main(String[] args) { OrderedArrayApp main = new OrderedArrayApp(); main.initialize(); } private void initialize() { OrderedArray array = new OrderedArray(20); } }Tabish Chasmawala
- 06-13-2011, 05:40 PM #2
Your code has no comments describing what it is trying to do and how it is going to do it. Where do you document how you are implementing each sort?
Where is your testing program? How do you test the code you posted?Last edited by Norm; 06-13-2011 at 05:45 PM.
- 06-13-2011, 06:10 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Your bubble sort implementation is incorrect (and it doesn't sort your data). It needs two nested loops, not a single loop. Check your text book(s).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-13-2011, 06:10 PM #4
Norm: Sorry about that... I didn't realize how important comments were. I will add the comments describing the process and post it again here later.
JosAH: How does it not sort the data? I tested it out and it works perfectly... And when I step through it, it is using the bubble sort algorithm. Is it necessary to use two loops? Aren't there different ways to approach the problem?
Thanks!Last edited by tabchas; 06-13-2011 at 06:13 PM.
Tabish Chasmawala
- 06-13-2011, 06:18 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Try to make it sort the data 5, 4, 3, 2, 1; it comes out as 4, 3, 2, 1, 5. One pass of 'bubbling' isn't enough, it only bubbles one element to its final place, i.o.w. you need n of those passes which means another (nested) loop. Your implemenation isn't correct.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-13-2011, 06:26 PM #6
It doesn't sort for me either.
You need to provide code that shows how you've tested it.
- 06-13-2011, 06:58 PM #7
Sorry about that. I now realize the importance of the two nested loops.
Thanks!
Here is the updated Bubble Sort Method:
Java Code:public void bubblesort () { for (int i = 0; i < num_elements - 1; i++) { for (int j = 0; j < num_elements - 1; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } }Tabish Chasmawala
- 06-13-2011, 07:27 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 06-14-2011, 12:50 AM #9
Similar Threads
-
Tnsping of some sorts
By mac in forum New To JavaReplies: 1Last Post: 01-23-2010, 03:47 PM -
Elementary problems
By mgm2010 in forum New To JavaReplies: 6Last Post: 02-01-2009, 08:33 PM -
Elementary Quesions
By hitmen in forum New To JavaReplies: 6Last Post: 10-21-2008, 12:23 AM -
Topological Sorts
By Emily in forum Advanced JavaReplies: 1Last Post: 04-03-2008, 04:39 PM -
Topological Sorts
By Emily in forum New To JavaReplies: 0Last Post: 03-27-2008, 12:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks