Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. selection_sort(int a, int b){
  2. if(a == b+1){
  3. //do nothing
  4. }else{
  5. i = minIndex(arr, a, b); //minIndex finds minimum value of 2 index's an array
  6. if(i != a)
  7. swap(arr, i, a);
  8. selection_sort(a+1, b);
  9. }
  10. }
  11.  
  12. void countdown(int t) {
  13. if (t <= 1) return;
  14. printf("%dn", t);
  15. countdown(t - 1);
  16. }
  17.  
  18. int factorial(int n) {
  19. if (n <= 1) return 1;
  20. return n * factorial(n - 1);
  21. }
  22.  
  23. int factorial(int n, int product = 1) {
  24. if (n <= 1) return product;
  25. return factorial(n - 1, product * n);
  26. }
  27.  
  28. selection_sort(int a, int b){
  29. tail_call:
  30. if(a == b+1){
  31. //do nothing
  32. }else{
  33. i = minIndex(arr, a, b);
  34. if(i != a)
  35. swap(arr, i, a);
  36.  
  37. // tweak the variables so the next iteration sees (a+1, b)
  38. a += 1;
  39. goto tail_call;
  40. }
  41. }
  42.  
  43. selection_sort(int a, int b){
  44. tail_call:
  45. if(a != b+1){
  46. i = minIndex(arr, a, b);
  47. if(i != a)
  48. swap(arr, i, a);
  49.  
  50. // tweak the variables so the next iteration sees (a+1, b)
  51. a += 1;
  52.  
  53. goto tail_call;
  54. }
  55. }
  56.  
  57. loop:
  58. if (condition) {
  59. ...magic...
  60. goto loop;
  61. }
  62.  
  63. while (condition) {
  64. ...magic...
  65. }
  66.  
  67. selection_sort(int a, int b){
  68. while (a != b+1) {
  69. int i = minIndex(arr, a, b);
  70. if(i != a)
  71. swap(arr, i, a);
  72.  
  73. a += 1;
  74. }
  75. }
  76.  
  77. selection_sort(){
  78. int firstIndex = 0;
  79. int lastIndex = arr.length;
  80. while(firstIndex < lastIndex) {
  81. int i = minIndex(arr, firstIndex, lastIndex);
  82. if(i != a) {
  83. swap(entries, i, firstIndex);
  84. }
  85. firstIndex ++;
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement