Just a couple small c programs.

mysort.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright (c) 2013, Lily Carpenter
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. Redistributions in binary form must reproduce the above copyright notice, this
  9. list of conditions and the following disclaimer in the documentation and/or
  10. other materials provided with the distribution.
  11. Neither the name of Lily Carpenter nor the names of its
  12. contributors may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <stddef.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <fcntl.h>
  29. // Resizes array a using realloc
  30. unsigned int *resize_array(unsigned int *a, size_t new_size)
  31. {
  32. unsigned int *save;
  33. save = realloc(a, new_size);
  34. if (save == NULL) {
  35. fprintf(stderr, "Realloc failed, cannot resize array.\n");
  36. exit(EXIT_FAILURE);
  37. }
  38. return save;
  39. }
  40. // Swaps the values of a and b
  41. void swap(unsigned int *a, unsigned int *b){
  42. unsigned int old_a = *a;
  43. *a = *b;
  44. *b = old_a;
  45. }
  46. // Creates a partition for use by quicksort
  47. int partition(unsigned int *array, int left, int right, int pivot){
  48. unsigned int pivot_value = array[pivot];
  49. swap(&array[pivot], &array[right]);
  50. int storeIndex = left;
  51. for(int i = left; i < right; i++){
  52. if (array[i] < pivot_value){
  53. swap(&array[i], &array[storeIndex]);
  54. storeIndex++;
  55. }
  56. }
  57. swap(&array[storeIndex], &array[right]);
  58. return storeIndex;
  59. }
  60. // Using partition to sort `array`
  61. void quicksort(unsigned int *array, int left, int right){
  62. if (left < right){
  63. int pivot = right;
  64. pivot = partition(array, left, right, pivot);
  65. quicksort(array, left, pivot - 1);
  66. quicksort(array, pivot + 1, right);
  67. }
  68. }
  69. int main()
  70. {
  71. const int temp_size = 50; // How many numbers to load at once?
  72. int size = 1;
  73. // Holds all numbers we need to sort
  74. unsigned int *numbers = malloc(size * sizeof *numbers);
  75. // Holds the numbers recently pulled from the stream
  76. unsigned int *temp = malloc(temp_size * sizeof *temp);
  77. int ret_val; // Holds the return value of fread so we know how many numbers were read
  78. /// Loop through stdin stream grabbing `temp_size` numbers each iteration
  79. while ((ret_val = fread(temp, sizeof *temp, temp_size, stdin))){
  80. // Should not happen if EOF has been reached/we have loaded all data
  81. if (ret_val != 0){
  82. // This looks complicated, just the math required to figure the new size of `numbers`
  83. int new_size = size - 1 + ret_val;
  84. // Actually resize array
  85. numbers = resize_array(numbers, new_size * sizeof *numbers);
  86. // Go from size to new_size, putting the numbers from `temp` into `numbers`
  87. int temp_count = 0;
  88. for(int i = size - 1; i < new_size; i++){
  89. numbers[i] = temp[temp_count++];
  90. }
  91. // Update `size`, now that we are done with it's previous value
  92. size = new_size;
  93. }
  94. }
  95. quicksort(numbers, 0, size - 1);
  96. for (int i = 0; i < size; i++){
  97. printf("%u\n", numbers[i]);
  98. }
  99. free(temp);
  100. free(numbers);
  101. return 0;
  102. }