Do you creates algorithm?

Thread Starter

Pushkar1

Joined Apr 5, 2021
40
I see many people write algorithms to solve complex problems. Do you believe that the algorithm should be developed before writing the code.

Did you @MrChips also write algorithms before to start programming for embedded systems?

I would like to know some tips, advice if you write algorithms.

How do you create Algorithm? and what should we keep in mind while writing algorithms?
 

MrChips

Joined Oct 2, 2009
23,528
I see many people write algorithms to solve complex problems. Do you believe that the algorithm should be developed before writing the code.

Did you @MrChips also write algorithms before to start programming for embedded systems?

I would like to know some tips, advice if you write algorithms.
Yes. If it is a complex algorithm then it is important to write it out first before writing code.
When you get good at it you can do it in your head for simple problems.
 

MrChips

Joined Oct 2, 2009
23,528
Here are two exercise problems:

1) Devise an algorithm to sort a list of names in alphabetical order.

2) Devise an algorithm in order to convert an analog voltage to a binary value using the successive approximation algorithm (ADC = analog-to-digital conversion).
 

dl324

Joined Mar 30, 2015
12,871
Do you believe that the algorithm should be developed before writing the code.
If they're simple enough, you can do them in your head.

I find it easier to check all boundary conditions if I create a flow chart. I have chronic pain that prevents me from being able to sit for more than an hour or so, so a flow chart relieves me of the burden of keeping track of all of the conditions in my head. When I could code for 8-12 hours at a sitting, I didn't use flow charts as much.
 

MrChips

Joined Oct 2, 2009
23,528
I suffered from severe upper back pain (pinched nerves between C5 and C6) which prevented me from doing more than 15 minutes of electronics assembly.

I went for physiotherapy and was instructed to change my posture, seating ergonomics at the computer, and do regular daily exercises. I also had to change my desk chairs at home and at work. I no longer have back pain but I am constantly diligent about my posture now when walking, sitting, and keyboarding.

I think we are well aware of the dangers of lifting heavy objects the wrong way. Bad keyboarding posture at the computer is one of the leading causes of back pain. When you are young you can abuse your body without knowing it. Eventually it catches up with you and you pay for it in suffering pain.
 

dl324

Joined Mar 30, 2015
12,871
I suffered from severe upper back pain (pinched nerves between C5 and C6) which prevented me from doing more than 15 minutes of electronics assembly.
I was in a motor vehicle accident and suffered injuries to L4 and L5. I had back surgery that was intended to alleviate sciatic pain shooting down my legs, but there was nothing they could do for the back pain because I wouldn't let them fuse vertebrae because out patient back surgery sent me to the emergency room for longer than the surgery took and I wouldn't risk another episode.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
40
Here are two exercise problems:

1) Devise an algorithm to sort a list of names in alphabetical order.
Sorry @MrChips , I can't write algorithms. Because i don't have any idea. I wanted to learn I was expecting someone to show the way how they make algorithms

When I think to sort a list of names in alphabetical order. I have couple of questions in my head.

1. A list can have many names 10, 100, 1000, more than?

2. Not everyone's name is the same size. there may 10 alphabet, 20 alphabet anything may be possible?

3. There can be many names in the list whose initial alphabet may be the same?

4. What happen if initial two, three or four
alphabet are same in list?

5. What happens when the name of the same person repeated two times, four times in list?

I do not know what's your plan, how these two examples can help to learn the algorithm. I did't understand what algorithm needs to sort a list of names in alphabetical order.
 

MrChips

Joined Oct 2, 2009
23,528
All of the above (1 to 5) apply.

That is the idea... to encourage you to think for yourself.
Start off simple. Try a simple list.

Sort the following list. How would you do this manually?

MARY
JOHN
PARM
ADAM
ISLA
 

BobaMosfet

Joined Jul 1, 2009
1,776
I see many people write algorithms to solve complex problems. Do you believe that the algorithm should be developed before writing the code.

Did you @MrChips also write algorithms before to start programming for embedded systems?

I would like to know some tips, advice if you write algorithms.

How do you create Algorithm? and what should we keep in mind while writing algorithms?
Yes- That's how you design. You identify a problem, you figure a solution, and then you code it and test it. Learn how to flow-chart so that you can create an algorith before you code it.
 

dl324

Joined Mar 30, 2015
12,871
I did't understand what algorithm needs to sort a list of names in alphabetical order.
It depends on how the names are going to be used and if there is other data associated with the names.

If I were going to do it, assuming each name had more data associated with it, I'd create a array of structure pointers that uses the hashed names as an index. If multiple names hashed to the same value, I'd create a linked list of elements at that value. Creating an efficient hashing algorithm would reduce the chances that two names would hash to the same value.

You can look up algorithms and methods for doing each of those things. Knuth was a giant in the area of algorithm development and I studied a number of his.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
40
All of the above (1 to 5) apply.

That is the idea... to encourage you to think for yourself.
Start off simple. Try a simple list.

Sort the following list. How would you do this manually?

MARY
JOHN
PARM
ADAM
ISLA
Thank you @MrChips

The English Alphabet consists of 26 letters: A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z.

Start from alphabet "A" Look the first alphabet of first Name "M"ARY. If the first alphabet match with alphabet " A". It will be in top of the list

In this case "A" doesn't match with "M"

Go to next Name "J"OHN
"A" doesn't match with "J"

Go to next Name "P"ARM
"A" doesn't match with "P"

Go to next Name "A"DAM
"A" Both match "A"
Add ADAM at the first position in list.

Go to next Name "I"SLA
"A" doesn't match with "I"

First pass complete

"B" doesn't match with "M"
Go to next Name "J"OHN
" B" doesn't match with "J"
Go to next Name "P"ARM
"B" doesn't match with "P"
Go to next Name "A"DAM
"B" doesn't match "A"
Go to next Name "I"SLA
"B" doesn't match with "I"

... Repeat all Till the last alphabet Z

There is a loop that run 26 times.

You can see a general idea. Every time I'm comparing each english alphabet with an first alphabet of each Name. If this match add into list if not check next alphabet

Does it make sense for you?
 

dl324

Joined Mar 30, 2015
12,871
Start from alphabet "A" Look the first alphabet of first Name "M"ARY. If the first alphabet match with alphabet " A". It will be in top of the list
It would be helpful if you specified the language you intend to use. Some have built-in capability for sorting lists of strings.
 

MrChips

Joined Oct 2, 2009
23,528
Now ask this question. Will your algorithm work for all names (given that we will restrict all names to be exactly four letters for now)?

Suppose you had

MARY
MARK

will your algorithm work?

Another question: Why loop 26 times when there are only 5 names in the list?

btw: This is how you develop computer algorithms. You create the algorithm on paper. Then you test it manually on paper. If it does not work you start over again. No coding necessary.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
40
It would be helpful if you specified the language you intend to use. Some have built-in capability for sorting lists of strings.
The original question wasn't to sort a list of names in alphabetical order. It was to learn to develop algorithm. I was answering Mrchip's questions. I will use c language to write program

Suppose you had
MARY
MARK
Now need to check first and second alphabet of name

Another question: Why loop 26 times when there are only 5 names in the list?
Loop will run 16 times because name start from A to P alphabet
 

dl324

Joined Mar 30, 2015
12,871
Here's an example in Perl (which was designed for text processing):
Code:
#!/usr/bin/perl

@list = qw(Mary Joe April Xavier Mark Joseph Maryann);
@sorted = sort(@list);

print("Original: " . join(" ", @list) . "\n");
print("Sorted:   " . join(" ", @sorted) . "\n");

exit(0);
The output:
Code:
Original: Mary Joe April Xavier Mark Joseph Maryann
Sorted:   April Joe Joseph Mark Mary Maryann Xavier
 

MrChips

Joined Oct 2, 2009
23,528
The original question wasn't to sort a list of names in alphabetical order. It was to learn to develop algorithm. I was answering Mrchip's questions. I will use c language to write program



Now need to check first and second alphabet of name


Loop will run 16 times because name start from A to P alphabet
No. You do not go through the letters of the alphabet. Besides, you do not know that the names start with A to P. They could start with A to Z.

You begin by taking two entries.
MARY
JOHN

You decide which one comes first. (That in itself requires a separate algorithm.)
What you do next depends on the algorithm you choose. For example, you can swap the order of the names if the second one comes first. Or you can choose to put one of the entries into another list. There are many options available. Your task is to think of as many different algorithms as possible that you can find. There are often many different algorithms for the same problem.
 
You can use <>= for strings. The most important thing you can do is to use pointers, so you don;t switch the list, but just the pointers to the list.

There are all sorts of sorting routines. Look up bubble or double bubble sort for example.

Once you have a sorted list, there is a method called binary searching. If the list is EXTREMELY long, you can incorporate hashing.

I coded an application in high school that would "pretty print" files written in BASIC. What I finally came up with was fast and efficient. It relied on two statements: Change a$ to a% and change a% to a$. They took a string and converted it to an array and back again. $ is a string and % was an integer.

We had sources to the BASIC compiler, so I had some hints. I also took a course in compiler writing. so the statement
Open A$ for input as file #1. "forinputasfile" is a single token. For I=1to11. For is a token and to is a token.
Part of the "pretty print" routine would remove all spaces unless they were within quotes and replace the token "forinputas file" with "for input as file". It was an algorithm.

A lot of times you can get buy with writing Pseudo-code.

Trouble-shooting can always be fun. The DEC service technicians could not figure out why our PDP 11/50 kept crashing until we found out that it was dropping a bit in the processor status word when the machine switched modes.
 

click_here

Joined Sep 22, 2020
190
In the C language there is a function called "qsort" in stdlib.h

You define the function that determines if input A is larger than/equal/less than B

The best way of storing these strings will probably be a linked list

If you want to get your own sorting algorithm, you'll need to replace the qsort with something that you write, such as a bubble sort

Another way of approaching this is to save the strings in a list that inserts them alphabetically as you add them, but I'm guessing that you are more after the learning experience!
 

MrChips

Joined Oct 2, 2009
23,528
I believe the focus of this thread is on algorithm design, i.e. how to design an algorithm, not what algorithm to use or what library functions are available.

I chose the sorting problem as an example because it is a classic exercise in algorithmic design. The challenge here is in analytical thinking, how to select and construct an algorithm and how to test its validity for special cases first and then for all cases.
 
Top