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?

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       Author: Lewis/Loftus/Cocking
//
//  Demonstrates the selection sort and insertion sort algorithms,
//  as well as a generic object sort.
//********************************************************************

public class Sorts
{
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the selection
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void selectionSort (int[] numbers)
   {
      int min, temp;

      for (int index = 0; index < numbers.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < numbers.length; scan++)
            if (numbers[scan] < numbers[min])
               min = scan;

         // Swap the values
         temp = numbers[min];
         numbers[min] = numbers[index];
         numbers[index] = temp;
      }
   }

   public static void selectionSort (Comparable[] objects)
   {
      int min;
      	Comparable temp;

      for (int index = 0; index < objects.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < objects.length; scan++)
            if (objects[scan].compareTo(objects[min])<0)
               min = scan;

         // Swap the values
         temp = objects[min];
         objects[min] = objects[index];
         objects[index] = temp;
      }
   }
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (int[] numbers)
   {
      for (int index = 1; index < numbers.length; index++)
      {
         int key = numbers[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && numbers[position-1] > key)
         {
            numbers[position] = numbers[position-1];
            position--;
         }
            
         numbers[position] = key;
      }
   }

   //-----------------------------------------------------------------
   //  Sorts the specified array of objects using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (Comparable[] objects)
   {
      for (int index = 1; index < objects.length; index++)
      {
         Comparable key = objects[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && objects[position-1].compareTo(key) > 0)
         {
            objects[position] = objects[position-1];
            position--;
         }
            
         objects[position] = key;
      }
   }
}

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

Attachments
//********************************************************************
//  Sorts.java       Author: Lewis/Loftus/Cocking
//
//  Demonstrates the selection sort and insertion sort algorithms,
//  as well as a generic object sort.
//********************************************************************

public class Sorts
{
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the selection
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void selectionSort (int[] numbers)
   {
      int min, temp;

      for (int index = 0; index < numbers.length-1; index++)
      {
         min = index;
         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.println(numbers[i]);
         }
         // Swap the values
         temp = numbers[min];
         numbers[min] = numbers[index];
         numbers[index] = temp;
         for(int i=0;i<numbers.length;i++)
          System.out.println(numbers[i]);
      }
   }

   public static void selectionSort (Comparable[] objects)
   {
      int min;
      	Comparable temp;

      for (int index = 0; index < objects.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < objects.length; scan++)
            if (objects[scan].compareTo(objects[min])<0)
               min = scan;

         // Swap the values
         temp = objects[min];
         objects[min] = objects[index];
         objects[index] = temp;
      }
   }
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (int[] numbers)
   {
      for (int index = 1; index < numbers.length; index++)
      {
         int key = numbers[index];
         int position = index;
		 System.out.println(numbers);
         // shift larger values to the right
         while (position > 0 && numbers[position-1] > key)
         {
            numbers[position] = numbers[position-1];
            position--;
             System.out.println(numbers);
         }
            
         numbers[position] = key;
      }
       System.out.println(numbers);
   }

   //-----------------------------------------------------------------
   //  Sorts the specified array of objects using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (Comparable[] objects)
   {
      for (int index = 1; index < objects.length; index++)
      {
         Comparable key = objects[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && objects[position-1].compareTo(key) > 0)
         {
            objects[position] = objects[position-1];
            position--;
         }
            
         objects[position] = key;
      }
   }
}

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

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?

This article has been dead for over six months. Start a new discussion instead.