Tuesday, 31 January 2012

Sorting using method

  1. declare an array of size 5. having name a.
  2. take input by writing a[b]=EasyIn........ in it using loop having variable name b starting from 0 to a.length and increment by 1
  3. pass this array to the the method having name methsort like methsort(a);
  4. write closing curly braces of main method.
  5. write method like public static void methsort(int[] x)
  6. write starting curly braces of method methsort.
  7. initialize a variable e=0;
  8. start a loop having variable c from 0 to less then 4 and then write starting curly braces of it
  9. write an other loop having variable name d from 0 to less then 4 and then write starting curly braces of it
  10. write if(x[d]>x[d+1]) and write starting curly braces of if 
  11. with in the body of if write e=x[d];x[d]=x[d+1];x[d+1]=e; write closing curly braces of if, second loop and first loop.
  12. For the output write an other loop having variable name f from 0 to 4 and increment by 1. and with in the body of this loop  write output statement ..........print("  "+x[f]);
  13. write closing curly brace of this loop.
 Note: This code will produce the numbers in ascending order and for descending order replace greater then sign with less then sign in point 10 if condition.

No comments:

Post a Comment