sorting.java
32 linesjava
DOWNLOAD
1// Aim: Program to perform Array operations (Sorting).
2import java.util.*; 
3public class sorting {
4    public static void main(String[] args) {
5        Scanner sc = new Scanner(System.in);
6        int arr[] = new int[30];
7        System.out.print("Enter the number of elements: ");
8        int n = sc.nextInt();
9        System.out.print("Enter the elements: ");
10        for(int i=0; i<n; i++) {
11            arr[i] = sc.nextInt();
12        }
13        System.out.println("Original array: ");
14        for(int i=0; i<n; i++) {
15            System.out.print(arr[i] + " ");
16        }
17        for(int i=0; i<n; i++) {
18            for(int j=i+1; j<n; j++) {
19                if(arr[i] > arr[j]) {
20                    int temp = arr[i];
21                    arr[i] = arr[j];
22                    arr[j] = temp;
23                }
24            }
25        }
26        System.out.println("Sorted array: ");
27        for(int i=0; i<n; i++) {
28            System.out.print(arr[i] + " ");
29        }
30    }
31}
32