-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_InsertionSort_and_SelectionSort.py
More file actions
68 lines (55 loc) · 1.99 KB
/
8_InsertionSort_and_SelectionSort.py
File metadata and controls
68 lines (55 loc) · 1.99 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Implementation of Insertion sort and selection sort
"""# INSERTION SORT
# Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time.
# It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
# Worst complexity: n^2
# Average complexity: n^2
# Best complexity: n
# Space complexity: 1
# SELECTION SORT
# In computer science, selection sort is an in-place comparison sorting algorithm.
# It has an O(n²) time complexity, which makes it inefficient on large lists,
# and generally performs worse than the similar insertion sort.
# Worst complexity: n^2
# Average complexity: n^2
# Best complexity: n^2
# Space complexity: 1"""
class Sort:
def InsertionSort(self):
value = input('Enter the list of numbers: ').split()
value = [int(x) for x in value]
print("Given List: ", value)
for i in range(1,len(value)):
key = value[i]
temp = i - 1
while key < value[temp]and temp>=0:
value[temp+1] = value[temp]
temp = temp-1
value[temp+1] = key
print("Sorted List: ", value)
def SelectionSort(self):
data = input('Enter the list of numbers: ').split()
data = [int(x) for x in data]
print("Given List: ", data)
for i in range(0, len(data) - 1):
minimum = i
for j in range(i + 1, len(data)):
if data[j] < data[minimum]:
minimum = j
data[i],data[minimum]=data[minimum],data[i]
print('Sorted List: ', data)
MenuDriven = Sort()
while(True):
print("\n1. INSERTION SORT")
print("2. SELECTION SORT")
print("3. EXIT")
userChoice = int(input("Enter your choice: "))
if userChoice == 1:
MenuDriven.InsertionSort()
elif userChoice == 2:
MenuDriven.SelectionSort()
elif userChoice == 3:
print("\nThanks for Performing Sorting Algorithms! Thank You by URK20CS2001")
break
else:
print("\nINVALID INPUT")