Jump to content

_RippleSort()


buzz44
 Share

Recommended Posts

Ripple sort, also known as bidirectional bubble sort, cocktail shaker sort, shaker sort, cocktail sort, or shuttle sort.

It varies from bubble sort in that instead of repeatedly passing through the array from top to bottom, it passes alternately from top to bottom and then from bottom to top. It can achieve slightly better performance than a standard bubble sort.

;===============================================================================
;
; Function Name:    _RippleSort()
; Description:      Sort a 1 dimensional Array of numbers on a specific index
;                   using the Ripple algorithm.
; Parameter(s):     $avArray      - Array
;                   $iReverse - Sort ascending when 0 / Sort descending when 1
;                   $iStart   - Start sorting at this Array entry.
;                   $iEnd     - End sorting at this Array entry. Default UBound($avArray) - 1
;
; Requirement(s):   None
; Return Value(s):  On Success - 1 and the sorted array is set
;                   On Failure - 0 and sets @ERROR = 1 when $avArray is not an array
;===============================================================================

#include <Array.au3>

Dim $Array[10]

$Array[0] = 500
$Array[1] = 19
$Array[2] = 2
$Array[3] = 7
$Array[4] = 1
$Array[5] = 21
$Array[6] = 17
$Array[7] = 23
$Array[8] = 46
$Array[9] = 3

_ArrayDisplay($Array,"UnSorted" )
_RippleSort($Array)
_ArrayDisplay($Array,"Sort Ascending" )
_RippleSort($Array,1)
_ArrayDisplay($Array,"Sort Decending" )

Func _RippleSort($avArray, $iReverse = 0, $iStart = 0, $iEnd = 0)
   If Not IsArray($avArray) Then
      SetError(1) ; Isn't an array.
      Return 0
   EndIf
   If $iEnd < 1 Or $iEnd > UBound($avArray) - 1 Then $iEnd = UBound($avArray) - 1
   If $iStart < 0 Or $iStart > UBound($avArray) - 1 Then $iStart = 0
   If $iReverse <> 1 Then $iReverse = 0
   While $iEnd > $iStart
      For $I = $iStart To $iEnd - 1
         If $iReverse = 1 Then
            If $avArray[$I] < $avArray[$I + 1] Then _ArraySwap($avArray[$I], $avArray[$I + 1])
         ElseIf $iReverse = 0 Then
            If $avArray[$I] > $avArray[$I + 1] Then _ArraySwap($avArray[$I], $avArray[$I + 1])
         EndIf
      Next
      For $I = $iEnd - 2 To $iStart + 1 Step - 1
         If $iReverse = 1 Then
            If $avArray[$I] > $avArray[$I - 1] Then _ArraySwap($avArray[$I], $avArray[$I - 1])
         ElseIf $iReverse = 0 Then
            If $avArray[$I] < $avArray[$I - 1] Then _ArraySwap($avArray[$I], $avArray[$I - 1])
         EndIf
      Next
      $iEnd = $iEnd - 1
   Wend
   Return 1
EndFunc
Edited by buzz44

qq

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...