# Bubblesort Algorithm

## Overview

The Bubblesort Algorithm is one of many algorithms used to sort a list of similar items (e.g. all numbers or all letters) into either ascending order or descending order.  Given a list (e.g.):

```python
[9, 7, 5, 3, 1, 2, 4, 6, 8]
```

To sort this list in ascending order using Bubblesort, you first have to compare the first two terms of the list.  If the first term is larger than the second term, you perform a swap.  The list then becomes:

```python
[7, 9, 5, 3, 1, 2, 4, 6, 8] # The "9" and "7" have been swapped because 9 is larger than 7 and thus 9 should be after 7
```

You then proceed by comparing the 2nd and 3rd terms, performing a swap *when necessary*, and then the 3rd and 4th term, then the 4th and 5th term, etc. etc.  When you reach the end of the list, it is said that you have completed **1 complete pass**.

## Task

Given an array of integers, your function `bubblesortOnce`/`bubblesort_once`/`BubblesortOnce` (or equivalent, depending on your language's naming conventions) should return a *new* array equivalent to performing exactly **1 complete pass** on the original array.  Your function should be pure, i.e. it should **not** mutate the input array.

Source: codewars | Difficulty: EASY
Tags: Sorting

Starter code: def bubblesort_once(l):
	

Solutions:
def bubblesort_once(l):
	l = l[:]
	for i in range(len(l) - 1):
		if l[i] > l[i + 1]:
			(l[i], l[i + 1]) = (l[i + 1], l[i])
	return l


def bubblesort_once(L):
	if len(L) <= 1:
		return L
	if L[0] <= L[1]:
		return [L[0]] + bubblesort_once(L[1:])
	return [L[1]] + bubblesort_once([L[0]] + L[2:])


def bubblesort_once(lst):
	result = lst[:]
	for i in range(len(result) - 1):
		if result[i] > result[i + 1]:
			(result[i], result[i + 1]) = (result[i + 1], result[i])
	return result


import copy

def bubblesort_once(l):
	res = copy.copy(l)
	for i in range(len(res) - 1):
		if res[i] > res[i + 1]:
			(res[i], res[i + 1]) = (res[i + 1], res[i])
	return res


def bubblesort_once(l):
	values = l[:]
	for i in range(0, len(values) - 1):
		first = values[i]
		second = values[i + 1]
		if first > second:
			values[i + 1] = first
			values[i] = second
	return values


def bubblesort_once(arr):
	new_arr = arr.copy()
	for i in range(len(new_arr) - 1):
		if new_arr[i] > new_arr[i + 1]:
			(new_arr[i], new_arr[i + 1]) = (new_arr[i + 1], new_arr[i])
	return new_arr


def bubblesort_once(lst):
	arr = lst[:]
	for i in range(len(arr) - 1):
		(arr[i], arr[i + 1]) = sorted([arr[i], arr[i + 1]])
	return arr


def bubblesort_once(l):
	if len(l) < 2:
		return l[:]
	(lst, mx) = ([], l[0])
	for v in l[1:]:
		if mx < v:
			(mx, v) = (v, mx)
		lst.append(v)
	lst.append(mx)
	return lst


def bubblesort_once(l):
	output = [i for i in l]
	for i in range(len(output) - 1):
		(output[i], output[i + 1]) = (min(output[i], output[i + 1]), max(output[i], output[i + 1]))
	return output