Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. let arrayToBeSort = [12,45,75,35,55,54,2,21]
  2.  
  3. // ## Bubble Sort
  4. /**
  5. *
  6. * Bubble sort is a simple sorting algorithm.
  7. * This sorting algorithm is comparison-based algorithm
  8. * in which each pair of adjacent elements is compared
  9. * and the elements are swapped if they are not in order.
  10. * This algorithm is not suitable for large data sets as
  11. * its average and worst case complexity are of Ο(n2)
  12. * where n is the number of items.
  13. */
  14. function bubbleSort(arrayToBeSort){
  15. // take the length of array
  16. let arrayLength = arrayToBeSort.length
  17. for(let i = 1; i <= arrayLength; i++){
  18. for(let j = 0; j < arrayLength - i ; j++){
  19. if(arrayToBeSort[j] > arrayToBeSort[j+1]){
  20. let temp = arrayToBeSort[j]
  21. arrayToBeSort[j] = arrayToBeSort[j+1]
  22. arrayToBeSort[j+1] = temp
  23. }
  24. }
  25. }
  26. }
  27.  
  28. // ## Insertion Sort
  29. /**
  30. *
  31. * This is an in-place comparison-based sorting algorithm. Here,
  32. * a sub-list is maintained which is always sorted. For example,
  33. * the lower part of an array is maintained to be sorted.
  34. * An element which is to be 'insert'ed in this sorted sub-list,
  35. * has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort.
  36. * The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array).
  37. * This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2),
  38. * where n is the number of items.
  39. */
  40. function insertionSort(arrayToBeSort){
  41. let arrayLength = arrayToBeSort.length
  42. for(let i = 0; i < arrayLength; i++){
  43. for(let j = i; j > 0; j --){
  44. if(arrayToBeSort[j-1] > arrayToBeSort[j]){
  45. let temp = arrayToBeSort[j-1]
  46. arrayToBeSort[j-1] = arrayToBeSort[j]
  47. arrayToBeSort[j] = temp
  48. }
  49. else
  50. break;
  51. }
  52. }
  53. }
  54.  
  55. //##
  56. /**
  57. *
  58. * Selection sort is a simple sorting algorithm.
  59. * This sorting algorithm is an in-place comparison-based algorithm
  60. * in which the list is divided into two parts, the sorted part at
  61. * the left end and the unsorted part at the right end. Initially,
  62. * the sorted part is empty and the unsorted part is the entire list.
  63. *
  64. * The smallest element is selected from the unsorted array and swapped
  65. * with the leftmost element, and that element becomes a part of the sorted array.
  66. * Thisprocess continues moving unsorted array boundary by one element to the right.
  67. *
  68. * This algorithm is not suitable for large data sets as its average and worst case
  69. * complexities are of Ο(n2), where n is the number of items.
  70. */
  71.  
  72. function selectionSort(arrayToBeSort){
  73. let arrayLength = arrayToBeSort.length
  74. for(let i = 0; i < arrayLength -1; i++){
  75. let minIndex = i;
  76. for(let j = minIndex + 1; j < arrayLength; j++ ){
  77. if(arrayToBeSort[minIndex] > arrayToBeSort[j])
  78. minIndex = j
  79. }
  80. if(minIndex !== i){
  81. let temp = arrayToBeSort[i]
  82. arrayToBeSort[i] = arrayToBeSort[minIndex]
  83. arrayToBeSort[minIndex] = temp
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement