How does someone approach an algorithm?
Index = 1
Found = FALSE
WHILE Index <= Length of Array DO
IF Array[Index] = Query THEN
PRINT "Found!"
Found = TRUE
ENDIF
ENDWHILE
IF Found == FALSE THEN
PRINT "Element is not in array"
ENDIF
In sam davis textbox, is "Index" like "Count" and "Query" is "ItemToFind"? I understand most algorithms logic, but writing one from scratch is very hard for meUnderstanding an algorithm? It's pretty much a description of a process, like a recipe. Try to translate it into English in your head. E.g. this is a linear search algorithm
So you start at the beginning of the array. You then go through each element of the array, checking if it matches the element you're querying for. If you find the element, output that you found it. If by the end of the program, you haven't found your element, then output that it's not in the array.Code:Index = 1 Found = FALSE WHILE Index <= Length of Array DO IF Array[Index] = Query THEN PRINT "Found!" Found = TRUE ENDIF ENDWHILE IF Found == FALSE THEN PRINT "Element is not in array" ENDIF
Pretty much do this for any algorithm you're given.
"The pseudocode syntax? Or how to translate an abstract solution into an algorithm?" and interpretation of the question is the hardest part for me and of course writing one from scratch (insertion,bubble,selection) just makes me have a headache just thinking about it.The specific names used are interchangeable, so yes, "Index" is like "Count" and "Query" is like "ItemToFind".
What do you have trouble with? The pseudocode syntax? Or how to translate an abstract solution into an algorithm? To write one from scratch, think about how you'd carry it out by hand. E.g. for a linear search, think about having a bunch of items in front of you, and going through them one by one. That's what I do.
Post up some questions and i will try to answer them to best of my ability.Do you have any examples of questions you can't do? It'll be easier to help you on a concrete case.
For all the standard algorithms like the sorts, just think about what they do.
* Insertion sort goes through each element sequentially and shifts it backwards until it's in its right position.
* Bubble sort repeatedly goes through the whole array and swaps out-of-order adjacent terms. It keeps doing this until the list is sorted.
* Selection sort goes through each spot in the sorted list and linear searches for the element that should go in that spot. It then puts the element in that spot.
INPUT Array // 0-indexed
IsSorted := FALSE
WHILE IsSorted = FALSE DO
i := 0
IsSorted = TRUE
WHILE i < length of Array DO
IF Array[i] < Array[i+1] THEN
Temp := Array[i]
Array[i] := Array[i+1]
Array[i+1] := Temp
IsSorted := FALSE
ENDIF
i := i + 1
ENDWHILE
ENDWHILE