Skip to content

Commit 3896a45

Browse files
authored
Update Selection_Sort
1 parent d27777a commit 3896a45

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

Selection_Sort

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
# Problem =>
77
# Implementation of Selection - sort
88
# Thoughts =>
9-
# Insertion sort pick minimum element from range [i+1..N] and swap it with the
9+
# Insertion sort pick minimum element from range [i+1..N]
10+
# and swap it with the
1011
# ith element with reference to the outer loop.
11-
# The algorithms sort the numbers in place so the space - Complexity will
12+
# The algorithms sort the numbers in place so the space -
13+
# Complexity will
1214
# be O(1) just like the insertion sort.
1315
# Time - Complexity =>
1416
Best - Ω(n)
@@ -20,38 +22,50 @@
2022
"""
2123
import random
2224
import time
23-
def selection_Sort(x):
24-
l = []
25-
for j in range(x):
26-
l.append(random.randint(-9999, 9999))
27-
N = len(l)
28-
start_time = time.time()
29-
for i in range(0,N-1):
30-
min = l[i]
31-
index = i
32-
flag = False
33-
for j in range(i+1,N):
34-
if l[j] < min:
35-
min = l[j]
36-
index = j
37-
flag = True
38-
if flag:
39-
temp = l[i]
40-
l[i] = min
41-
l[index] = temp
42-
return time.time() - start_time
4325

44-
print("The round-time elapsed to sort 1000 elements by selection sort ", round(selection_Sort(1000)))
45-
print("The round-time elapsed to sort 2000 elements by selection sort ", round(selection_Sort(2000)))
46-
print("The round-time elapsed to sort 4000 elements by selection sort ", round(selection_Sort(4000)))
47-
print("The round-time elapsed to sort 8000 elements by selection sort ", round(selection_Sort(8000)))
48-
print("The round-time elapsed to sort 16000 elements by selection sort ", round(selection_Sort(16000)))
26+
27+
def Selection_Sort(noOfElements):
28+
listOfElements = []
29+
for i in range(noOfElements):
30+
listOfElements.append(random.randint(0, 10))
31+
listOfElements.sort()
32+
listOfElements.reverse()
33+
# print("Before sorting",listOfElements)
34+
start = time.time()
35+
for i in range(noOfElements - 1):
36+
key = listOfElements[i]
37+
j = i + 1
38+
for k in range(j, noOfElements):
39+
if listOfElements[k] < key:
40+
key = listOfElements[k]
41+
index = k
42+
listOfElements[i], listOfElements[index] =
43+
listOfElements[index], listOfElements[i]
44+
# print("For ith loop ",i , listOfElements)
45+
# print("After Sorting", listOfElements)
46+
end = time.time()
47+
return end - start
48+
49+
print (
50+
"Time taken to sort 4000 elements with Selection_Sort ",
51+
round(Selection_Sort(4000))
52+
)
53+
print (
54+
"Time taken to sort 8000 elements with Selection_Sort ",
55+
round(Selection_Sort(8000))
56+
)
57+
print (
58+
"Time taken to sort 16000 elements with Selection_Sort ",
59+
round(Selection_Sort(16000))
60+
)
61+
print (
62+
"Time taken to sort 32000 elements with Selection_Sort ",
63+
round(Selection_Sort(32000))
64+
)
4965

5066
'''
51-
Output :-
52-
The round-time elapsed to sort 1000 elements by selection sort 0
53-
The round-time elapsed to sort 2000 elements by selection sort 0
54-
The round-time elapsed to sort 4000 elements by selection sort 1
55-
The round-time elapsed to sort 8000 elements by selection sort 4
56-
The round-time elapsed to sort 16000 elements by selection sort 17
67+
Time taken to sort 4000 elements with Selection_Sort 1
68+
Time taken to sort 8000 elements with Selection_Sort 4
69+
Time taken to sort 16000 elements with Selection_Sort 16
70+
Time taken to sort 32000 elements with Selection_Sort 67
5771
'''

0 commit comments

Comments
 (0)