Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void quicksort( double array[], long long left, long long right);
  4.  
  5. long long partition( double array[], long long left, long long right);
  6.  
  7. int fDig( double x);
  8.  
  9. int main( void) {
  10. long long quantity = 0LL, counter = 0LL;
  11. int dig = 0;
  12. scanf("%lli", &quantity);
  13.  
  14. double beard[quantity];
  15.  
  16. for( counter = 0LL; counter < quantity; counter++) {
  17. scanf("%lf", &beard[counter]);
  18. }
  19.  
  20. quicksort( beard, 0LL, quantity - 1LL);
  21.  
  22. for( counter = 0LL; counter < quantity; counter++) {
  23. dig = fDig( beard[counter]);
  24. printf("%.*lf", dig, beard[counter]);
  25. if( counter == quantity - 1LL) {
  26. break;
  27. }
  28.  
  29. printf(" ");
  30. }
  31. return 0;
  32. }
  33.  
  34. int fDig( double x) {
  35. int result = 0;
  36. while( x != floor(x)) {
  37. x *= 10;
  38. result++;
  39. }
  40.  
  41. return result;
  42.  
  43.  
  44.  
  45. }
  46.  
  47. /* Quick sort recursive function */
  48.  
  49. void quicksort( double array[], long long left, long long right){
  50. if ( left < right) {
  51. long long middle = partition( array, left, right);
  52. quicksort( array, left, middle - 1LL);
  53. quicksort( array, middle + 1LL, right);
  54. }
  55. }
  56.  
  57. /* Partition :
  58. Modifies the array :
  59. SMALLER , PIVOT , GREATER
  60. Returns the index for pivot because pivot is placed in the final position
  61. */
  62.  
  63. long long partition( double array[], long long left, long long right) {
  64. long long middle;
  65.  
  66. double x = array[left];
  67. long long l = left;
  68. long long r = right;
  69.  
  70. while( l < r) {
  71. while( ( array[l] <= x) && ( l < right)) {
  72. l++;
  73. }
  74.  
  75. while( ( array[r] > x) && ( r >= left)) {
  76. r--;
  77. }
  78.  
  79. if( l < r) {
  80. double temp = array[l];
  81. array[l] = array[r];
  82. array[r] = temp;
  83. }
  84. }
  85.  
  86. middle = r;
  87.  
  88. double temp = array[left];
  89. array[left] = array[middle] ;
  90. array[middle] = temp;
  91.  
  92. return middle ;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement