Docsity

Algorithms (basic description), Study notes of Computer science

Partial preview of the text

Download Algorithms (basic description) and more Study notes Computer science in PDF only on Docsity!

Algorithms What is an Algorithm? An algorithm is a step-by-step set of instructions designed to solve a problem or perform a specific task. It provides a clear, logical, and unambiguous path for achieving the desired outcome. For example, there are Algorithms worked out for sorting a list efficiently. Thee are Algorithms to create random numbers,how to shade a pixel and how to calculate something. What is a problem? A problem is an issue or challenge that requires a solution or resolution. A simple problem is straightforward with clear steps to find the solution. It often has a single, well-defined solution and doesn't require much time or resources. A complex problem involves multiple variables, may have no clear solution, and requires deeper analysis, creativity, and more time or resources to solve. It may have several possible solutions or require dealing with uncertainty. Algorithms are important because they provide a systematic way to solve problems efficiently. They help in optimizing processes, saving time, reducing errors, and improving performance. Algorithms are the foundation of all software applications, enabling tasks like data processing, decision-making, and automation. Steps to write an algorithm Understand the problem clearly. Define the inputs and outputs. Break the problem into small steps. Write the steps in the correct logical order. Use simple and clear language for each step. Check and test the algorithm. Improve it if needed. NQaPWN > An example algorithm Set out an algorithm to count to 10 1.Set a variable to a starting value of 0 (let's call the variable X) 2.Add 1 to the current value of X 3.If 10 steps have taken place then finish A.lf not then repeat step 2 This does the job-it counts to 10 - but this is actually a poor algorithm because it is too specific.What if the programmer wants to add to 11 instead? A good algorithm will be as general as possible to allow for the widest use.For example change the algorithm above to add up to any number 1.Set a variable Y to hold the number of counts needed (say 11). 2.Set a variable X to a starting value of 0. 3.Add 1 to the current value of X. 4.ls X the same as Y yet? 5.If not repeat step three otherwise finish. Just by adding an extra variable to hold the number of additions the algorithm can now deal with any count Characteristics of an efficient algorithm A good algorithm is finite, meaning it will end after a set number of steps. Each step must be clear and unambiguous, ensuring that the algorithm is definite. It should accept zero or more inputs and produce at least one output. All steps should be simple and executable, making the algorithm effective. When choosing between algorithms, you should consider efficiency—an algorithm that performs fewer steps or takes less time. Simplicity is important for easier understanding and implementation, while scalability ensures the algorithm works well with larger inputs. Memory usage is also crucial, as an algorithm with lower memory consumption is preferred. Lastly, readability makes the algorithm easier to maintain and modify. Variables A variable is anamed storage location in a computer's memory that holds a value. This value can be changed or updated during the execution of an algorithm. For Example Think of calculating the area of a rectangle: Let length = 5 (This assigns the value 5 to a variable named "length’) Let width = 10 (This assigns the value 10 to a variable named "width") Let area = length * width (This calculates the area and stores it in a variable named "area" Output area (The computer shows the calculated area, which is 50) In this example, "length," "width," and "area" are all variables. Their values are 1. IF...ELSE: Used to make decisions. The program checks a condition and runs different steps based on whether it is true or false. Example: IF score >= 50 THEN Output "Pass" ELSE Output "Fail" Recall what is a loop A loop is a set of steps that repeat again and again.A finite loop repeats a certain number of times and then stops. It has a clear end.An infinite loop is a loop in programming that never ends. It keeps running because the condition to stop it is never met or is always true.Loops are used to save time and avoid writing the same steps again and again. ENDIF 2. FOR Loop: Used to repeat steps a set number of times. Example: focused more on simplicity and the task itself list = [2, 4, 6, 8, 10] FOR each item IN list DO PRINT item END FOR focused more on control over the process names = ["Alice", "Bob", "Charlie", "David"] FORi = 1 TO LENGTH(names) DO PRINT namesli] END FOR 3. WHILE Loop: Repeats steps while a condition is true. It checks the condition before each repeat. Example: sum =0 i=1 WHILE sum < 100 DO sum = sum +i i=i+1 END WHILE 4. Nesting: Nesting is placing one programming construct (like an IF or a loop) inside another. This allows for more complex control flow in an algorithm. a.Selection Nested selection occurs when one IF statement is placed inside another IF statement. The inner IF is only evaluated if the outer IF's condition is true. This creates a hierarchy of decisions. Example: IF marks >= 50 THEN IF marks >= 80 THEN OUTPUT "Grade A" ELSE OUTPUT "Grade B" ELSE OUTPUT "Fail" b.Iteration Nested iteration is when one loop (like a FOR or WHILE loop) is placed entirely inside another loop. The inner loop completes all its repetitions for each single repetition of the outer loop. This is used for repetitive actions within a larger repetition. Example: FORi=1TO5 OUTPUT i Applications of Algorithms Algorithms are used in many daily activities. For example, in maps and GPS, algorithms help find the best route by checking distance and traffic. In online shopping, they suggest products based on your past searches and purchases, making shopping faster and more personal. Emerging technologies Emerging technologies Cloud Computing involves storing and accessing data and software over the internet instead of on local devices. It allows users to access files and applications from anywhere, reduces the need for expensive hardware, and is used by businesses for storage, software, and services like Google Drive and Dropbox. Emerging technologies like Blockchain provide secure, decentralized data storage and transactions. Autonomous Vehicles use Al and sensors to navigate without human input, transforming transportation. 5G enhances internet speeds, enabling faster data transfer for connected devices. 3D Printing allows for creating physical objects from digital designs, revolutionizing manufacturing and production. Disadvantages of emerging technologies 1. Automation and Al can lead to job displacement by replacing human workers. 2. New technologies, such as loT and cloud computing, can be vulnerable to hacking and data breaches. 3. The development and implementation of emerging technologies can be expensive, making them inaccessible to some businesses or individuals. 4. Emerging technologies like Al and biotechnology raise ethical concerns about privacy, control, and moral implications. 5. Over-reliance on emerging technologies can cause problems if systems fail or malfunction.