Tuesday, December 24, 2019
Friday, December 20, 2019
Write a Java Program to sort the elements of an array in descending order
Java Program to sort the elements of an array in descending order
In this program, we need to sort the given array in descending order such that elements will be arranged from largest to smallest. This can be achieved through two loops. The outer loop will select an element, and inner loop allows us to compare selected element with rest of the elements.

Elements will be sorted in such a way that largest element will appear on extreme left which in this case is 8. The smallest element will appear on extreme right which in this case is 1.
Algorithm
- STEP 1: START
- STEP 2: INITIALIZE arr[] ={5, 2, 8, 7, 1 }.
- STEP 3: SET temp =0
- STEP 4: PRINT "Elements of Original Array"
- STEP 5: REPEAT STEP 6 UNTIL i<arr.length
//for(i=0; i<arr.length; i++) - STEP 6: PRINT arr[i]
- STEP 7: REPEAT STEP 8 to STEP 9 UNTIL i<arr.length
//for(i=0; i<arr.length; i++ ) - STEP 8: REPEAT STEP 9 UNTIL j<arr.length
//for(j=i+1;j<arr.length;j++) - STEP 9: if(arr[i]<arr[j]) then
temp = arr[i]
arr[i]=arr[j]
arr[j]=temp - STEP 10: PRINT new line
- STEP 11: PRINT "Elements of array sorted in descending order"
- STEP 12: REPEAT STEP 13 UNTIL i<arr.length
//for(i=0;i<arr.length;i++) - STEP 13: PRINT arr[i]
- STEP 14: END
Program:
- public class SortDsc {
- public static void main(String[] args) {
- //Initialize array
- int [] arr = new int [] {5, 2, 8, 7, 1};
- int temp = 0;
- //Displaying elements of original array
- System.out.println("Elements of original array: ");
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + " ");
- }
- //Sort the array in descending order
- for (int i = 0; i < arr.length; i++) {
- for (int j = i+1; j < arr.length; j++) {
- if(arr[i] < arr[j]) {
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- System.out.println();
- //Displaying elements of array after sorting
- System.out.println("Elements of array sorted in descending order: ");
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + " ");
- }
- }
- }
Output:
Elements of original array: 5 2 8 7 1 Elements of array sorted in descending order: 8 7 5 2 1
Write a Java Java Program to sort the elements of an array in ascending order
Java Program to sort the elements of an array in ascending order
In this program, we need to sort the given array in ascending order such that elements will be arranged from smallest to largest. This can be achieved through two loops. The outer loop will select an element, and inner loop allows us to compare selected element with rest of the elements.

Elements will be sorted in such a way that the smallest element will appear on extreme left which in this case is 1. The largest element will appear on extreme right which in this case is 8.
Algorithm
- STEP 1: START
- STEP 2: INITIALIZE arr[] ={5, 2, 8, 7, 1 }.
- STEP 3: SET temp =0
- STEP 4: PRINT "Elements of Original Array"
- STEP 5: REPEAT STEP 6 UNTIL i<arr.length
//for(i=0; i<arr.length; i++) - STEP 6: PRINT arr[i]
- STEP 7: REPEAT STEP 8 to STEP 9 UNTIL i<arr.length
//for(i=0; i<arr.length; i++ ) - STEP 8: REPEAT STEP 9 UNTIL j<arr.length
//for(j=i+1;j<arr.length;j++) - STEP 9: if(arr[i]>arr[j]) then
temp = arr[i]
arr[i]=arr[j]
arr[j]=temp - STEP 10: PRINT new line
- STEP 11: PRINT "Elements of array sorted in ascending order"
- STEP 12: REPEAT STEP 13 UNTIL i<arr.length
//for(i=0;i<arr.length;i++) - STEP 13: PRINT arr[i]
- STEP 14: END
Program:
- public class SortAsc {
- public static void main(String[] args) {
- //Initialize array
- int [] arr = new int [] {5, 2, 8, 7, 1};
- int temp = 0;
- //Displaying elements of original array
- System.out.println("Elements of original array: ");
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + " ");
- }
- //Sort the array in ascending order
- for (int i = 0; i < arr.length; i++) {
- for (int j = i+1; j < arr.length; j++) {
- if(arr[i] > arr[j]) {
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- System.out.println();
- //Displaying elements of array after sorting
- System.out.println("Elements of array sorted in ascending order: ");
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + " ");
- }
- }
- }
Output:
Elements of original array: 5 2 8 7 1 Elements of array sorted in ascending order: 1 2 5 7 8
Write a Java Program to print the largest element in an array
Program to print the largest element in an array
In this program, we need to find out the largest element present in the array and display it. This can be accomplished by looping through the array from start to end by comparing max with all the elements of an array. If any of element is greater than max, then store a value of the element in max. Initially, max will hold the value of the first element. At the end of the loop, max represents the largest element in the array.

In the above array, initially, max will hold the value 25. In the 1st iteration, max will be compared with 11, since 11 is less than max. Max will retain its value. In the next iteration, it will be compared to 7, 7 is also less than max, no change will be made to the max. Now, max will be compared to 75. 75 is greater than max so that max will hold the value of 75. Continue this process until the end of the array is reached. At the end of the loop, max will hold the largest element in the array.
Algorithm
- STEP 1: START
- STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56}
- STEP 3: max = arr[0]
- STEP 4: REPEAT STEP 5 for(i=0; i< arr.length; i++)
- STEP 5: if(arr[i]>max) max=arr[i]
- STEP 6: PRINT "Largest element in given array:"
- STEP 7: PRINT max
- STEP 8: END
Program:
- public class LargestElement_array {
- public static void main(String[] args) {
- //Initialize array
- int [] arr = new int [] {25, 11, 7, 75, 56};
- //Initialize max with first element of array.
- int max = arr[0];
- //Loop through the array
- for (int i = 0; i < arr.length; i++) {
- //Compare elements of array with max
- if(arr[i] > max)
- max = arr[i];
- }
- System.out.println("Largest element present in given array: " + max);
- }
- }
- Output:
Largest element present in given array: 7
Write a java Program to print the elements of an array in reverse order
Program to print the elements of an array in reverse order
In this program, we need to print the elements of the array in reverse order that is; the last element should be displayed first, followed by second last element and so on.

Above array in reversed order:

Algorithm
- STEP 1: START
- STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}
- STEP 3: PRINT "Original Array:"
- STEP 4: REPEAT STEP 5 for(i=0; i<arr.length ; i++)
- STEP 5: PRINT arr[i]
- STEP 6: PRINT "Array in reverse order"
- STEP 7: REPEAT STEP 8 for(i= arr.length-1; i>=0; i--)
- STEP 8: PRINT a[i]
- STEP 9: END
Program:
- public class ReverseArray {
- public static void main(String[] args) {
- //Initialize array
- int [] arr = new int [] {1, 2, 3, 4, 5};
- System.out.println("Original array: ");
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + " ");
- }
- System.out.println();
- System.out.println("Array in reverse order: ");
- //Loop through the array in reverse order
- for (int i = arr.length-1; i >= 0; i--) {
- System.out.print(arr[i] + " ");
- }
- }
- }
Output:
Original array: 1 2 3 4 5 Array in reverse order: 5 4 3 2 1
Subscribe to:
Posts (Atom)