C Online Compiler
Example: Bubble Sort without Function in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Bubble Sort without Function #include <stdio.h> int main() { // Step 1: Declare and initialize the array int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements // Step 2: Print the original array printf("Original array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); // Step 3: Implement Bubble Sort algorithm // Outer loop for passes (n-1 passes are sufficient) for (int i = 0; i < n - 1; i++) { // Inner loop for comparisons and swaps in each pass // The last i elements are already in place, so we don't need to check them for (int j = 0; j < n - i - 1; j++) { // Compare adjacent elements if (arr[j] > arr[j + 1]) { // Swap if the element found is greater than the next element int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } // Step 4: Print the sorted array printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
Output
Clear
ADVERTISEMENTS