Just a couple small c programs.

mysort.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <stddef.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. // Resizes array a using realloc
  6. unsigned int *resize_array(unsigned int *a, size_t new_size)
  7. {
  8. unsigned int *save;
  9. save = realloc(a, new_size);
  10. if (save == NULL) {
  11. fprintf(stderr, "Realloc failed, cannot resize array.\n");
  12. exit(EXIT_FAILURE);
  13. }
  14. return save;
  15. }
  16. // Swaps the values of a and b
  17. void swap(unsigned int *a, unsigned int *b){
  18. unsigned int old_a = *a;
  19. *a = *b;
  20. *b = old_a;
  21. }
  22. // Creates a partition for use by quicksort
  23. int partition(unsigned int *array, int left, int right, int pivot){
  24. unsigned int pivot_value = array[pivot];
  25. swap(&array[pivot], &array[right]);
  26. int storeIndex = left;
  27. for(int i = left; i < right; i++){
  28. if (array[i] < pivot_value){
  29. swap(&array[i], &array[storeIndex]);
  30. storeIndex++;
  31. }
  32. }
  33. swap(&array[storeIndex], &array[right]);
  34. return storeIndex;
  35. }
  36. // Using partition to sort `array`
  37. void quicksort(unsigned int *array, int left, int right){
  38. if (left < right){
  39. int pivot = right;
  40. pivot = partition(array, left, right, pivot);
  41. quicksort(array, left, pivot - 1);
  42. quicksort(array, pivot + 1, right);
  43. }
  44. }
  45. int main()
  46. {
  47. const int temp_size = 50; // How many numbers to load at once?
  48. int size = 1;
  49. // Holds all numbers we need to sort
  50. unsigned int *numbers = malloc(size * sizeof *numbers);
  51. // Holds the numbers recently pulled from the stream
  52. unsigned int *temp = malloc(temp_size * sizeof *temp);
  53. int ret_val; // Holds the return value of fread so we know how many numbers were read
  54. /// Loop through stdin stream grabbing `temp_size` numbers each iteration
  55. while ((ret_val = fread(temp, sizeof *temp, temp_size, stdin))){
  56. // Should not happen if EOF has been reached/we have loaded all data
  57. if (ret_val != 0){
  58. // This looks complicated, just the math required to figure the new size of `numbers`
  59. int new_size = size - 1 + ret_val;
  60. // Actually resize array
  61. numbers = resize_array(numbers, new_size * sizeof *numbers);
  62. // Go from size to new_size, putting the numbers from `temp` into `numbers`
  63. int temp_count = 0;
  64. for(int i = size - 1; i < new_size; i++){
  65. numbers[i] = temp[temp_count++];
  66. }
  67. // Update `size`, now that we are done with it's previous value
  68. size = new_size;
  69. }
  70. }
  71. quicksort(numbers, 0, size - 1);
  72. for (int i = 0; i < size; i++){
  73. printf("%u\n", numbers[i]);
  74. }
  75. free(temp);
  76. free(numbers);
  77. return 0;
  78. }