# Cocktail sort

```ruby
func cocktailsort(a) {
    var swapped = false
    func cmpsw(i) {
        if (a[i] > a[i+1]) {
            a[i, i+1] = a[i+1, i]
            swapped = true
        }
    }
    var max = a.end
    do {
        {|i| cmpsw(i) } << ^max
        swapped.not! && break
        {|i| cmpsw(max-i) } << 1..max
    } while (swapped)
    return a
}
```

Test:

```ruby
var numbers = [7,6,5,9,8,4,3,1,2,0]
say cocktailsort(numbers)

var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"]
say cocktailsort(strs)
```

#### Output:

```
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
['Alice', 'Jane', 'Joe', 'John', 'Kate', 'Zerg']
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/s/sorting-algorithms/cocktail_sort.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
