-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesortseq.py
More file actions
39 lines (31 loc) · 929 Bytes
/
bubblesortseq.py
File metadata and controls
39 lines (31 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Import required modules
import time
from random import randrange
# Select size of array to generate and sort
size = 1000
# The simple bublesort algorithm
def bubbleSort(arr):
n = len(arr)
# Traverse through array as many times as there are array elements
for i in range(n):
# Traverse the array from 0 to n-1
for j in range(0, n-1):
# Swap if the element found is greater than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
# Create an array of desired size
arr = [0] * size
# Initialize array with random numbers
for i in range(size) :
arr[i]=randrange(500)
t = time.time()
# Run the algorithm
bubbleSort(arr)
# Calculate how long it took
t2 = time.time() - t
# Print out the sorted array
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i]),
# Print algorithm runtime
print(t2)