Tech Study

Write a Java Program to Sort the Array in an Ascending Order

Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
public class Javaexcercise 
{
    public static void main(String[] args) 
    {
        int num, temp;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements: ");
        num = s.nextInt();
        int a[] = new int[num];
        System.out.println("Enter all the elements:");
        for (int i = 0; i < num; i++) 
        {
            a[i] = s.nextInt();
        }
        for (int i = 0; i < num; i++) 
        {
            for (int j = i + 1; j < num; j++) 
            {
                if (a[i] > a[j]) 
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.print("Ascending Order:");
        for (int i = 0; i < num - 1; i++) 
        {
            System.out.print(a[i] + ",");
        }
        System.out.print(a[num - 1]);
    }
}
 

Result

Write a Java Program to Sort the Array in an Ascending Order
Write a Java Program to Sort the Array in an Ascending Order

TaggedWrite a Java Program to Sort the Array in an Ascending Order

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more