Underrated step for logic building in programming.

Image
Logic building is a crucial and complex skill in programming. In essence, it is ability to come-up with solution of coding problem and write precise instructions ( or code) that a computer can execute autonomously. This skill requires aligning your thought process with computer and its capabilities. And running through code some-what abstractly to know and predict the behavior of code before it is executed. To be able to do this, one essential step that many beginner programmers overlook is performing dry runs. Understanding Dry Runs The concept of a dry run in programming is straightforward: can you mentally execute your code and predict its output without actually running it on a computer? While this seems simple, it is a challenging task. Typically, we are taught to write code, run it, and observe the output. This cycle is essential because code needs to run to be validated. However, if you rely solely on running your code to understand its behavior, you may struggle with building

Python code #13

1. 2D array bubble sort of array with 3 columns

def bubble_sort_2d(arr):

    rows = len(arr)

for i in range(rows):
for j in range(rows - i - 1):
if arr[j][0] > arr[j + 1][0]:
temp1 = arr[j][0]
arr[j][0] = arr[j + 1][0]
arr[j + 1][0] = temp1

temp2 = arr[j][1]
arr[j][1] = arr[j + 1][1]
arr[j + 1][1] = temp2

temp3 = arr[j][2]
arr[j][2] = arr[j + 1][2]
arr[j + 1][2] = temp3

return arr


two_d_array = [
[4, 2, 3],
[1, 5, 6],
[7, 8, 9],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
[19, 20, 21],
[8, 23, 24],
[28, 26, 27],
[21, 29, 30]
]

sorted_array = bubble_sort_2d(two_d_array)

for row in sorted_array:
print(row)
---------------------------------------------------------------------------------------------
def bubble_sort_2d(arr):
rows = len(arr)
cols = len(arr[0])

for i in range(rows):
for j in range(rows - i - 1):
if arr[j][0] > arr[j + 1][0]:
# Use a loop to swap elements in all columns
for k in range(cols):
temp = arr[j][k]
arr[j][k] = arr[j + 1][k]
arr[j + 1][k] = temp

return arr


two_d_array = [
[4, 2, 3],
[1, 5, 6],
[7, 8, 9],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18],
[19, 20, 21],
[8, 23, 24],
[28, 26, 27],
[21, 29, 30]
]

sorted_array = bubble_sort_2d(two_d_array)

for row in sorted_array:
print(row)
---------------------------------------------------------------------------------------------
2. 1D array bubble sort:
def bubble_sort(arr):
array_length = len(arr)

for i in range(array_length - 1):
for j in range(array_length - i - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp

return arr

array = [3,4,9,1,4]

print(bubble_sort(array))
---------------------------------------------------------------------------------------------
def bubble_sort(arr):
array_length = len(arr)

for i in range(array_length - 1):
for j in range(array_length - i - 1):
if arr[j] > arr[j + 1]:
temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp

print(arr)

array = [3,4,9,1,4]
bubble_sort(array)

Comments

Popular posts from this blog

Building JavaScript Array Methods from Scratch in 2024 - Easy tutorial for beginners # 1

Python Code # 12

A better way to learn programming.