From c9d09cf41dcfe47fdd0703d580a6b20581ce1608 Mon Sep 17 00:00:00 2001 From: Mohamad El Bohsaly Date: Sat, 29 Oct 2022 12:38:28 +0300 Subject: [PATCH] Add a bubble sorting algorithm using a while loop --- Arrays and Matrix/sort.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Arrays and Matrix/sort.py diff --git a/Arrays and Matrix/sort.py b/Arrays and Matrix/sort.py new file mode 100644 index 0000000..fd2f917 --- /dev/null +++ b/Arrays and Matrix/sort.py @@ -0,0 +1,37 @@ +# Sort array/matrix +# Set a temporary variable that iterates over array elements +# Do a swapping between adjacent elements if necessary + +import random + +# temporary variable ready for swapping +temp = 0 + +# using random.sample() +# to generate random number list +data = random.sample(range(1, 50), 20) + + +def swap(temp, x, y): + temp = y + y = x + x = temp + + +outerIndex = 0 + +while outerIndex < len(data): + # Restart innerIndex so that it repeats scanning after doing a round of sorting + innerIndex = 0 + + # Include outerIndex so that we don't re-scan items already sorted + while innerIndex in range(len(data) - outerIndex - 1): + temp = data[innerIndex] + + # Do a swap between adjacent elements + if (temp > data[innerIndex+1]): + data[innerIndex], data[innerIndex + 1] = data[innerIndex + 1], data[innerIndex] + innerIndex += 1 + outerIndex += 1 + +print("Sorted list",data)