Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- void countSort(char *arr[], int n, int k, int pos){
- char result[n][k];
- int val;
- char count[10] = { 0 };
- for(int i = 0; i < n; i++){
- count[arr[i][pos] - '0']++;
- }
- for(int i = 1; i < 10; i++){
- count[i] += count[i - 1];
- }
- /*for(int i = 0; i < 10; i++){
- printf("%d ", count[i]);
- }*/
- for(int i = n - 1; i >= 0; i--){
- for(int j = 0; j < k; j++) {
- result[count[arr[i][pos]- '0'] - 1][j] = arr[i][j];
- }
- count[ arr[i][pos] - '0' ]--;
- }
- for(int i = 0; i < n; i++){
- for(int j = 0; j < k; j++){
- arr[i][j] = result[i][j];
- }
- }
- }
- int main() {
- int n;
- int k;
- scanf("%d", &n);
- scanf("%d", &k);
- char** arr = (char **)malloc((n + 1) * sizeof(char *));
- for(int i = 0; i < n; i++){
- arr[i] = (char *)malloc((k + 1) * sizeof(char));
- }
- for(int i = 0; i < n; i++){
- scanf("%s", arr[i]);
- }
- for(int i = k - 1; i >= 0; i--) {
- countSort(arr, n, k, i);
- }
- for (int i = 0; i < n; i++) {
- printf("\n%s", arr[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement