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?

Recommended Answers

All 12 Replies

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?

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

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 in bars.

What is this bars?

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

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

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

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

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();
         }

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; 
} 

}

}

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

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.