# Sort - Squash-PHP/Squash GitHub Wiki

## Bubble Sort

The `bubble`

function sorts an array using the bubble sort algorithm.

### Parameters

`array $arr`

: The array to be sorted.

### Return Value

Returns the sorted array.

### Example

```
$unsortedArray = [64, 34, 25, 12, 22, 11, 90];
$sortedArray = $Squash->sort->bubble($unsortedArray);
print_r($sortedArray); // Outputs: [11, 12, 22, 25, 34, 64, 90]
```

### Description

The function works by repeatedly swapping the adjacent elements if they are in wrong order. It continues its pass through the list until it makes no more swaps, at which point the list is sorted.

## Insertion Sort

The `insertion`

function sorts an array using the insertion sort algorithm.

### Parameters

`array $arr`

: The array to be sorted.

### Return Value

Returns the sorted array.

### Example

```
$unsortedArray = [4, 3, 2, 10, 12, 1, 5, 6];
$sortedArray = $Squash->sort->insertion($unsortedArray);
print_r($sortedArray); // Outputs: [1, 2, 3, 4, 5, 6, 10, 12]
```

### Description

The function works by dividing the input into a sorted and an unsorted region. The sorted region is initially the first element of the array, and with each iteration, one element from the unsorted region is picked and moved to the sorted region, at the correct position. This process continues until the unsorted region is empty and the sorted region contains all elements.

## Merge Sort

The `merge`

function sorts an array using the merge sort algorithm.

### Parameters

`array $arr`

: The array to be sorted.

### Return Value

Returns the sorted array.

### Example

```
$unsortedArray = [38, 27, 43, 3, 9, 82, 10];
$sortedArray = $Squash->sort->merge($unsortedArray);
print_r($sortedArray); // Outputs: [3, 9, 10, 27, 38, 43, 82]
```

#### Description

The function works by dividing the unsorted list into n sublists, each containing one element (a list of one element is considered sorted), then repeatedly merge sublists to produce new sorted sublists until there is only one sublist remaining. This will be the sorted list.

## Selection Sort

The `selection`

function sorts an array using the selection sort algorithm.

### Parameters

`array $arr`

: The array to be sorted.

### Return Value

Returns the sorted array.

### Example

```
$unsortedArray = [64, 25, 12, 22, 11];
$sortedArray = $Squash->sort->selection($unsortedArray);
print_r($sortedArray); // Outputs: [11, 12, 22, 25, 64]
```

### Description

The function works by dividing the input into a sorted and an unsorted region. The sorted region is initially empty, while the unsorted region contains all the elements. The function repeatedly selects the smallest (or largest, depending on the ordering) element from the unsorted region and moves it to the end of the sorted region. This process continues until the unsorted region is empty and the sorted region contains all elements.

## Selection Sort

The `selection`

function sorts an array using the selection sort algorithm.

### Parameters

`array $arr`

: The array to be sorted.

### Return Value

Returns the sorted array.

### Example

```
$unsortedArray = [64, 25, 12, 22, 11];
$sortedArray = $Squash->sort->selection($unsortedArray);
print_r($sortedArray); // Outputs: [11, 12, 22, 25, 64]
```

### Description

The function works by dividing the input into a sorted and an unsorted region. The sorted region is initially empty, while the unsorted region contains all the elements. The function repeatedly selects the smallest (or largest, depending on the ordering) element from the unsorted region and moves it to the end of the sorted region. This process continues until the unsorted region is empty and the sorted region contains all elements.

## Heap Sort

The `heap`

function sorts an array using the heap sort algorithm.

### Parameters

`array $arr`

: The array to be sorted.

### Return Value

Returns the sorted array.

### Example

```
$unsortedArray = [12, 11, 13, 5, 6, 7];
$sortedArray = $Squash->sort->heap($unsortedArray);
print_r($sortedArray); // Outputs: [5, 6, 7, 11, 12, 13]
```

### Description

The function works by building a max heap from the input data. Then, it moves the greatest item from the heap to the end of the sorted array. This process is repeated reducing the size of the heap until the heap is empty.

## Radix Sort

The `radix`

function sorts an array using the radix sort algorithm.

### Parameters

`array $arr`

: The array to be sorted.

### Return Value

Returns the sorted array.

### Example

```
$unsortedArray = [37, 89, 41, 65, 91, 53];
$sortedArray = $Squash->sort->radix($unsortedArray);
print_r($sortedArray); // Outputs: [37, 41, 53, 65, 89, 91]
```

### Description

The function works by sorting the numbers in the array by each digit, starting from the least significant digit and moving towards the most significant. This is done using counting sort as a subroutine. The process continues until the array is sorted.

## Quick Sort

The `quick`

function sorts an array using the quick sort algorithm.

### Parameters

`array $arr`

: The array to be sorted.

### Return Value

Returns the sorted array.

### Example

```
$unsortedArray = [3, 2, 5, 1, 4];
$sortedArray = $Squash->sort->quick($unsortedArray);
print_r($sortedArray); // Outputs: [1, 2, 3, 4, 5]
```

### Description

The function works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then recursively sorted. This process continues until the array is sorted.