Posted By

A4i on 12/23/11


Tagged

bubblesort bubbleSortInc bubbleSortDec binSearch


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

sqdus33


SortAndSearchLib


 / Published in: C
 

Sort And Search Lib

  1. #include <stdio.h>
  2.  
  3. void bubbleSortInc(double array[])
  4. {
  5. int i, j, n = 0;
  6. double temp;
  7.  
  8. while(array[n])
  9. {
  10. n++;
  11. }
  12.  
  13. for(i = n - 1; i > 0; i--)
  14. {
  15. for(j = 1; j <= i; j++)
  16. {
  17. if(array[j - 1] > array[j])
  18. {
  19. temp = array[j - 1];
  20. array[j - 1] = array[j];
  21. array[j] = temp;
  22. }
  23. }
  24. }
  25. }
  26.  
  27. void bubbleSortDec(double array[])
  28. {
  29. int i, j, n = 0;
  30. double temp;
  31.  
  32. while(array[n])
  33. {
  34. n++;
  35. }
  36.  
  37. for(i = n - 1; i > 0; i--)
  38. {
  39. for(j = 1; j <= i; j++)
  40. {
  41. if(array[j - 1] < array[j])
  42. {
  43. temp = array[j - 1];
  44. array[j - 1] = array[j];
  45. array[j] = temp;
  46. }
  47. }
  48. }
  49. }
  50.  
  51. int binSearch(double array[], double x)
  52. {
  53. int start, half, end, n = 0;
  54.  
  55. while(array[n])
  56. {
  57. n++;
  58. }
  59.  
  60. start = 0;
  61. end = n - 1;
  62.  
  63. while(start <= end)
  64. {
  65. half = (start + end) / 2;
  66.  
  67. if(x < array[half])
  68. {
  69. end = half - 1;
  70. }
  71.  
  72. else if(x > array[half])
  73. {
  74. start = half + 1;
  75. }
  76.  
  77. else
  78. {
  79. return half;
  80. }
  81. }
  82.  
  83. return -1;
  84. }

Report this snippet  

You need to login to post a comment.