Question: Using the following Java program, modify the code so that every time you run your program, it generates random numbers for your array, and then

Using the following Java program, modify the code so that every time you run your program, it generates random numbers for your array, and then prints it (insertion sort)

import java.awt.Graphics; import java.applet.Applet;

public class SortingProg extends Applet { int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 };

public void paint(Graphics g) { print(g,"Data items in original order",a,25,25);

sort();

print(g,"Data items in ascending order",a,25,55); }

public void sort() {

int n = a.length;

for (int i=1; i

{

int key = a[i];

int j = i-1;

/* Move the elements of the array that are greater than the key, to one position ahead of their current position, thus properly sorting through the elements. */

while (j>=0 && a[j] > key)

{

a[j+1] = a[j];

j = j-1;

}

a[j+1] = key;

}

}

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!