Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. var arr = [5, 56, 33, 54, 37, 67, 84, 38, 44, 43, 94, 49];
  2.  
  3. //Bubble
  4. for (i = 0; i < arr.length; i++) {
  5. for (j = 0 ; j < (arr.length - i); j++) {
  6. if (arr[j] > arr[j+1]) {
  7. temp = arr[j];
  8. arr[j] = arr[j+1];
  9. arr[j+1] = temp;
  10. }
  11. }
  12. }
  13. console.log(arr);
  14.  
  15. // Insertion
  16. for (i = 0; i < arr.length; i++) {
  17. for (j = i ; j >= 0; j--) {
  18. if (arr[j] > arr[j+1]) {
  19. temp = arr[j];
  20. arr[j] = arr[j+1];
  21. arr[j+1] = temp;
  22. }
  23. }
  24. }
  25. console.log(arr);
  26.  
  27.  
  28. // Selection
  29. for (i = 0; i < arr.length; i++) {
  30. min = i;
  31. for (j = i+1 ; j < arr.length ; j++) {
  32. if (arr[j] < arr[min]) {
  33. min = j;
  34. }
  35. }
  36.  
  37. temp = arr[i];
  38. arr[i] = arr[min];
  39. arr[min] = temp;
  40.  
  41. }
  42. console.log(arr);
  43.  
  44. // Merge
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement