// Method-I:
Note: This is also called Bubble sort algorithm.
// Method-II:
- public class SortIntegerArrayAlgorithm {
- public static void main(String[] args) {
- int a[] = { 2, 3, 4, 5, 22, 13, 5, 3 };
- int temp;
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a.length - 1; j++) {
- if (a[j] > a[j + 1]) {
- temp = a[j];
- a[j] = a[j + 1];
- a[j + 1] = temp;
- }
- }
- }
- for (int i : a) {
- System.out.print(i + " ");
- }
- }
- }
Note: This is also called Bubble sort algorithm.
// Method-II:
- public class SortIntArrayAlgorithm {
- public static void main(String[] args) {
- int a[] = { 1, 43, 4, 43, 33, 2, 2, 5, 5, 5, 5, 33, 777 };
- Arrays.sort(a);
- for (int i : a) {
- System.out.print(i + " ");
- }
- }
- }
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