Wednesday, October 20, 2021

Selection Sort in Java

 Machine Problem

 Write a Java program that will perform selection sort algorithm by asking the user to give a number and then it is display the original arrangement of numbers, and its sorted values.

 I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.





Program Listing

package test;

import java.util.Scanner;  

/*
 * Selection_Sort.java
 * 
 * Jake Rodriguez Pomperada, MAED-IT, MIT
 * www.jakerpomperada.com  and www.jakerpomperada.blogspot.com
 * jakerpomperada@gmail.com
 * Bacolod City, Negros Occidental Philippines
 * 
 * Machine Problem
 * 
 * Write a Java program that will perform selection sort algorithm by
 * asking the user to give a number and then it is display the original
 * arrangement of numbers, and its sorted values.
 * 
 */

public class Selection_Sort {
  public static void main(String args[])  
   {  
       int size, i, j, temp;  
       int arr[] = new int[50];  
       
       @SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);  
       
       System.out.println();
       System.out.println("\tSelection Sort in Java");
       System.out.println();
         
       System.out.print("Enter Array Size : ");  
       size = scan.nextInt();  
         
       
       for(i=0; i<size; i++)  
       {  
       System.out.print("Enter Element Value No." + (i+1) + ": "); 
       arr[i] = scan.nextInt();  
       }  
         
       System.out.println();
       System.out.print("UnSorted Array Values : \n");  
       System.out.println();
       for(i=0; i<size; i++)  
       {  
           System.out.print(arr[i]+ "  ");  
       }  
       
       System.out.println();
         for(i=0; i<size; i++)  
       {  
           for(j=i+1; j<size; j++)  
           {  
               if(arr[i] > arr[j])  
               {  
                   temp = arr[i];  
                   arr[i] = arr[j];  
                   arr[j] = temp;  
               }  
           }  
       }  
         
       System.out.println();
       System.out.print("Sorted Array Values : \n");  
       System.out.println();
       for(i=0; i<size; i++)  
       {  
           System.out.print(arr[i]+ "  ");  
       }  
       
       System.out.println("\n\n");
       System.out.print("End of Program \n");  
       System.out.println();
       scan.close();
   }  
}  

No comments:

Post a Comment