Guest User

Untitled

a guest
Oct 20th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. /**
  2. * helpers.c
  3. *
  4. * CS50 AP
  5. * Sort Race
  6. *
  7. * Helper functions for the sort race
  8. */
  9.  
  10. #include <cs50.h>
  11. #include <stdio.h>
  12. #include "helpers.h"
  13.  
  14. /**
  15. * Returns true if str is a valid flag (-a, -b, -r, or -s), false otherwise.
  16. */
  17. bool check_flag(char *str)
  18. {
  19. //store the dash and the letter
  20. char dash = str[0];
  21. char letter = str[1];
  22. //return false if there is not a dash
  23. //return false if the letter doesn't match a, b , r , s
  24. //return true otherwise
  25. switch (dash)
  26. {
  27. case '-':
  28. switch (letter)
  29. {
  30. case 'a':
  31. case 'b':
  32. case 'r':
  33. case 's':
  34. return true;
  35. default:
  36. return false;
  37. }
  38. break;
  39. default:
  40. return false;
  41. }
  42. }
  43.  
  44. /**
  45. * Sorts array of n values using bubble sort.
  46. */
  47. void bubble(int values[], int n)
  48. {
  49. bool loop = true; //keep looping over arary while loop is true,
  50. //loop keep track if a change was made last iteration
  51. while (loop)
  52. {
  53. loop = false;
  54. for (int i = 0; i < n - 1; i++) //loop over array
  55. {
  56. if (values[i] > values[i + 1]) //swap values if the next one is larger
  57. {
  58. //do the swapping
  59. int temp = values[i];
  60. values[i] = values[i + 1];
  61. values[i + 1] = temp;
  62. //loop again
  63. loop = true;
  64. }
  65. }
  66. }
  67. }
  68.  
  69. /**
  70. * Sorts array of n values using selection sort.
  71. */
  72. void selection(int values[], int n)
  73. {
  74. int sortedStart = 0; //keeps track of the sorted portion
  75. int min; //value of the smallest
  76. int minLocation; //location of the smallest
  77. while (sortedStart != n - 1) //loop until the sorted portion of the array is the entire array
  78. {
  79. min = 1000;
  80. for (int i = sortedStart; i < n; i++) // find the min value
  81. {
  82. if (values[i] < min)
  83. {
  84. min = values[i];
  85. minLocation = i;
  86. }
  87. }
  88. //move the min value, and swap
  89. int temp = values[sortedStart];
  90. values[sortedStart] = values[minLocation];
  91. values[minLocation] = temp;
  92. sortedStart++;
  93. }
  94. }
  95.  
  96. /**
  97. * Sorts array of n values using insertion sort.
  98. */
  99. void insertion(int values[], int n)
  100. {
  101. int holePosition;
  102. int valueToInsert;
  103. for (int i = 1; i <= n; i++) //loop over array
  104. {
  105. //set location and value to the current place in the array being sorted
  106. valueToInsert = values[i];
  107. holePosition = i;
  108. //loop while the value is smaller than the one before it, while moving into the hole position
  109. while (holePosition > 0 && values[holePosition - 1] > valueToInsert)
  110. {
  111. values[holePosition] = values[holePosition - 1];
  112. holePosition--;
  113. }
  114.  
  115. values[holePosition ] = valueToInsert;
  116. }
  117. }
Add Comment
Please, Sign In to add comment