Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
my_array = [64, 34, 25, 12, 22, 11, 90, 5] n = len(my_array) for i in range(1,n): insert_index = i current_value = my_array.pop(i) for j in range(i-1, -1, -1): if my_array[j] > current_value: insert_index = j my_array.insert(insert_index, current_value) print("Sorted array:", my_array) #Python
#include
int main() { int myArray[] = {64, 34, 25, 12, 22, 11, 90, 5}; int n = sizeof(myArray) / sizeof(myArray[0]); for (int i = 1; i < n; i++) { int insertIndex = i; int currentValue = myArray[i]; int j = i - 1; while (j >= 0 && myArray[j] > currentValue) { myArray[j + 1] = myArray[j]; insertIndex = j; j--; } myArray[insertIndex] = currentValue; } printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", myArray[i]); } return 0; } //C
public class Main { public static void main(String[] args) { int[] myArray = {64, 34, 25, 12, 22, 11, 90, 5}; int n = myArray.length; for (int i = 1; i < n; i++) { int insertIndex = i; int currentValue = myArray[i]; int j = i - 1; while (j >= 0 && myArray[j] > currentValue) { myArray[j + 1] = myArray[j]; insertIndex = j; j--; } myArray[insertIndex] = currentValue; } System.out.print("Sorted array: "); for (int value : myArray) { System.out.print(value + " "); } } } //Java
Python result:
C result:
Java result:
Sorted array: [5, 11, 12, 22, 25, 34, 64, 90]
Sorted array: 5 11 12 22 25 34 64 90
Sorted array: 5 11 12 22 25 34 64 90