Guest User

Untitled

a guest
Dec 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. function ScriptoniteSort() {
  2. this.arr = [];
  3. }
  4.  
  5. ScriptoniteSort.prototype = {
  6. swap: function(arr, index_one, index_two) {
  7. //swap
  8. var temp = arr[index_one];
  9. arr[index_one] = arr[index_two];
  10. arr[index_two] = temp;
  11. },
  12.  
  13. validateCollection: function(datalist) {
  14. if(arguments.length === 0 || !Array.isArray(this.arr)) {
  15. throw new Error('Either No Arguments Passed, Or Passed Param is not an Array');
  16. }
  17. },
  18.  
  19. //use this insertion sort when working with shell sort
  20. insertionSortGap: function(datalist, startIndex, increment) {
  21. //set length of array
  22. var dataLength = datalist.length;
  23. //create sub lists and do sorting
  24. for (var i = startIndex + increment; i < dataLength; i += increment) {
  25. var temp = this.arr[i];
  26. //placeholder is where we will insert
  27. var placeHolderIndex = i;
  28. //like the insertion sort method we do the same here only using the increment values
  29. while( (placeHolderIndex >= increment) && (this.arr[placeHolderIndex - increment] > temp) ) {
  30. //if condition is met then keep shifting the values
  31. this.arr[placeHolderIndex] = this.arr[placeHolderIndex - increment];
  32. placeHolderIndex -= increment;
  33. }
  34. //after shifting is all done, insert the value into right location
  35. this.arr[placeHolderIndex] = temp;
  36. }
  37. },
  38.  
  39. //this method uses two for loops instead of a while within a loop
  40. insertionSortGapAlternative: function(datalist, startIndex, increment) {
  41. //set length of array
  42. var dataLength = datalist.length;
  43. //create sub lists and do sorting
  44. for(var i = startIndex; i < dataLength; i += increment) {
  45. //console.log(this.arr[i]);
  46. for(var j = Math.min(i + increment, dataLength - 1); j - increment >= 0; j = j - increment) {
  47. if(this.arr[j] < this.arr[j - increment]) {
  48. this.swap(this.arr, j, j - increment);
  49. } else {
  50. break;
  51. }
  52. }
  53. }
  54. },
  55.  
  56. shellSort: function(datalist) {
  57. //assign the data property to the passed in datalist
  58. this.arr = datalist;
  59. //check if passed in is an array
  60. this.validateCollection(this.arr);
  61. //set the increment
  62. var increment = Math.floor(this.arr.length / 2);
  63. //a long as increment is greater than 1 call the modified insertion sort with gap
  64. while(increment >= 1) {
  65. for(var i = 0; i < increment; i++) {
  66. this.insertionSortGap(datalist, i, increment);
  67. //or call the other method insertion sort method
  68. //this.insertionSortGapAlternative(datalist, i, increment);
  69. }
  70. increment = Math.floor(increment / 2);
  71. }
  72. //display or return final array
  73. console.log("---->" + this.arr.toString());
  74. }
  75. };
  76.  
  77. var myArr = [9, 11, 5, 1, 7, 2, 15, 1, 8, 6];
  78. //var myArr = [1, 7, 4, 0, 5, 3];
  79. //var myArr = [64, 25, 12, 22, 11];
  80. var collection = new ScriptoniteSort();
  81. collection.insertionSort(myArr);
Add Comment
Please, Sign In to add comment