Algorithm
An algorithm is a list of steps that can be followed to solve a problem using a limited number of steps. For example, a recipe is an algorithm to prepare a dish in a limited amount of time. The words 'algorithm' and 'algorism' come from the name of a Persian mathematician called Al-Khwārizmī (Persian: خوارزمی, c. 780–850). In computing, an algorithm is a precise list of operations that could be done by a Turing machine. For the purpose of computing, algorithms are written in pseudocode, flow charts, or programming languages. Comparing algorithmsThere is usually more than one way to solve a problem. There may be many different recipes to make a certain dish which looks different but ends up tasting the same when all is said and done. The same is true for algorithms. However, some of these ways will be better than others. If a recipe needs lots of complicated ingredients that you do not have, it is not as a good as a simple recipe. When we look at algorithms as a way of solving problems, often we want to know how long it would take a computer to solve the problem using a particular algorithm. When we write algorithms, we like our algorithm to take the least amount of time so that we can solve our problem as quickly as possible. In cooking, some recipes are more difficult to do than others, because they take more time to finish or have more things to keep track of. It is the same for algorithms, and algorithms are better when they are easier for the computer to do. The thing that measures the difficulty of an algorithm is called complexity. When we ask how complex an algorithm is, often we want to know how long it will take a computer to solve the problem we want it to solve. SortingThis is an example of an algorithm for sorting cards with colors on them into piles of the same color:
Sorting by numbersThese are examples of algorithms for sorting a stack of cards with many different numbers, so that the numbers are in order. Players start with a stack of cards that have not been sorted. First algorithmThis algorithm goes through the stack of cards, one card at a time. This card is compared to the next card in the stack. Please note that this position only changes in step 6. This algorithm is called bubble sort. It is slow.
Step-by-step example![]() Let us take a stack of the cards with the numbers "5 1 4 2 8", and sort it from smallest number to biggest one using this algorithm. In each step, the algorithm compares the elements written in bold. The top of the stack of cards is on the left-hand side. First pass: HistoryThis is an easy-to-understand algorithm for sorting. Computer scientists called it Bubble sort, because smaller elements will rise to the top, changing their position in each run. Unfortunately, the algorithm is not very good, because it needs a long time (many passes through the stack of cards) to sort it. Second algorithm![]() This algorithm uses another idea. Sometimes solving a problem is difficult, but the problem can be changed so it is made of simpler problems that are easier to solve. This is called recursion. It is more difficult to understand than the first example, but it will give a better algorithm. Basic idea
Merging two stacks togetherThis works with two stacks of cards. One of them is called A, the other is called B. There is a third stack that is empty at the start, called C. At the end, it will contain the result.
HistoryJohn von Neumann developed this algorithm in 1945. He did not call it Sorting by numbers, he called it Mergesort. It is a very good algorithm for sorting, compared to others. Third algorithm![]() The first algorithm takes much longer to sort the cards than the second, but it can be improved (made better). Looking at bubble sort, it can be noticed that cards with high numbers move from the top of the stack quite quickly, but cards with low numbers at the bottom of the stack take a long time to rise (move to the top). To improve the first algorithm here is the idea:
HistoryThis algorithm was developed by C. A. R. Hoare in 1960. It is one of most widely used algorithms for sorting today. It is called Quicksort. Putting algorithms togetherIf players have cards with colors and numbers on them, they can sort them by color and number if they do the "sorting by colors" algorithm, then do the "sorting by numbers" algorithm to each colored stack, then put the stacks together. The sorting-by-numbers algorithms are more difficult to do than the sorting-by-colors algorithm, because they may have to do the steps again many times. One would say that sorting by numbers is more complex. Related pages
Other websites![]() Wikimedia Commons has media related to Algorithms.
|
Portal di Ensiklopedia Dunia