{ /* opening braces for class problem1 */
int[] a; /* declaring an array of integer */
int nElems; /* declaring an variable of data type integer */
public void ArrayBub(int max) /* ArrayBub is defining a new parameterised method */
{
a = new int[max]; /* to initialize the array with 'max' as size of the array */
}
public void insert(int value) /* defining the insert method to insert values in the array */
{
a[nElems] = value; /* assigning the value to array at current position */
nElems++; /* incrementing the position counter */
}
public void Sort() /* defining the method to sort the array */
{
int out, in; /* out & in are two variables of type integer */
for(out=nElems-1; out>1; out--) /* outer loop starting from lenght of array - 1 to last element */
/* when outer loop's condition become true then control pass to innner loop. inner loop will execute
till the condition of inner loop will be true. */
for(in=0; in
if( a[in] > a[in+1] ) /* checking condition Is element of a[in] is greater than a[in+1]*/
/* if condition is true swap the value by calling swap function .
if condition is false control goes to outer loop */
swap(in, in+1); /* calling method swap & passing arguments */
}
public void swap(int one, int two) /* creating method swap with two parameters of type integer */
{
long temp = a[one]; /* taking varibale temp & storing value of a[one] */
a[one] = a[two]; /* value of a[two] is stoder in a[one] */
a[two] = temp; /* now temp is stored in a[two]. value is swaped now and temp is free */
}
No comments:
Post a Comment