Bubble sort
Wed Oct 09 2024 18:18:22 GMT+0000 (Coordinated Universal Time)
Saved by
@wayneinvein
import java.util.Scanner;
public class P1 {
public static void print(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();
int arr[] = new int[n];
System.out.println("Enter the array elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int temp;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Sorted array in ascending order:");
print(arr);
scanner.close();
}
}
content_copyCOPY
Comments