Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. void countSort(char *arr[], int n, int k, int pos){
  4.     char result[n][k];
  5.     int val;
  6.     char count[10] = { 0 };
  7.     for(int i = 0; i < n; i++){
  8.         count[arr[i][pos] - '0']++;
  9.     }
  10.     for(int i = 1; i < 10; i++){
  11.         count[i] += count[i - 1];
  12.     }
  13.  
  14.     /*for(int i = 0; i < 10; i++){
  15.         printf("%d ", count[i]);
  16.     }*/
  17.  
  18.     for(int i = n - 1; i >= 0; i--){
  19.         for(int j = 0; j < k; j++) {
  20.             result[count[arr[i][pos]- '0'] - 1][j] = arr[i][j];
  21.         }
  22.         count[ arr[i][pos] - '0' ]--;
  23.     }
  24.     for(int i = 0; i < n; i++){
  25.         for(int j = 0; j < k; j++){
  26.             arr[i][j] = result[i][j];
  27.         }
  28.     }
  29.  
  30.  
  31. }
  32.  
  33. int main() {
  34.     int n;
  35.     int k;
  36.     scanf("%d", &n);
  37.     scanf("%d", &k);
  38.     char** arr = (char **)malloc((n + 1) * sizeof(char *));
  39.     for(int i = 0; i < n; i++){
  40.         arr[i] = (char *)malloc((k + 1) * sizeof(char));
  41.     }
  42.     for(int i = 0; i < n; i++){
  43.         scanf("%s", arr[i]);
  44.     }
  45.     for(int i = k - 1; i >= 0; i--) {
  46.         countSort(arr, n, k, i);
  47.     }
  48.     for (int i = 0; i < n; i++) {
  49.         printf("\n%s", arr[i]);
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement