akadjoker

Untitled

Jul 16th, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. #define MAX 50
  7.  
  8.  
  9. double getRandomNumber(double min, double max)
  10. {
  11. double range = max - min;
  12. double randomNum = (double)rand() / RAND_MAX;
  13. return min + randomNum * range;
  14. }
  15.  
  16.  
  17.  
  18. void counting_sort(int array[], int size, int place) {
  19. const int max = 10;
  20. int output[size];
  21. int count[max];
  22.  
  23. for (int i = 0; i < max; ++i)
  24. count[i] = 0;
  25.  
  26. for (int i = 0; i < size; i++)
  27. count[(array[i]/place) % 10]++;
  28.  
  29. for (int i = 1; i < max; i++)
  30. count[i] += count[i - 1];
  31.  
  32. for (int i = size - 1; i >= 0; i--) {
  33. output[count[(array[i]/place) % 10] - 1] = array[i];
  34. count[(array[i]/place) % 10]--;
  35. }
  36.  
  37. for (int i = 0; i < size; i++)
  38. array[i] = output[i];
  39. }
  40.  
  41. void radixsort(int array[], int size) {
  42. int max = abs(array[0]);
  43. for (int i = 1; i < size; i++)
  44. if (abs(array[i]) > max)
  45. max = abs(array[i]);
  46.  
  47. for (int place = 1; max/place > 0; place *= 10)
  48. counting_sort(array, size, place);
  49. }
  50.  
  51. void split_and_sort(int arr[], int n) {
  52. int max = 0;
  53. for(int i = 0; i < n; i++) {
  54. if(abs(arr[i]) > max) {
  55. max = abs(arr[i]);
  56. }
  57. }
  58.  
  59. int negCount = 0, posCount = 0;
  60. int negArray[n], posArray[n];
  61. for (int i = 0; i < n; i++) {
  62. if (arr[i] < 0) {
  63. negArray[negCount++] = -arr[i];
  64. } else {
  65. posArray[posCount++] = arr[i];
  66. }
  67. }
  68.  
  69. radixsort(negArray, negCount);
  70. radixsort(posArray, posCount);
  71.  
  72. for(int i = 0; i < negCount / 2; i++) {
  73. int temp = negArray[i];
  74. negArray[i] = negArray[negCount - i - 1];
  75. negArray[negCount - i - 1] = temp;
  76. }
  77.  
  78. printf("Array after sorting: \n");
  79. for(int i = 0; i < negCount; i++) {
  80. printf("%d ", -negArray[i]);
  81. }
  82. for(int i = 0; i < posCount; i++) {
  83. printf("%d ", posArray[i]);
  84. }
  85. }
  86.  
  87. int main() {
  88. int array[] = {19, -52, 53, 56, -12, 46, 91, 76, -23, -82, 88, -38, -73, 14, 49, -3, -39, 58, -79, 98, 90, 37, 18, 25, 94, -78, -4, -80, 34, -32, -63, -46, 14, 89, -89, -97, 36, -97, 78, -86, -80, 66, -25, -54, -18, -76, 42, 41, 82, 62};
  89. int n = sizeof(array)/sizeof(array[0]);
  90. split_and_sort(array, n);
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment