# Quicksort and random testing

We implemented a other sorting algorithm (Quicksort), and a random tester to compare the results of Quicksort and Mergesort.

## Mergesort

In `mergesort.py`:

```def merge(l1: list, l2: list) -> list:
index1 = 0
index2 = 0
result = []
while index1 < len(l1) or index2 < len(l2):
if index1 >= len(l1):
result.append(l2[index2])
index2 += 1
elif index2 >= len(l2):
result.append(l1[index1])
index1 += 1
elif l1[index1] < l2[index2]:
result.append(l1[index1])
index1 += 1
else:
result.append(l2[index2])
index2 += 1
return result

def mergesort(l: list) -> list:
if len(l) <= 1:
return l[:]
l1 = l[:len(l) // 2]
l2 = l[len(l) // 2:]
l1_sorted = mergesort(l1)
l2_sorted = mergesort(l2)
return merge(l1_sorted, l2_sorted)

```

## Quicksort

In `quicksort.py`:

```def quicksort(l: list) -> list:
if len(l) <= 1:
return l[:]
pivot = l[0]
smaller = [x for x in l if x < pivot]
equal = [x for x in l if x == pivot]
larger = [x for x in l if x > pivot]
smaller_sorted = quicksort(smaller)
larger_sorted = quicksort(larger)
return smaller_sorted + equal + larger_sorted
```

## Random testing

In `test_sorting.py`:

```from random import randint
from quicksort import quicksort
from mergesort import mergesort

MAX_LENGTH = 5000
MIN_VALUE = -100
MAX_VALUE = 100
NUM_LISTS = 100

def random_list() -> list:
length = randint(0, 5000)
return [randint(MIN_VALUE, MAX_VALUE) for i in range(length)]

def test_quicksort_mergesort():
for i in range(NUM_LISTS):
lst = random_list()
assert quicksort(lst) == mergesort(lst)

```