home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Include / shellSort.js < prev    next >
Encoding:
JavaScript  |  2002-05-13  |  2.5 KB  |  89 lines

  1. /***************************************************************
  2. ADOBE SYSTEMS INCORPORATED 
  3. Copyright 2002 Adobe Systems Incorporated 
  4. All Rights Reserved 
  5.  
  6. NOTICE:  Adobe permits you to use, modify, and distribute this 
  7. file in accordance with the terms of the Adobe license agreement 
  8. accompanying it.  If you have received this file from a source 
  9. other than Adobe, then your use, modification, or distribution
  10. of it requires the prior written permission of Adobe. 
  11. ***************************************************************/
  12. /***************************************************************
  13. Author: Mary Obelnicki
  14. ***************************************************************/
  15.  
  16. /***************************************************************
  17.  
  18. A multipass, in-place sort algorithm which, on each pass, sorts 
  19. i sets of n/i items, where n is the total number of items to sort.
  20. Originally the first increment was half the number of items to be 
  21. sorted, i.e., n/2, and each succeeding increment was half the 
  22. preceding increment. 
  23.  
  24. Arguments:
  25.     <a> - an array of objects or values
  26.     <property> - an optional argument.  If <a> is an array of
  27.         objects, this is the property for which the sort will
  28.         be based upon.
  29.  
  30. Example:
  31. var a = new Object(); 
  32. a.x = -5; 
  33. var b = new Object(); 
  34. var c = new Object();
  35. c.x = 0; 
  36. var d = new Object() 
  37. d.x = 10; 
  38.  
  39. var testArray = new Array(d, c, b, a); 
  40. shellSort(testArray, "x"); 
  41. var i; 
  42. for(i = 0; i < testArray.length; i++)
  43. {
  44.     Console.write(testArray[i].x + "\n"); 
  45. }
  46.  
  47. ***************************************************************/
  48.  
  49. /***************************************************************
  50. DO NOT EDIT BELOW THIS LINE
  51. ***************************************************************/
  52.  
  53. function shellSort(a, property)
  54. /*
  55. sorts the array <a> using shell sort.  Optionally, if the array 
  56. contains objects, the objects can be sorted by <property>, 
  57. a property of the objects. 
  58.  
  59. a: the array to sort
  60. property: if a is an array of Objects, the property the objects will
  61. be sort by. all objects must have this property to be sorted properly. 
  62.  */
  63. {
  64.     if(property == null)
  65.     property = ""; 
  66.     else
  67.     property = "." + property; 
  68.     
  69.     var i, j, h, v; 
  70.     for(h = 1; (h *3 + 1 < a.length) ; h = 3*h+1); 
  71.     for( ; h > 0; h = Math.floor(h/3))
  72.     {
  73.     for(i = h; i < a.length; i++)
  74.     {
  75.         v = a[i]; 
  76.         j = i; 
  77.         while(j>=h && 
  78.           eval("a[j-h]"+property) > eval("v"+property))
  79.         {
  80.         a[j] = a[j-h]; 
  81.         j -= h; 
  82.         }
  83.         a[j] = v; 
  84.     }
  85.     }
  86. }
  87.  
  88.  
  89.