2:55 am - Friday July 20, 2018

Java Example Program for Selection sort

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

//example program for Selection sort

/**
*@author:candidjava.com
*@description:selectionSort()
*/
import java.io.*;

public class SelectionSort // creation of class
{
public static void main(String[] args) throws IOException {
int a;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the number of elements”);
int n = Integer.parseInt(br.readLine());
int ar[] = new int[n]; // creation of object ‘ar’ with ‘n’
for (a = 0; a < n; a++) {
System.out.println(“Enter ” + a + “th number”);
ar[a] = Integer.parseInt(br.readLine()); // get the values by using
// the array variable ar
}
// selection sort begins
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (ar[i] < ar[j]) // check the condition for sorting to swap
{
ar[i] = ar[j] + ar[i];
ar[j] = ar[i] – ar[j];
ar[i] = ar[i] – ar[j];
}
}
} // sorting finished
System.out.println(“The result of sorting array is”);
for (int i = 0; i < n; i++) {
System.out.println(ar[i]); // to print the sorted list
}
}
}


Output:

Enter the number of elements
6
Enter 0th number
4
Enter 1th number
7
Enter 2th number
8
Enter 3th number
9
Enter 4th number
1
Enter 5th number
5
The result of sorting array is
9
8
7
5
4
1

Filed in: Z-A All Java Codes

Comments are closed.