JAVA: I need to create a program that outputs the original list and the sorted l
ID: 3668037 • Letter: J
Question
JAVA: I need to create a program that outputs the original list and the sorted list in ascending order. I am having difficulty with the paint function. The program repaints automatcially and outputs the sorted list both times. here is what i have.
package sort;
import java.awt.Graphics;
import java.applet.Applet;
public class Sort extends Applet{
int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 };
int b[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 };
public void paint(Graphics g){
print(g,"Data items in original orer",a,25,25);
sort(a);
print(g,"Data items in ascending order",a,25,55);
}
//Function to sort the array into ascending order.
public void sort(int b[]){
int i,j,t;
for(i=0;i<b.length;i++){
for(j=0;j<b.length;j++){
if(b[i]<b[j]){
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
}
public void print(Graphics g, String head, int b[], int x, int y){
g.drawString(head,x,y);
x+=15;
y+=15;
for(int i=0;i<b.length;i++){
g.drawString(String.valueOf(b[i]),x,y);
x+=20;
}
}
}
Explanation / Answer
Can help you with this:
public void sort()
{
int i,j, min,temp=0;
for(i=0;i<a.length;i++)
{
min = i;
for(j=i+1;j<a.length;j++)
{
if (a[j]<a[min])
{
min = j;
}
}
temp = a[i];
a[i] = a[min];
a[min]=temp;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.