home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Include / bubbleSort.js next >
Encoding:
JavaScript  |  2002-05-13  |  2.1 KB  |  81 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. Sort by comparing each adjacent pair of items in a list in turn, 
  19. swapping the items if necessary, and repeating the pass through 
  20. the list until no swaps are done. 
  21.  
  22. Arguments:
  23.     <a> - an array of objects or values
  24.     <property> - an optional argument.  If <a> is an array of
  25.         objects, this is the property for which the sort will
  26.         be based upon.
  27.  
  28. Example:
  29. var a = new Object(); 
  30. a.x = -5; 
  31. var b = new Object(); 
  32. var c = new Object();
  33. c.x = 0; 
  34. var d = new Object() 
  35. d.x = 10; 
  36.  
  37. var testArray = new Array(d, c, b, a); 
  38. bubbleSort(testArray, "x"); 
  39. var i; 
  40. for(i = 0; i < testArray.length; i++)
  41. {
  42.     Console.write(testArray[i].x + "\n"); 
  43. }
  44.  
  45. ***************************************************************/
  46.  
  47. /***************************************************************
  48. DO NOT EDIT BELOW THIS LINE
  49. ***************************************************************/
  50.  
  51.  
  52. function swap(a, i, j)
  53. /*
  54. a: the array in which to swap elements
  55. i, j: indexes of the elements to swap
  56. */
  57. {
  58.     var t = a[i]; 
  59.     a[i] = a[j]; 
  60.     a[j] = t; 
  61. }
  62.  
  63. function bubbleSort(a, property)
  64. {
  65.     var i, j; 
  66.     if(property == null)
  67.     property = ""; 
  68.     else
  69.     property = "." + property; 
  70.     for(i = a.length-1; i>=0; i--)
  71.     for(j = 1; j <= i; j++)
  72.     {
  73.         $.bp(); 
  74.         if(eval("a[j-1]"+ property) > eval("a[j]"+property))
  75.         swap(a, j-1, j); 
  76.     }
  77. }
  78.  
  79.  
  80.  
  81.