You are on page 1of 2

QUESTION 1: Improve the understandability of the below given ode:

import java.util.*; //importing the required packages import java.io.*;


class problem3 //creating a class
{
int[] numArray = new int[10]; //declaring an integer array of size 10 called numArray
public static voi increment!lements "int[] inte#erArray$ //defining the increment!lementst"#method
$ith one integer parameter
{
int arraylen = inte#erArray.len#t%; // initializing an integer varia%le arraylen $ith the length& i.e.
the no of elements in array
&or "int i = 0; i ' arraylen; i (($ //definition of for loop to print the current values of array
{
)ystem.out.println"inte#erArray[i]$; //printing the element at position or inde' (i)
*
&or "int i = 0; i ' arraylen; i (($ //definition of for loop to increment the current values of array
{
inte#erArray[i] = inte#erArray[i] ( 10; //add 10 to each value of array
*
&or "int i=0; i ' arraylen; i (($
{
)ystem.out.println"inte#erArray[i]$;
*
*
E!"#$N$TION:
+%en any element o& t%e inte#er array occurs, t%en it is printe &irst an a&ter t%at it is
incremente by 10 an t%en t%e resultant array is printe. -%e coe is pree&ine in t%e pac.a#e
an we want to e/ecute any application w%ic% re0uires t%e coe. +e on1t %ave to write t%e
w%ole coe as we can irectly import t%e pac.a#e an e/ecute t%at application.
QUESTION %: Improve the understandability of the below given ode:
class 2roblem1 //creating a ne$ class
{
int[] a; //declaring a ne$ integer varia%le a of array type to store the num%ers
int n!lems; //declaring an integer varia%le to hold the value of no. of elements in the array
public Array3ub"int ma/$
{
a = new int[ma/];
*
public voi insert"int value$ //defining the insert method to insert values in the array
{
a[n!lems] = value; //assigning the value to array at current position
n!lems((; //incrementing the position counter
*
public voi )ort"$ //defining the method to sort the array
{
int out, in; // declaring t$o integer varia%les out * in
&or"out=n!lems41; out51; out44$ //outer loop
&or"in=0; in'out; in(($ //inner loop
i&" a[in] 5 a[in(1] $ //conditional statement to compare the t$o values
swap"in, in(1$; //s$apping the t$o values in specified order
*
public voi swap"int one, int two$ //defining s$ap function to perform s$apping of elements
{
lon# temp = a[one]; //interchanging the values
a[one] = a[two];
a[two] = temp;
*
E!"#$N$TION:
-%is is a pro#ram &or 3ubble )ort. 6t is t%e simplest sortin# al#orit%m. 6n bubble sort al#orit%m,
t%e array is traverse &rom 0 to t%e len#t%41 ine/ o& t%e array an compare one element to t%e
ne/t element an swap values in between i& t%e ne/t element is less t%an t%e previous element. 6n
ot%er wors, bubble sortin# al#orit%m compare two values an put t%e lar#est value at lar#est
ine/. -%e al#orit%m &ollows t%e same steps an per&orms multiple passes until t%e values o& t%e
array are sorte. 3ubble sort %as worst case i& t%e elements are all in reverse orer. 6n worst4case
t%e comple/ity o& bubble sort is 7"n8$ an in best4case t%e comple/ity o& bubble sort is 7"n$.

You might also like