Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Test
- {
- public static void main(String args[])
- {
- Scanner obj =new Scanner(System.in);
- System.out.println("Enter the number of elements in the array ");
- int len=obj.nextInt();
- System.out.println("Enter the elements of the array ");
- int arr[]=new int[len];
- for(int i=0;i<arr.length;i++)//here the elements are being entered into the array
- {
- arr[i]=obj.nextInt();
- }
- SelectionSort obj1=new SelectionSort();
- obj1.selection(arr);
- }
- }
- class SelectionSort
- {
- void selection(int arr[])
- {
- int pos,temp;
- for(int i=0;i<arr.length;i++)//sorting algorithm
- {
- pos=smallest(i,arr);
- if(arr[pos]<arr[i])
- {
- temp=arr[pos];
- arr[pos]=arr[i];
- arr[i]=temp;
- }
- }
- System.out.println("the array in the sorted order is :");
- for(int i =0;i<arr.length;i++)
- {
- System.out.println(arr[i]);
- }
- }
- int smallest(int i,int arr[]) // in this function the smallest element is calculated
- {
- int small=arr[i],pos=i;
- for(int k=i+1;k<arr.length;k++)
- {
- if(arr[k]<small)
- {
- small=arr[k];
- pos=k;
- }
- }
- return pos;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement