运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,暗/亮
前往 Spaces
Python
C
Java
my_array = [64, 34, 25, 12, 22, 11, 90, 5] n = len(my_array) for i in range(n): min_index = i for j in range(i+1, n): if my_array[j] < my_array[min_index]: min_index = j my_array[i], my_array[min_index] = my_array[min_index], my_array[i] print("Sorted array:", my_array) #Python
#include <stdio.h> int main() { int my_array[] = {64, 34, 25, 12, 22, 11, 90, 5}; int n = sizeof(my_array) / sizeof(my_array[0]); for (int i = 0; i < n; i++) { int min_index = i; for (int j = i+1; j < n; j++) { if (my_array[j] < my_array[min_index]) { min_index = j; } } int temp = my_array[i]; my_array[i] = my_array[min_index]; my_array[min_index] = temp; } 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 = {64, 34, 25, 12, 22, 11, 90, 5}; int n = my_array.length; for (int i = 0; i < n; i++) { int min_index = i; for (int j = i+1; j < n; j++) { if (my_array[j] < my_array[min_index]) { min_index = j; } } int temp = my_array[i]; my_array[i] = my_array[min_index]; my_array[min_index] = temp; } System.out.print("Sorted array: "); for (int i = 0; i < n; i++) { System.out.print(my_array[i] + " "); } System.out.println(); } } //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