Sorting dynamic array

Create dynamic array, initialized element and display the sorted value

package com.cj.jtwj.array;
import java.io.*;

public class DynArraySort {

    public static void main(String args[]) throws Exception {
        int n;
        int[] a = new int[10];
        int temp = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter no of elements:");
        n = Integer.parseInt(br.readLine());
        System.out.println("Enter the elements");
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }

        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (a[i] > a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }

        System.out.println("Sorted Order");

        for (int i = 0; i < n; i++) {
            System.out.println(a[i]);
        }
    }
}
 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

2,377` Total Views

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *


six × = 30