C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Selection Sort Program - Java Programs

Define a class pincode and store the given pin codes in a single dimensional array. Sort these pin codes in ascending order using the selection sort technique only. Display the sorted array.


110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033

class pincode{ public static void main(String[] args){ int a[] = {110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033}; for(int i = 0; i < a.length; i++){ int small = a[i]; int pos = i; for(int j = i + 1; j < a.length; j++){ if(small > a[j]){ small = a[j]; pos = j; } } int temp = a[i]; a[i] = small; a[pos] = temp; } for(int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } }

Output

 
 OUTPUT : 
110001 110006 110019 110023 110029 110033 110055 110061