Pages

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.
Java Program to sort the elements of an array in descending order
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:

  1. public class SortDsc {    
  2.     public static void main(String[] args) {        
  3.         //Initialize array     
  4.         int [] arr = new int [] {52871};     
  5.         int temp = 0;    
  6.             
  7.         //Displaying elements of original array    
  8.         System.out.println("Elements of original array: ");    
  9.         for (int i = 0; i < arr.length; i++) {     
  10.             System.out.print(arr[i] + " ");    
  11.         }    
  12.             
  13.         //Sort the array in descending order    
  14.         for (int i = 0; i < arr.length; i++) {     
  15.             for (int j = i+1; j < arr.length; j++) {     
  16.                if(arr[i] < arr[j]) {    
  17.                    temp = arr[i];    
  18.                    arr[i] = arr[j];    
  19.                    arr[j] = temp;    
  20.                }     
  21.             }     
  22.         }    
  23.             
  24.         System.out.println();    
  25.             
  26.         //Displaying elements of array after sorting    
  27.         System.out.println("Elements of array sorted in descending order: ");    
  28.         for (int i = 0; i < arr.length; i++) {     
  29.             System.out.print(arr[i] + " ");    
  30.         }    
  31.     }    
  32. }    
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.
Java Program to sort the elements of an array in ascending order
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:

  1. public class SortAsc {    
  2.     public static void main(String[] args) {        
  3.             
  4.         //Initialize array     
  5.         int [] arr = new int [] {52871};     
  6.         int temp = 0;    
  7.             
  8.         //Displaying elements of original array    
  9.         System.out.println("Elements of original array: ");    
  10.         for (int i = 0; i < arr.length; i++) {     
  11.             System.out.print(arr[i] + " ");    
  12.         }    
  13.             
  14.         //Sort the array in ascending order    
  15.         for (int i = 0; i < arr.length; i++) {     
  16.             for (int j = i+1; j < arr.length; j++) {     
  17.                if(arr[i] > arr[j]) {    
  18.                    temp = arr[i];    
  19.                    arr[i] = arr[j];    
  20.                    arr[j] = temp;    
  21.                }     
  22.             }     
  23.         }    
  24.           
  25.         System.out.println();    
  26.             
  27.         //Displaying elements of array after sorting    
  28.         System.out.println("Elements of array sorted in ascending order: ");    
  29.         for (int i = 0; i < arr.length; i++) {     
  30.             System.out.print(arr[i] + " ");    
  31.         }    
  32.     }    
  33. }    
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.
Program to print the largest element in an 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

  1. STEP 1: START
  2. STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56}
  3. STEP 3: max = arr[0]
  4. STEP 4: REPEAT STEP 5 for(i=0; i< arr.length; i++)
  5. STEP 5: if(arr[i]>max) max=arr[i]
  6. STEP 6: PRINT "Largest element in given array:"
  7. STEP 7: PRINT max
  8. STEP 8: END

Program:

  1. public class LargestElement_array {  
  2.     public static void main(String[] args) {  
  3.   
  4.         //Initialize array  
  5.         int [] arr = new int [] {251177556};  
  6.         //Initialize max with first element of array.  
  7.         int max = arr[0];  
  8.         //Loop through the array  
  9.         for (int i = 0; i < arr.length; i++) {  
  10.             //Compare elements of array with max  
  11.            if(arr[i] > max)  
  12.                max = arr[i];  
  13.         }  
  14.         System.out.println("Largest element present in given array: " + max);  
  15.     }  
  16. }
  17. 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.
Program to print the elements of an array in reverse order
Above array in reversed order:
Program to print the elements of an array in reverse 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:

  1. public class ReverseArray {  
  2.     public static void main(String[] args) {  
  3.         //Initialize array  
  4.         int [] arr = new int [] {12345};  
  5.         System.out.println("Original array: ");  
  6.         for (int i = 0; i < arr.length; i++) {  
  7.             System.out.print(arr[i] + " ");  
  8.         }  
  9.         System.out.println();  
  10.         System.out.println("Array in reverse order: ");  
  11.         //Loop through the array in reverse order  
  12.         for (int i = arr.length-1; i >= 0; i--) {  
  13.             System.out.print(arr[i] + " ");  
  14.         }  
  15.     }  
  16. }  
Output:
 Original array: 
1 2   3   4   5
Array in reverse order:
5    4   3   2   1