I have written a program for selection sort(I think).But I get the input I entered as the output. Please help me with what is wrong?
Code:
Output:package selecsort; import java.io.*; import java.util.*; public class Selecsort { int i,n,result; int arr[]=new int[20]; void get() { Scanner rd=new Scanner(System.in); System.out.println("Enter the number of elements"); n=rd.nextInt(); System.out.println("Enter the elements of the array"); for(i=0;i<n;i++) arr[i]=rd.nextInt(); } int biggie(int consize) { int max = 0; int max1=0; for(i=0;i<consize;i++) { if(arr[i]>max1) { max1=arr[i]; max=i; } } return max; } void sor() { int j; int consize=n; for(j=n;j>0;j--) { result=biggie(consize); swap(arr[i],arr[result]); consize--; } System.out.println("The sorted array is"); for(i=0;i<n;i++) System.out.println(+arr[i]); } void swap(int x,int y) { int temp1; temp1=x; x=y; y=temp1; } public static void main(String[] args) { Selecsort se=new Selecsort(); se.get(); se.sor(); } }
Enter the number of elements
3
Enter the elements of the array
3
45
15
The sorted array is
3
45
15