Sie sind auf Seite 1von 2

import java.util.

Scanner;

public class sorting {

public static void main(String[] args) {


// TODO Auto-generated method stub++
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{/
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
0 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.print("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.println(a[n - 1]);

int n1, temp1;


Scanner s1 = new Scanner(System.in);
System.out.println("Enter no. of elements you want in array:");
n1 = s1.nextInt();
int a1[] = new int[n1];
System.out.println("Enter all the elements:");
for (int x = 0; x < n1; x++)
{
a1[x] = s1.nextInt();
}
for (int x = 0; x < n1; x++)
{
for (int y = x + 1; y < n1; y++)
{
if (a1[x] < a1[y])
{
temp1 = a1[x];
a1[x] = a1[y];
a1[y] = temp1;
}
}
}

System.out.print("Descending Order:");
for (int i = 0; i < n1 - 1; i++)
{
System.out.print(a1[i] + ",");
}
System.out.print(a1[n1 - 1]);
}
}

Das könnte Ihnen auch gefallen