Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to 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
#include
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 result:
C result:
Java result:
Original array: [170, 45, 75, 90, 802, 24, 2, 66]
Sorted array: [2, 24, 45, 66, 75, 90, 170, 802]
Original array: 170 45 75 90 802 24 2 66
Sorted array: 2 24 45 66 75 90 170 802
Original array: 170 45 75 90 802 24 2 66
Sorted array: 2 24 45 66 75 90 170 802