Using Java complete the following sorting method and a main

Using Java, complete the following sorting method and a main function to test 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 their original order\",a,25,25);

     sort();

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

   }


/* Complete the sorting method here. */

      

     

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;

   }

   }

}

Solution

Hi, Please find my implementation.

It is working fine.Please let me know in case of any issue.

import java.awt.Graphics;

import java.applet.Applet;

public class SortingProg extends Applet

{

   /**

   *

   */

   private static final long serialVersionUID = 1L;

   int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 };

   public void paint(Graphics g)

   {

       print(g,\"Data items in their original order\",a,25,25);

       sort();

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

   }

   /* Complete the sorting method here. */

   public void sort(){

       int n = a.length;

for (int i=1; i<n; ++i)

{

int key = a[i];

int j = i-1;

/* Move elements of arr[0..i-1], that are

   greater than key, to one position ahead

   of their current position */

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<b.length;i++)

       {

           g.drawString(String.valueOf(b[i]),x,y);

           x+=20;

       }

   }

}

Using Java, complete the following sorting method and a main function to test it...(insertion sort) import java.awt.Graphics; import java.applet.Applet; public
Using Java, complete the following sorting method and a main function to test it...(insertion sort) import java.awt.Graphics; import java.applet.Applet; public
Using Java, complete the following sorting method and a main function to test it...(insertion sort) import java.awt.Graphics; import java.applet.Applet; public

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site