Sponsered Links
Categories
Sponsered Links

Write a program TO INPUT 1 Dimensional ARRAY & DO SORTING USING SELECTION SORT.

Write a program TO INPUT 1 Dimensional ARRAY & DO SORTING USING SELECTION SORT.

import java.io.*;

class THIRTEEN{
    public static void selectionSort(int[] x) {
        int temp;
        for (int i=0; i<x.length-1; i++) {
            for (int j=i+1; j<x.length; j++) {
                if (x[i] > x[j]) {
                    temp = x[i];
                    x[i] = x[j];
                    x[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) throws Exception{
        int numbers[]=new int[5];
        System.out.println("Enter 5 numbers to store into array:");
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        for(int i=0;i<numbers.length;i++){
            String input = reader.readLine();
            int num=Integer.parseInt(input);
            numbers[i]=num;
        }
        System.out.println("Sorted Array is: ");
        selectionSort(numbers);
        for(int i=0;i<numbers.length;i++){
            System.out.println(numbers[i]);
        }
    }
}

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved