Thursday, April 19, 2018

Write a program to sort numeric array !!

// Method-I:
  1. public class SortIntegerArrayAlgorithm {
  2. public static void main(String[] args) {
  3. int a[] = { 2, 3, 4, 5, 22, 13, 5, 3 };
  4. int temp;
  5. for (int i = 0; i < a.length; i++) {
  6. for (int j = 0; j < a.length - 1; j++) {
  7. if (a[j] > a[j + 1]) {
  8. temp = a[j];
  9. a[j] = a[j + 1];
  10. a[j + 1] = temp;
  11. }
  12. }
  13. }
  14. for (int i : a) {
  15. System.out.print(i + " ");
  16. }
  17. }
  18. }
Output: 2 3 3 4 5 5 13 22
Note: This is also called Bubble sort algorithm.

// Method-II:
  1. public class SortIntArrayAlgorithm {

  2. public static void main(String[] args) {
  3. int a[] = { 1, 43, 4, 43, 33, 2, 2, 5, 5, 5, 5, 33, 777 };
  4. Arrays.sort(a);
  5. for (int i : a) {
  6. System.out.print(i + " ");
  7. }
  8. }
  9. }
Output: 1 2 2 4 5 5 5 5 33 33 43 43 777 
Note: Sorting has been done using pre-defined method not our own algorithm.

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...