fork download
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. void swap(int* arr, int indexOne, int indexTwo) {
  5. if(arr!=NULL){
  6. int tmp = arr[indexOne];
  7. arr[indexOne] = arr[indexTwo];
  8. arr[indexTwo] = tmp;
  9. }
  10. }
  11.  
  12. void displayArray(int* arr, int len) {
  13. if(arr!=NULL) {
  14. printf("{ ");
  15. for(int i = 0; i < len; ++i) {
  16. printf("%d ", arr[i]);
  17. }
  18. printf("}\n");
  19. }
  20. }
  21.  
  22. void selectionSort(int* arr, int len){
  23.  
  24. if(arr!=NULL) {
  25. for(int i = 0; i < (len-1); ++i) {
  26. int minIndex = i;
  27. int minValue = arr[i];
  28.  
  29. //Iterate thru right subarray and find the smallest value
  30. for(int j = (i+1); j < len; ++j) {
  31. if(arr[j]<minValue) {
  32. minIndex = j;
  33. minValue = arr[j];
  34. }
  35. }
  36.  
  37. if(minIndex!=i) {
  38. //Swap current value
  39. swap(arr,i, minIndex);
  40. }
  41. }
  42. }
  43.  
  44. }
  45.  
  46. void insertionSort(int* arr, int len){
  47.  
  48. for(int i = 1; i < len; ++i){
  49. int key = arr[i];
  50. int j = (i-1);
  51.  
  52. //Iterate thru left subarray and find the correct position for insertion
  53. while((j>=0) && (arr[j]>key)){
  54. arr[j+1] = arr[j];
  55. --j;
  56. }
  57.  
  58. arr[j+1] = key;
  59. }
  60. }
  61.  
  62. bool isPalindrome(char* word, int len) {
  63. bool isPalindrome = true;
  64.  
  65. if(word!=NULL) {
  66. for(int i=0; i<(len/2); ++i) {
  67. int end = (len-i) - 1;
  68.  
  69. //Determine if the characters at the opposite ends of the string are
  70. //equal
  71. if(word[i]!=word[end]) {
  72. isPalindrome = false;
  73. break;
  74. }
  75. }
  76. }
  77.  
  78. return isPalindrome;
  79. }
  80.  
  81. bool isPalindrome2(char* word, int len) {
  82.  
  83. //String is less than or equal to a single character
  84. if(len<=1) {
  85. return true;
  86. }
  87.  
  88. //Compare first and last characters
  89. if(word[0]!=word[len-1]) {
  90. return false;
  91. }
  92.  
  93. return isPalindrome2(word + 1, (len-2));
  94. }
  95.  
  96. int main() {
  97.  
  98. int arr[] = { 56, 0, -2, 10, -3, 1 };
  99. int len = sizeof(arr)/sizeof(int);
  100.  
  101. //Expected: -3, -2, 0, 1, 10, 56
  102. selectionSort(arr,len);
  103. displayArray(arr,len);
  104.  
  105. int arr2[] = { 70, 100, 3, 4, -9, 1, 5 };
  106. int len2 = sizeof(arr2)/sizeof(int);
  107.  
  108. //Expected: -9, 1, 3, 4, 5, 6, 100
  109. insertionSort(arr2,len2);
  110. displayArray(arr2,len2);
  111.  
  112. printf("Is '%s' a palindrome: %s\n", "a", isPalindrome("a", 1)? "true": "false");
  113. printf("Is '%s' a palindrome: %s\n", "motor", isPalindrome("motor", 5) ? "true" : "false");
  114. printf("Is '%s' a palindrome: %s\n", "rotor", isPalindrome("rotor", 5) ? "true" : "false");
  115. printf("Is '%s' a palindrome: %s\n", "civic", isPalindrome("civic", 5) ? "true" : "false");
  116. printf("Is '%s' a palindrome: %s\n", "racecar", isPalindrome("racecar", 7) ? "true" : "false");
  117. printf("Is '%s' a palindrome: %s\n", "", isPalindrome("", 0) ? "true" : "false");
  118.  
  119. printf("\nIs '%s' a palindrome: %s\n", "a", isPalindrome2("a", 1)? "true": "false");
  120. printf("Is '%s' a palindrome: %s\n", "motor", isPalindrome2("motor", 5) ? "true" : "false");
  121. printf("Is '%s' a palindrome: %s\n", "rotor", isPalindrome2("rotor", 5) ? "true" : "false");
  122. printf("Is '%s' a palindrome: %s\n", "civic", isPalindrome2("civic", 5) ? "true" : "false");
  123. printf("Is '%s' a palindrome: %s\n", "racecar", isPalindrome2("racecar", 7) ? "true" : "false");
  124. printf("Is '%s' a palindrome: %s\n", "", isPalindrome2("", 0) ? "true" : "false");
  125.  
  126. return 0;
  127. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
{ -3 -2 0 1 10 56 }
{ -9 1 3 4 5 70 100 }
Is 'a' a palindrome: true
Is 'motor' a palindrome: false
Is 'rotor' a palindrome: true
Is 'civic' a palindrome: true
Is 'racecar' a palindrome: true
Is '' a palindrome: true

Is 'a' a palindrome: true
Is 'motor' a palindrome: false
Is 'rotor' a palindrome: true
Is 'civic' a palindrome: true
Is 'racecar' a palindrome: true
Is '' a palindrome: true