3 May 2024 Shift 1 — Sorting Algorithm (Bubble Sort)
E · easyP · Verified PYQarrayssorting
Problem
Sort an array of N integers in ascending order using Bubble Sort. Input: First line = N. Second line = N space-separated integers.
Example
Input
5 64 25 12 22 11
Output
11 12 22 25 64
Outer loop N-1 times. Inner loop compares adjacent pairs. Swap if arr[i]>arr[i+1]. Stop early if no swaps.
javamay contain transcription errors
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
for(int i = 0; i < n-1; i++){
for(int j = 0; j < n-i-1; j++){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int num : arr)
System.out.print(num + " ");
}
}