运行 ❯
获取您的
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
def bubbleSort(arr): n = len(arr) for i in range(n): for j in range(0, n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] def radixSortWithBubbleSort(arr): max_val = max(arr) exp = 1 while max_val // exp > 0: radixArray = [[],[],[],[],[],[],[],[],[],[]] for num in arr: radixIndex = (num // exp) % 10 radixArray[radixIndex].append(num) for bucket in radixArray: bubbleSort(bucket) i = 0 for bucket in radixArray: for num in bucket: arr[i] = num i += 1 exp *= 10 myArray = [170, 45, 75, 90, 802, 24, 2, 66] print("Original array:", myArray) radixSortWithBubbleSort(myArray) print("Sorted array:", myArray) #Python
#include <stdio.h> #include <stdlib.h> void bubbleSort(int arr[], int n) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n - i - 1; ++j) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } void radixSortWithBubbleSort(int arr[], int n) { int max_val = arr[0]; for (int i = 1; i < n; ++i) { if (arr[i] > max_val) max_val = arr[i]; } int exp = 1; while (max_val / exp > 0) { int radixArray[10][n]; int count[10] = {0}; for (int i = 0; i < n; ++i) { int radixIndex = (arr[i] / exp) % 10; radixArray[radixIndex][count[radixIndex]++] = arr[i]; } for (int i = 0; i < 10; ++i) { bubbleSort(radixArray[i], count[i]); } int index = 0; for (int i = 0; i < 10; ++i) { for (int j = 0; j < count[i]; ++j) { arr[index++] = radixArray[i][j]; } } exp *= 10; } } int main() { int myArray[] = {170, 45, 75, 90, 802, 24, 2, 66}; int n = sizeof(myArray) / sizeof(myArray[0]); printf("Original array: "); for (int i = 0; i < n; ++i) printf("%d ", myArray[i]); printf("\n"); radixSortWithBubbleSort(myArray, n); printf("Sorted array: "); for (int i = 0; i < n; ++i) printf("%d ", myArray[i]); printf("\n"); return 0; } //C
public class Main { public static void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } public static void radixSortWithBubbleSort(int[] arr) { int maxVal = arr[0]; for (int num : arr) { if (num > maxVal) maxVal = num; } int exp = 1; while (maxVal / exp > 0) { int[][] radixArray = new int[10][arr.length]; int[] count = new int[10]; for (int num : arr) { int radixIndex = (num / exp) % 10; radixArray[radixIndex][count[radixIndex]++] = num; } for (int i = 0; i < 10; i++) { int[] tempArray = new int[count[i]]; System.arraycopy(radixArray[i], 0, tempArray, 0, count[i]); bubbleSort(tempArray); System.arraycopy(tempArray, 0, radixArray[i], 0, count[i]); } int index = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < count[i]; j++) { arr[index++] = radixArray[i][j]; } } exp *= 10; } } public static void main(String[] args) { int[] myArray = {170, 45, 75, 90, 802, 24, 2, 66}; System.out.print("Original array: "); for (int num : myArray) System.out.print(num + " "); System.out.println(); radixSortWithBubbleSort(myArray); System.out.print("Sorted array: "); for (int num : myArray) System.out.print(num + " "); System.out.println(); } } //Java
Python 结果
C 结果
Java 结果
原始数组: [170, 45, 75, 90, 802, 24, 2, 66]
排序后的数组: [2, 24, 45, 66, 75, 90, 170, 802]
原始数组: 170 45 75 90 802 24 2 66
排序后的数组: 2 24 45 66 75 90 170 802
原始数组: 170 45 75 90 802 24 2 66
排序后的数组: 2 24 45 66 75 90 170 802