Skip to content

Commit cc1ce9b

Browse files
committed
Array related Data Stucture using Python
0 parents commit cc1ce9b

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed

Array/Bubble Sort.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#Algorithms:
2+
3+
# The function "bubble_sort" takes a list 'my_list' as input and performs bubble sort on it. It returns the sorted list.
4+
# 1. Set variable 'n' to the length of my_list.
5+
# 2. [Outer Loop – 3 to 6 steps] Iterate over each element in the range from 0 to n:
6+
# 3. [Inner Loop – 4 and 5 steps] Iterate over each element in the range from 0 to n-1:
7+
# 4. Check if the current element at index j is greater than the next element at index j+1.
8+
# 5. If true, swap the elements at indices j and j+1 in my_list.
9+
# [End of Inner loop]
10+
# 6. Return the sorted my_list.
11+
# [End of Outer loop]
12+
# 7. Exit.
13+
14+
15+
16+
17+
#----------------------------------
18+
19+
#Python_Code
20+
21+
def bubble_sort(my_list):
22+
n = len(my_list)
23+
24+
for i in range(n):
25+
for j in range(n - 1):
26+
if my_list[j] > my_list[j + 1]:
27+
my_list[j], my_list[j + 1] = my_list[j + 1], my_list[j]
28+
29+
return my_list
30+
31+
Array_len = int(input("Enter the array lenght: "))
32+
Array = []
33+
34+
print("Enter the", Array_len, "element of array: ")
35+
36+
for i in range(Array_len):
37+
Array.append(int(input(f"Enter no. {i+1} element: ")))
38+
i = i + 1
39+
40+
41+
print("Original Array:", Array)
42+
43+
sorted_array = bubble_sort(Array)
44+
print("Sorted Array:", sorted_array)

Array/Deletion Array.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#Algorithms:
2+
3+
# The function "delete index" takes a list 'my_list', and the position at which the value should be inserted 'delete_position'. It returns # a new list with removes the element at the specified position in the input list and returns a new list without that element.
4+
# 1. Set an empty list new_list.
5+
# 2. Set variables 'i' for index and 'j' the length of my_list.
6+
# 3. [Repeat Steps 4 and 5] While i < j
7+
# 4. Check If i != delete_position, add the value’s to new_list one by one.
8+
# 5. [Increase Counter] Set i:= i-1.
9+
# [End the loop]
10+
# 7. Return new_list.
11+
# 8. Exit
12+
13+
14+
15+
#----------------------------------
16+
17+
#Python_Code
18+
19+
def delete_index(my_list, delete_position):
20+
new_list = []
21+
i = 0
22+
j = len(my_list)
23+
delete_position = delete_position - 1
24+
25+
while i < j:
26+
if i != delete_position:
27+
new_list = new_list + [my_list[i]]
28+
i += 1
29+
30+
return new_list
31+
32+
33+
Array_len = int(input("Enter the array lenght: "))
34+
Array = []
35+
36+
print("Enter the", Array_len, "element of array: ")
37+
38+
for i in range(Array_len):
39+
Array.append(int(input(f"Enter no. {i+1} element: ")))
40+
i = i + 1
41+
42+
43+
k = int(input("Position: "))
44+
45+
if 1 <= k < (len(Array) + 1):
46+
print("Original Array:", Array)
47+
48+
Output = delete_index(Array, k)
49+
print("Modified Array:", Output)
50+
else:
51+
print("Array:", Array)
52+
print("Delete position Error!")

Array/Insertion (Array).py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#Algorithms:
2+
3+
# The function "insert_value" takes a list 'my_list', a value to be 'inserted value', and the position at which the value should be inserted 'insert_position'. It returns a new list with the value inserted at the specified position.
4+
5+
# 1. Set an empty list new_list.
6+
# 2. Set variables 'i' for index and 'j' the length of my_list.
7+
# 3. [Repeat Steps 4 to 6] While i <= j
8+
# 4. Check If i = insert_position, add the value to new_list.
9+
# 5. Check If i < j, add the current element of my_list to new_list.
10+
# 6. [Increase Counter] Set i:= i-1.
11+
# [End the loop]
12+
# 7. Return new_list.
13+
# 8. Exit
14+
15+
16+
17+
18+
#----------------------------------
19+
20+
#Python_Code
21+
22+
23+
def insert_value(my_list, value, insert_position):
24+
new_list = []
25+
i = 0
26+
j = len(my_list)
27+
insert_position = insert_position - 1
28+
29+
while i <= j:
30+
if i == insert_position:
31+
new_list = new_list + [value]
32+
if i < j:
33+
new_list = new_list + [my_list[i]]
34+
i += 1
35+
return new_list
36+
37+
38+
Array_len = int(input("Enter the array lenght: "))
39+
Array = []
40+
41+
print("Enter the", Array_len, "element of array: ")
42+
43+
for i in range(Array_len):
44+
Array.append(int(input(f"Enter no. {i+1} element: ")))
45+
i = i + 1
46+
47+
print("Original Array:", Array)
48+
49+
item = int(input("Value: "))
50+
k = int(input("Position: "))
51+
52+
if 1 <= k <= (len(Array) + 1):
53+
print("Original Array:", Array)
54+
55+
Output = insert_value(Array, item, k)
56+
print("Modified Array:", Output)
57+
else:
58+
print("Array:", Array)
59+
print("Inserted position Error!")

Array/Linear Search.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#Algorithms:
2+
3+
# 1. Set a list named 'list' with values.
4+
# 2. Get user input for the search value and store it in the variable 'search'.
5+
# 3. Set a variable 'found' to False. (Because, Program hasn’t started yet.)
6+
# 4. Initialize a variable 'i' to 0 for indexing.
7+
# 5. [Use FOR loop] For each 'item' in 'my_list':
8+
# a. Check if 'search' == 'item'.
9+
# b. If equal, set 'found' to True, break out of the loop.
10+
# c. Set i:= i+1.
11+
# [End the loop]
12+
# 6. Check if 'found' is True:
13+
# a. Print the search value, ‘position’, and a message indicating it is found.
14+
# 7. If 'found' is False:
15+
# a. Print a message indicating the search value is not found.
16+
# 8. Exit.
17+
18+
19+
20+
21+
#----------------------------------
22+
23+
#Python_Code
24+
25+
list_len = int(input("Enter the list lenght: "))
26+
list = []
27+
28+
print("Enter the", list_len, "element of list...")
29+
30+
for i in range(list_len):
31+
list.append(input(f"Enter no. {i+1} element: "))
32+
i = i + 1
33+
34+
print("Original list:", list)
35+
36+
37+
search = input("Enter your Search value: ")
38+
found = False
39+
i = 0
40+
for item in list:
41+
if search == item:
42+
found = True
43+
break
44+
i += 1
45+
46+
if found == True:
47+
print(search, "is found and ", search, "position", i + 1)
48+
else:
49+
print(search, "is not found")

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# DSA-Python

0 commit comments

Comments
 (0)