954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Display how sort works? Please help

I have attached the file with the sorts. I have to show 2 sorts insertion, and selection sorts demonstration in bars. I should show the position of the bars after each run. How do i go on about doing that?

Alishaikh
Light Poster
25 posts since Jan 2008
Reputation Points: 7
Solved Threads: 0
 

I didn't post the file sorry. It is attached below. Do I have to make changes to this program? or can I write a driver one?

Attachments Sorts.java (2.82KB)
Alishaikh
Light Poster
25 posts since Jan 2008
Reputation Points: 7
Solved Threads: 0
 

Would you mind putting your code directly here in code tags ?

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 
I have attached the file with the sorts. I have to show 2 sorts insertion, and selection sorts demonstration in bars. I should show the position of the bars after each run. How do i go on about doing that?

What do you mean when you say:
I have to show 2 sorts insertion, and selection sorts demonstration inbars.

What is this bars?

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

hii
try to make your question more clear until we can help you with your problem.
good luck

hypocrisy
Newbie Poster
6 posts since Jan 2009
Reputation Points: 10
Solved Threads: 1
 

Well I have to show the sort go through the array. Like every time it goes through the array, in an applet

Alishaikh
Light Poster
25 posts since Jan 2008
Reputation Points: 7
Solved Threads: 0
 

You can create an array with some numbers inside.
>Then print its values,
>Call the sort method
>Print again the values.

Also if you want to see how the sort progresses you can change the methods given and add some :
>System.out.printlns after each loop is run.
Every time a loop of the method is executed print the values. (You will need another for-loop) to print the values.

As for applets have a textarea where you print the values of the array in each line. Check the API for the JTextArea class. Also you probably have some notes on how to create an applet.


What I can't understand is why you where given the code ready and asked to do it in applet. Usually applets are more "advanced" than a simple sort method. How can your teacher expect you to write an applet when you haven't even learned the simple sort algorithms since he is handing them to you ?

I believe that you should first learn how to write these sort algorithms before being asked to do them in applets

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

No no, I wrote the sorts in an earlier program. But I cant get it to print after every loop. This is what I have so far(I decided to forgo the applet, and do this first, and its due within the next hour. I'm in school right now)
BUT it wont work at all....PLease help

Attachments Array.java (0.45KB) Sorts.java (3.1KB)
Alishaikh
Light Poster
25 posts since Jan 2008
Reputation Points: 7
Solved Threads: 0
 

You don't say what doesn't work and what errors you get.

Also it would be a good idea to print the values in 1 line:

for (int scan = index+1; scan < numbers.length; scan++){
           if (numbers[scan] < numbers[min])
               min = scan;
           for(int i=0;i<numbers.length;i++) {
			 System.out.print(numbers[i]+", ");
          }
          System.out.println();
         }
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

I made new code!!! This is an applet, but I dont know how to keep displaying the bars after each loop. PLease tell me how I can do that.

/**
 * @(#)SelectionBars.java
 *
 * SelectionBars Applet application
 *
 * @author 
 * @version 1.00 2009/1/8
 */

import java.awt.event.*; 
import java.util.Random; 
import javax.swing.*; 
import java.awt.*;
import java.applet.*;

public class SelectionBars extends Applet {
//Variables and arrays 
	Rectangle[] bar_array = new Rectangle[10]; 
	int minIndex; 
public void init() {
		final int NUM_BARS = 10, WIDTH = 30, MAX_HEIGHT = 300, GAP = 9; 
		int x = GAP, height; 
		bar_array = new Rectangle[10]; 

		Random generator = new Random(); 
		height = generator.nextInt(MAX_HEIGHT) + 1; 
		
		for (int i = 0; i < NUM_BARS; i++) 
		{ 
		bar_array[i] = new Rectangle(x, MAX_HEIGHT-height, WIDTH, height); 
		height = generator.nextInt(MAX_HEIGHT) + 1; 
		x = x + WIDTH + GAP; 
			}
			
}		
void selectionSort() 
{ 
for( int i = 0; i < bar_array.length - 1; i++ ) 
{ 
int minIndex = i; 
for( int j = i + 1; j < bar_array.length; j++ ) 
{ 
if( bar_array[j].height < bar_array[minIndex].height ) 
{ 
minIndex = j; 
} 
} 

if(minIndex > i) 
{ 
Rectangle temp = bar_array[i]; 
bar_array[i] = bar_array[minIndex]; 
bar_array[minIndex] = temp; 

int tempX = bar_array[i].x; 
bar_array[i].x = bar_array[minIndex].x; 
bar_array[minIndex].x = tempX; 
} 
} 
} 
	
	
	
	
	
	
	
	
public void paint (Graphics page) 
{ 
final int GAP = 9; 
int x; 
setBackground (Color.black); 

page.setColor (Color.blue); 
x = GAP; 

for (int i = 0; i < bar_array.length; i++) 
{ 
Dimension d = bar_array[i].getSize(); 
Point p = bar_array[i].getLocation(); 
page.fillRect(p.x, p.y, d.width, d.height); 

x = x + WIDTH + GAP; 
} 

}

}
Alishaikh
Light Poster
25 posts since Jan 2008
Reputation Points: 7
Solved Threads: 0
 

Applets are not my strong point.
But I advised you to use a JTextArea and display in each line the elements of the array.
Check the API of JTextArea on how to append and set text

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

But I dont need text. I just need to know how to use the sort in the applet

Alishaikh
Light Poster
25 posts since Jan 2008
Reputation Points: 7
Solved Threads: 0
 
But I dont need text. I just need to know how to use the sort in the applet

If you want to use it, just call it and sort an array.
But how would you show to the GUI the results? Aren't you supposed to print them somewhere?

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You