Submission
Status:
-----
Score: 0
User: Angonnyyy
Problemset: ชั้นหนังสือ
Language: c
Time: 0.001 second
Submitted On: 2024-09-25 16:43:14
#include <stdio.h>
int main() {
int n, i, j, counter, temp;
// Input the size of the array
scanf("%d", &n);
int array[n];
// Input the elements of the array
for (i = 0; i < n; i++) { // Change loop to n instead of n-1
scanf("%d", &array[i]); // Remove space in scanf format
}
// Bubble Sort
for (i = 0; i < n - 1; i++) { // Correct outer loop
counter = 0;
for (j = 0; j < n - 1 - i; j++) { // Reduce the range of inner loop
if (array[j] > array[j + 1]) {
// Swap elements
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
counter++;
}
}
// If no swaps occurred, the array is sorted
if (counter == 0) {
break;
}
}
// Output the sorted array
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n"); // Newline for better output format
return 0;
}