- package SolveProblem;//Package name---------
- import java.util.Scanner;
- public class SelectionSort {
- public static void main(String[] args) {
- Scanner myScanner=new Scanner(System.in);
- //-----------------input----------------
- System.out.print("Enter how many number : ");
- int n=myScanner.nextInt();
- int[] arr=new int[n];
- int i;
- System.out.print("Enter "+n+" numbers : ");
- for (i = 0; i < n; i++) {
- arr[i]=myScanner.nextInt();
- }
- int pos,temp,j,k;
- // -----------sort start-------------
- for (k = 1; k < n; k++) {
- pos=k-1;
- for (j = k; j < n; j++) {
- if (arr[pos]>arr[j]) {
- pos=j;
- }
- }
- //-----------when position in not equal with k-1 then interchange--------------
- if(pos!=k-1){
- temp=arr[k-1];
- arr[k-1]=arr[pos];
- arr[pos]=temp;
- }
- }
- // ----------------------output------------------
- System.out.print("Sorted list is : ");
- for (i = 0; i <n; i++) {
- System.out.print(arr[i]+"\t");
- }
- System.out.println();
- }
- }










No comments:
Post a Comment