Archive
Python: Sort Algorithm – 3 – Insertion Sort
Learning : Python coding, Math, Algorithm,
Subject: Writing Merge Sorting Algorithm in Python
[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]
Sorting Algorithm is a way to sort a given list/Array of numbers, there are several sorting Algorithm as follow:
Type of Sorting Algorithm
1. Quick Sort. [Click Here]
2. Bubble Sort. [Click Here]
3. Merge Sort. [Click Here]
4. Insertion Sort. [Click Here]
5. Selection Sort. [Click Here]
6. Heap Sort. [Click Here]
7. Radix Sort. [Click Here]
8. Bucket Sort. [Click Here]
Here in this post we will write a function to take a given list and sort it then pass it back. We assume the user will enter a serial of numbers, that he want to sort, our function will sort it and print out the original numbers and the sorted one.
Insertion Sort: Steps of Insertion Sorting Algorithm are:
1. Start with index (x = 1), Compare the Element (k) in index [x] with the Element in index [x-1].
1.1 If Element in [x-1] SMALLER than Element in [x] we swap the two elements.
1.2 Once we Swap we will have new index for k , and again we will compare the (k) in (new index) with element before it (new index -1), and keep moving it to left until we stop at index [0] or we face an Element GRATER than k.
2. If the Element k GRATER than the element before it, we left k and take the Next Element (to be k) and start comparing K [in x index] with Element in [x-1] index.
3. We do this until k will be in index (length on the list)
Now starting with the codes, as our standard we follow in Sorting Algorithm Applications we will have a Main-Menu and three Items the user will chose among them, and Function to let the user to enter the Numbers [The List or the Array] to be sorted. The Options in the Main-Menu are :
1. Insertion Sort Algorithm – Fast Run.
2. Insertion Sort Algorithm – Step By Step.
9. Exit.
The Fasr-Run we call the create_list(): Function first so the user will Enter the Numbers in the Array, then the Fast-Run will show the sorted Array on the screen.
In the Step-By-Step (Option 2 in the Menu) again calling create_list(): Function first, after the user Enters the Array, we will print-out on the screen each steps of selecting the index, and index-1, the k values and when/if we will SWAP or not.
The last option in the Menu is to Exit the Application (option 9).
Now we start coding.. Here is the Main-Menu.
# Main-Menu
def main_menu ():
os.system('clear')
print('\n\n',' '*5,'******************************')
print(' '*5,' ***',' Sorting Algorithm ',' '*1,'***')
print(' '*5,' ***',' Insertion Sort ',' '*1,'***')
print(' '*5,' ***',' '*22,'***')
print(' '*5,' ******************************\n\n')
print(' '*7,'1. Insertion Sort Algorithm - Fast Run.')
print(' '*7,'2. Insertion Sort Algorithm - Step By Step.')
print(' '*7,'9. Exit.')
user_choice = input('\n Select your choice. > ')
return user_choice
Here is the codes for create_list(): to collect the Array from the user..
|
|
Finaly, here is the code for the Step-by-Step running for the Insetion Sort Algorithm. It is the same copy of the Fast-Run but with some print statements to show what is happening.
|
We Done another coding for Sorting Algorithms, another one will be published in coming days..
… Have fun with Coding … 🙂
To Download my Python code (.py) files Click-Here
By: Ali Radwani
Project: Knapsack Problem
Learning : Python, Math, Algorithm
Subject: Solving Knapsack Problem using Python
[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]
Definition: The knapsack problem is a Problem in Combinatorial Optimization: Given a set of Items, Each with a Weight and a Value or Profit, We need to Determine the Number of Each Item to Include in a Collection so that the Total Weight is Less than or Equal to a Given Limit and the Total Value is as Large as Possible. Source: Wikipedia
In this post we will write three Functions, The Main Menu, one to Collect the data and another to solve the problem. So first, let’s see the Main-Menu ..
# Main Menu of the Project
def main_menu ():
os.system('clear')
print('\n',' '*5,'******************************')
print(' '*5,' ***',' Knapsack Problem',' '*1,'***')
print(' '*5,' ***',' '*22,'***')
print(' '*5,' ******************************')
print('\n',' '*5,"==========[ Main Menu ]==========")
print(' '*5,' 1. About Knapsack Problem.')
print(' '*5,' 2. Collect the Items.')
print(' '*5,' 3. Solve the Problem.')
print(' '*5,' 9. Exit.')
user_choice = input("\n Select from the Menu: > ")
return user_choice
Above Menu will display three option that the user can select from:
1. About Knapsack Problem. [To give simple information about what is Knapsack Problem]
2. Collect the Items. [Will ask the user to Enter the Items and their coresponding Weights and Profits.]
3. Solve the Problem. [The user will Enter the Weight limit we have then we will Solve the problem]
Now we will write the Function to collect the Data from the user we will call it def collect_items(): the user will Enter the Item Name, the Weight and the Value or Profit and will save it in a list, then will return it as item_list. Here is the code and run-time screen.
![]() |
![]() |
After Collecting the Items, the user can select Number (3) from the Menu to Solve the Knapsack Problem. First we will ask user to Enter the Weights Limit we have, then calculating the Profit over Weight for each Items. In Knapsack we select the Items based on the Max w/p for each and store the indexs in a list, and with each selection we must not exceed the weight limits. Here is the code.. ..
![]() |
![]() |
So from the above example, we can achieve the Maximum Profit with weight limits to 50Kg if we take Full Amount of Item a, and Full Amount of Item b and 0.666666666 (0.67) amount of Item c.
1 * 60 = 60
1 * 100 = 100
0.67 * 120 = 80
60 + 100 + 80 = 240
NOTE: The Weight in Knapsack Problem can be weight in kg, or Number/Amount of the item (60 bags, 100 bags ..) or any Unit.
Have fun and do some coding .. 🙂
To Download my Python code (.py) files Click-Here
By: Ali Radwani
Python: Sorting Algorithm (3. Merge Sort)
Learning : Python coding, Math, Algorithm,
Subject: Writing Merge Sorting Algorithm in Python
[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]
Sorting Algorithm is a way to sort a given list/Array of numbers, there are several sorting Algorithm as follow:
Type of Sorting Algorithm
1. Quick Sort. [Click Here]
2. Bubble Sort. [Click Here]
3. Merge Sort. [Click Here]
4. Insertion Sort. [Click Here]
5. Selection Sort. [Click Here]
6. Heap Sort. [Click Here]
7. Radix Sort. [Click Here]
8. Bucket Sort. [Click Here]
Here in this post we will write a function to take a given list and sort it then pass it back. We assume the user will enter a serial of numbers, that he want to sort, our function will sort it and print out the original numbers and the sorted one.
Merge Sort: Steps of Merge Sorting Algorithm are:
1. If the Array has 1 Element then return it.
2. If the givin Array has more than 1 Element then we Divide it into 2 Parts.
Left = from Element 0 T0 Element len(arr)//2 and Right = from Element len(arr)//2 To Element len(arr). And we keep repeating step 2 for each part.
3. Once we have All parts divided we tart merge them in a sorted order.
Coding: In this Post we will write the Function for Main-Mnu, another one to collect the Array [Not Sorted] Elements from the user, then we will write the Main Function, here we will have One to Divide the Array into parts and One to Merge the Element together again. And as we done with Quick & Bubble Sort projects we will have one Fast-Run Function and another one with Details Step-By-Step run to show the Algorithm.
NOTE: All the Code will be Available in the Download Page.
Let’s start with the Main-menu to list down three option were the user will select one among them, the menu options are: 1. Merge Sort Algorithm – Fast Run. & 2. Merge Sort Algorithm – Step By Step and the last one is 3. Exit. Here is the code.
|
I will Not post the code for collecting the Array Element from the user [Find it in the .py file for this project]. Here is the code for merge_sort_divider Function, it takes two arguments the Array and the user_selection, so if the user selection is 2 (2. Merge Sort Algorithm – Step By Step) We will print-out the Dividing Steps and some other Text. [We are using If Statement to print-out Text lines]. Here is the Code ..
|
Now for merging function, for merging function we will have two copies, one as Fast-Run and another for Details to print-out the running Steps, this will make the Fast-Run Function short and easy to follow-up. So, here is the Fast-Run Function code for merge the two part of array returned from the merge_sort_divider. Here is the Code ..
|
Run time screen ..
|
End of Merge Sorting Algorithm.
To Download my Python code (.py) files Click-Here
By: Ali Radwani
Python: Sorting Algoritm – Bubble Sort
Learning : Python, Math, Algorithm
Subject: Sorting Algoritm – Bubble Sort
[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]
Sorting Algorithm is a way to sort a given list of numbers, there are several sorting Algorithm as follow:
Type of Sorting Algorithm
Quick Sort. [Click to Read]
Bubble Sort.
Merge Sort. [Commeing Soon]
Insertion Sort. [Commeing Soon]
Selection Sort. [Commeing Soon]
Heap Sort. [Commeing Soon]
Radix Sort. [Commeing Soon]
Bucket Sort. [Commeing Soon]
Here in this post we will write a function to take a given list and sort it then pass it back. We assume the user will enter a serial of numbers, that he want to sort, our function will sort it and print out the original numbers and the sorted one.
Bubble Sort: Steps of Bubble Sorting Algorithm are:
1. We will select the First Number (Current number) in the list and Compare it with the next number in the list. (Compare list[0] and list[1])
2. If the Next Number in the list IS SMALLER than Current Number SWAP the Two, then compare the Current with the third number and so on.
2.2 If the Next Number in the list IS NOT SMALLER than Current Number, Then we LEFT the Current Number in it’s Position and will Set the Next number as the Current. And will start Comparing Again as Step 2 until the last number in the list.
3. When we reach the end of the list and we perform a SWAP, then we start again with First Element as STEP 1.
4. If we reach the end of the list Without any SWAP being made, then the list is ordered and the algorithm can stop.
Coding Starting with the Main Menu Function and a Function to ask the
User to enter the numbers in the list, here are those two Functions.
|
And here is the Main code that will call the Menu and trigger the Functions based on the User selecting.
|
Also we have the Function that will ask the user to Enter the Numbers for the List or Array to be sort.All the code will be Available as a .py file in the Download Page [Click Here]
Now let’s see the code for Bubble Sorting Algorithm, we will have two version of the code, one for Fast-Run that takes the list and return it back as Sorted, and another one will be to show the Details Step By Step of applying the Algorithm. Both Functions are the same but the details one will have several print statements to show the steps, here I am posting the Fast-Run version.
|
Run Screen..
|
End of Bubble Sort Algorithm, hope you enjoy it.
.. Have Fun With Coding ..
To Download my Python code (.py) files Click-Here
By: Ali Radwani
Another sketch challenge: The House
This week sketch challenge @1hour1sketch on Twitter is to Draw a House so here is my sketch using pencil then black Pen, it takes around 25min. More Sketches on my Sketch page ..
you may Follow me on Twitter @h_ta3kees
Here is my sketch..

Another sketch challenge: Eagle Face
This week sketch challenge @1hour1sketch on Twitter is to Draw an Eagle Face, so here is my sketch using pencil then black Pen, it takes around 15min. More Sketches on my Sketch page ..
you may Follow me on Twitter @h_ta3kees
Here is my sketch..

Python Project: Disarium Numbers in Range
Learning : python code
Subject: Finding the Disarium number
in a range
[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]
Definition We call a Number as A Disarium Number if the Sum of it’s Ddigits Powered with their Respective Position is Equal to the Original Number.
So 89 is a Disarium Number, because 8^1 + 9^2 = 8 + 81 = (89 the original number)
and 135 is also a Disarium Number, because 1^1 = 1, 3^2 = 9, 5^3 = 125 and the Total is [1+3+125 = 135 the original number]
we have a previous post to check if a given number is a Disarium Number or not … Read The Post Here .. In this post we will write the Function to print-out all the Disarium Numbers in a given range.
So our application will ask the user to enter two Numbers as a range From and To, then the disarium_n_range(num1,num2) taking two argument will work through a For loop to check each number in the range and if it is a Disarium Number we will store it in a list (disarium_n =[]) .. Let’s start by the asking the user to Input the range numbers..
# Code to collect the Numbers from the User
print('\n\n This Project will Print-Out all the Disarium Numbers in a given range.')
num1 = input('\n Enter the From Number. > ')
num2 = input('\n Enter the To Number. > ')
And now let’s see the main Function of the application ..
![]() |
Run-Screen for the range From 10 To 99![]() |
Another Run for the Range From 100 To 999
|
End of the post ..
To Download my Python code (.py) files Click-Here
By: Ali Radwani
Python: Sorting Algorithm (1.Quick Sort)
Learning : SQL, Python, sqlite, DataBase
Subject: Testing the SQL Join commands using Python.
Sorting Algorithm is a way to sort a given list of numbers, there are several sorting Algorithm as follow:
Type of Sorting Algorithm
Quick Sort.
Bubble Sort.
Merge Sort.
Insertion Sort.
Selection Sort.
Heap Sort.
Radix Sort.
Bucket Sort.
Here in this post we will write a function to take a given list and sort it then pass it back. We assume the user will enter a serial of numbers, that he want to sort, our function will sort it and print out the original numbers and the sorted one.
Quick Sort Steps of Quick Sorting Algorithm are:
1 – Save the first element of the list as pivot. We will call it as pv .
2 – Define Two variables i and j. We will call them as fc, lc fc will be 0 (first element position in the list) and lc will be the length of the list.(last element position in the list) .
3 – Increment fc until the number in the list in fc position is smaller or equal to pv (the first element).
4 – Decrement lc until the number in the list in lc position smaller than pv.
until list[j] < pivot then stop.
5 – If fc less than lc then we swap the two elements in the location of fc and lc. (SWAP list[fc] and list[lc]).
7 – Exchange the pivot element with list[j] element.
Coding First we will write a sort Menu for the project, we will have tree items to select from, Quick Sort Algorithm – Fast Run and Quick Sort Algorithm – Step By Step This will show sorting details.
# Main Menu
def main_menu ():
os.system('clear')
print('\n\n',' '*5,'******************************')
print(' '*5,' ***',' Sorting Algorithm ',' '*1,'***')
print(' '*5,' ***',' Quick Sort ',' '*1,'***')
print(' '*5,' ***',' '*22,'***')
print(' '*5,' ******************************\n\n')
print(' '*7,'1. Quick Sort Algorithm - Fast Run.')
print(' '*7,'2. Quick Sort Algorithm - Step By Step.')
print(' '*7,'9. Exit.')
user_choice = input('\n Select your choice. > ')
return user_choice
And this is the main code body that will call the menu and check the user selection ..
# The Main application Body
while True:
user_select = main_menu()
if user_select == '1' :
user_list = create_list()
fpos = 0 # first position index
lpos = len(user_list)-1 # last position index
original_list = user_list
print('\n The original List is: ',original_list)
user_sorted_list = quick_sort(user_list,fpos,lpos)
print('\n DONE .. We Finish Sorting .. ')
print(' The Sorted List is: > ',user_sorted_list)
input('\n ...Press any key to continue. ')
if user_select == '2' :
user_list = create_list()
print('\n We will show the Quick Sorting Step By Step... \n')
fpos = 0 # first position index
lpos = len(user_list)-1 # last position index
original_list = user_list
print('\n The Original List is: ',original_list)
user_list = quick_sort_details(user_list,fpos,lpos)
print('\n DONE .. We Finish Sorting .. ')
print(' The Sorted List is: > ',user_list)
input('\n ...Press any key to continue. ')
if user_select == '9' :
break
Also we will have a Function to take the List of Elements from the user, the user input will be as a string, we will convert it to an integer List and will return it back.. Here is the code ..
# Create the List
def create_list():
print('\n Enter the List Elements separated by SPACE, when Finish just Press Enter.')
the_list = input('\n Start Entering the Numbers in the List. > ')
# Convert user input to List
the_list = the_list.split()
# Convert str list to int list
the_list = [int(each) for each in the_list]
return the_list
Now let’s write the Quick_sort function, then we will duplicaet it and add some print statements to show sorting steps. So first the code for Quick Sort Algorithm – Fast Run. Here is the code ..
Screen shot of the Quick Sort Algorithm – Fast Run.![]() |
Running this Function will return the sorted list and display it on the screen, I thought it will be nice if we show the sorting process Step by Step, so I copy the same Function with adding some print-statement in-between .. here is the code and the run-output..
Screen shot of the Quick Sort Algorithm – Detail Run.
|
![]() |
End of Sorting Algorithm (1.Quick Sort)
To Download my Python code (.py) files Click-Here
By: Ali Radwani
Flowers in my lens … 8
Here is another photo taken with a #galaxy #Note9 phone for some flowers from my garden..
For mor follow me on Twitter @h_ta3kees

Flowers in my lens … 5
Here is another photo sample taken with #galaxy #Note9 phone for some flowers in my garden..
For mor follow me on Twitter @h_ta3kees




Follow me on Twitter..



















