Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
my_array = [7, 3, 9, 12, 11] n = len(my_array) for i in range(n-1): swapped = False for j in range(n-i-1): if my_array[j] > my_array[j+1]: my_array[j], my_array[j+1] = my_array[j+1], my_array[j] swapped = True if not swapped: break print("Sorted array:", my_array) #Python
#include
#include
int main() { int my_array[] = {7, 3, 9, 12, 11}; int n = sizeof(my_array) / sizeof(my_array[0]); for (int i = 0; i < n-1; i++) { bool swapped = false; for (int j = 0; j < n-i-1; j++) { if (my_array[j] > my_array[j+1]) { int temp = my_array[j]; my_array[j] = my_array[j+1]; my_array[j+1] = temp; swapped = true; } } if (!swapped) { break; } } printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", my_array[i]); } printf("\n"); return 0; } //C
public class Main { public static void main(String[] args) { int[] my_array = {7, 3, 9, 12, 11}; int n = my_array.length; for (int i = 0; i < n-1; i++) { boolean swapped = false; for (int j = 0; j < n-i-1; j++) { if (my_array[j] > my_array[j+1]) { int temp = my_array[j]; my_array[j] = my_array[j+1]; my_array[j+1] = temp; swapped = true; } } if (!swapped) { break; } } System.out.print("Sorted array: "); for (int i = 0; i < n; i++) { System.out.print(my_array[i] + " "); } System.out.println(); } } //Java
Python result:
C result:
Java result:
Sorted array: [3, 7, 9, 11, 12]
Sorted array: 3 7 9 11 12
Sorted array: 3 7 9 11 12