Valencia, Gian Kurt D. CEIT-37-302-A: Code
Valencia, Gian Kurt D. CEIT-37-302-A: Code
Valencia, Gian Kurt D. CEIT-37-302-A: Code
CEIT-37-302-A
Code:
import java.util.Arrays;
public class Sorting
{
public static void main(String[]args)
{
int Sort[]={10,5,8,9,24,50,1,17};
Arrays.sort(Sort);
Code:
import java.util.Scanner;
public class SortingDescending
{
public static void main(String[] args)
{
int n, bbq;
Scanner scan = new Scanner(System.in);
System.out.print("Number of Elements: ");
n = scan.nextInt();
int a[] = new int[n];
System.out.println("Input Elements: ");
for (int i = 0; i < n; i++)
{
a[i] = scan.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
bbq = a[i];
a[i] = a[j];
a[j] = bbq;
}
}
}
System.out.print("Descending Order: ");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
Output:
Sorting Methods
1. Bubble Sort
= works by swapping adjacent elements if they're not in the desired order. This process
repeats from the beginning of the array until all elements are in order
2. Insertion Sort
= dividing the array into the sorted and unsorted subarrays
3. Selection Sort
= Selection Sort also divides the array into a sorted and unsorted subarray. Though, this time,
the sorted subarray is formed by inserting the minimum element of the unsorted subarray at
the end of the sorted array, by swapping