运行 ❯
获取你
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 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[i] for j in range(i-1, -1, -1): if my_array[j] > current_value: my_array[j+1] = my_array[j] insert_index = j else: break my_array[insert_index] = current_value print("Sorted array:", my_array) #Python
#include <stdio.h> 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 结果
C 结果
Java 结果
排序后的数组: [5, 11, 12, 22, 25, 34, 64, 90]
排序后的数组: 5 11 12 22 25 34 64 90
排序后的数组: 5 11 12 22 25 34 64 90