
									 Problem
								
								
									 Solution
								
								
									 Submissions
								
								
							Sort an array of integers using bubble sort.
								Certification: Basic Level
								Accuracy: 42.86%
								Submissions: 7
								Points: 8
							
							Write a C# program to implement the BubbleSort(int[] arr) function, which sorts an array of integers in ascending order using the bubble sort algorithm.
Algorithm
- Step 1: Iterate through the array multiple times.
 - Step 2: In each iteration, compare adjacent elements and swap them if they are in the wrong order.
 - Step 3: After each pass, the largest element bubbles up to the end of the array.
 - Step 4: Continue until no more swaps are needed, indicating the array is sorted.
 
Example 1
- Input: arr = [5, 1, 4, 2, 8]
 - Output: [1, 2, 4, 5, 8]
 - Explanation: 
- First Pass:
 - - Compare 5 > 1, swap: [1, 5, 4, 2, 8]
 - - Compare 5 > 4, swap: [1, 4, 5, 2, 8]
 - - Compare 5 > 2, swap: [1, 4, 2, 5, 8]
 - - Compare 5 > 8, no swap: [1, 4, 2, 5, 8]
 - Second Pass:
 - - Compare 1 > 4, no swap: [1, 4, 2, 5, 8]
 - - Compare 4 > 2, swap: [1, 2, 4, 5, 8]
 - - Compare 4 > 5, no swap: [1, 2, 4, 5, 8]
 - - Compare 5 > 8, no swap: [1, 2, 4, 5, 8]
 - Third Pass:
 - - No swaps needed, array is sorted.
 
 
Example 2
- Input: arr = [3, 3, 1]
 - Output: [1, 3, 3]
 - Explanation: 
- First Pass:
 - - Compare 3 > 3, no swap: [3, 3, 1]
 - - Compare 3 > 1, swap: [3, 1, 3]
 - Second Pass:
 - - Compare 3 > 1, swap: [1, 3, 3]
 - - Compare 3 > 3, no swap: [1, 3, 3]
 - Third Pass:
 - - No swaps needed, array is sorted.
 
 
Constraints
- 1 ≤ arr.length ≤ 10^3
 - -10^6 ≤ arr[i] ≤ 10^6
 - Time Complexity: O(n²)
 - Space Complexity: O(1)
 
Editorial
									
												
My Submissions
										All Solutions
									| Lang | Status | Date | Code | 
|---|---|---|---|
| You do not have any submissions for this problem. | |||
| User | Lang | Status | Date | Code | 
|---|---|---|---|---|
| No submissions found. | ||||
Solution Hints
- Use two nested loops to implement the bubble sort algorithm
 - The outer loop determines the number of passes needed (at most n-1 passes for an array of length n)
 - The inner loop performs the comparisons and swaps for each pass
 - Add an optimization to stop early if no swaps were made in a pass