 An “algorithm” is a procedure or method for solving a problem. Learning programming requires learning algorithms along with the grammar of the programming language . This time, we will introduce the types and roles of algorithms and the meaning of learning algorithms.

## Sort algorithm

In programming, including databases, there are many cases where a large amount of data is handled. At that time, the algorithm used for sorting the data stored in the storage area according to a certain rule, such as ascending order (arranging the values ​​in ascending order) and descending order (arranging the values ​​in descending order), is the “sort algorithm” . ..

There are various types of sorting algorithms, but there are three typical algorithms: “bubble sort”, “quick sort”, and “merge sort”. .

### Bubble sort Bubble sort is a method of sorting by repeating comparison and replacement of adjacent elements from the end of the data .

[Example] Sort {10, 7, 6, 8, 5, 4} in ascending order

{10, 7, 6, 8, 5, 4}
→ {10, 7, 6, 8, 4, 5} (exchange 5 and 4)
→ {10, 7, 6, 4, 8, 5} (8) And 4 are exchanged)
→ {10, 7, 4, 6, 8, 5} (6 and 4 are exchanged)
→ {10, 4, 7, 6, 8, 5} (7 and 4 are exchanged)
→ {4 , 10, 7, 6, 8, 5} (exchange 10 and 4, the first element is confirmed)
→ {4, 10, 7, 6, 5, 8} (exchange 8 and 5)
→ {4, 10, 7, 5, 6, 8} (exchange 6 and 5)
→ {4, 10, 5, 7, 6, 8} (exchange 7 and 5)
→ {4, 5, 10, 7, 6, 8} (Exchange 10 and 5, the second element from the beginning is confirmed)
→ {4, 5, 10, 6, 7, 8} (Exchange 7 and 6)
→ {4, 5, 6, 10, 7, 8 } (Exchange 10 and 6, the third element from the beginning is confirmed)
→ {4, 5, 6, 7, 10, 8} (Exchange 10 and 7, the fourth element from the beginning is confirmed)
→ {4 , 5, 6, 7, 8, 10} (10 and 8 are exchanged, the 5th and 6th elements from the beginning are confirmed)

### Quicksort

Quicksort is an algorithm that achieves fast sorting. In quicksort, a reference value (pivot) is determined, and the process of dividing the data group into two groups, one above the reference value and one below the reference value , is repeated to replace the elements.

There are various ways to take the reference value, but this time we will take the larger value as the reference value out of the two different values ​​at the beginning.

[Example] Sort {4, 8, 7, 3} in ascending order

1. Looking from the beginning, there are two different values ​​4 and 8, so take 8 as the reference value. Searching for values ​​greater than or equal to 8 from the beginning will find 8 (second element from the beginning), and searching for values ​​less than 8 from the end will find 3 (elements at the end). So, replace 8 and 3 to make {4, 3, 7, 8}.

2. Continue exploring. Searching from the left (third and subsequent elements from the beginning) finds 8, and searching from the right (second and subsequent elements from the end) finds 7, but the search positions intersect. Therefore, divide the sequence between 7 and 8 where the search positions intersect to make {4, 3, 7} {8}.

By doing this, one sequence could be divided into a group {4, 3, 7} below the reference value and a group {8} above the reference value.

3. For {4, 3, 7}, set the reference value to 4. If you exchange the 4 found by searching from the beginning and the 3 found by searching from the end, you get {3, 4, 7}. If the search is continued, the search position intersects between 3 and 4, so it is divided into {3} {4, 7}.

4. For {4, 7}, set the reference value to 7. Divide it between 4 and 7 where the search positions intersect to make {4} {7}.

The above contents can be summarized as follows.

{4, 8, 7, 3}
→ {4, 3, 7, 8}
→ {4, 3, 7} {8}
→ {3, 4, 7} {8}
→ {3} {4, 7} {8}
→ {3} {4} {7} {8}

### Merge sort

Merge sort repeats the division of data and merges (merges) the disjointed elements, paying attention to the order . It is attractive that the processing time is not greatly affected by the arrangement of data.

[Example] Sort {8, 4, 5, 7, 2, 1, 3, 6} in ascending order

{8, 4, 5, 7, 2, 1, 3, 6}
→ {8, 4, 5, 7} {2, 1, 3, 6}
→ {8, 4} {5, 7} {2, 1} {3, 6}
→ {8} {4} {5} {7} {2} {1} {3} {6}
→ {4, 8} {5, 7} {1, 2} {3 , 6}
→ {4, 5, 7, 8} {1, 2, 3, 6}
→ {1, 2, 3, 4, 5, 6, 7, 8}

### Search algorithm

“search algorithm” is indispensable for efficiently finding the target element that meets the conditions from a large amount of data . The main search algorithms are “linear search” and “binary search” .

Linear search is an algorithm that searches from the first data against the conditions . It’s a simple method, but the downside is that if the element you’re looking for is the last element in a set of data, you’ll have to look at all the elements.

On the other hand, in the binary search, the sorted data group is divided into two groups, and the search range is narrowed by repeatedly determining which group the element is looking for.

Binary search is useful for searching sorted data. If you want to apply a binary search to unaligned data, use a sort algorithm to align and then use the binary search.

### The importance of algorithms and how to study

In programming, there are many situations where a large amount of data is handled. In order to utilize a large amount of data efficiently, it is essential not only to understand the algorithm as knowledge but also to try coding it.

After understanding the principle of the algorithm, read the code of the sample program line by line to see what kind of processing is being performed.

Why are algorithms important? This is because the amount of calculation changes depending on how the algorithm is selected. When sorting a large amount of data, if you know the sorting algorithm, you will come up with the idea that quicksort can sort the data quickly.

However, if the data is ordered to some extent, it will not be very effective, so you can choose another algorithm.

Also note that quicksort is not a stable sort. Stable sorting means that when you sort a group of data with equivalent data, the order before sorting is maintained.

For example, consider sorting grade data, which is a pair of student numbers and test scores, in order of score. With a stable sort, students with the same score will be sorted by student number. However, in the case of an unstable sort, students with the same score may not be sorted by student number.

For those who are new to programming, algorithms can be tricky, but once you master them, you’ll be able to master them. After writing a program and confirming that it works, let’s think about whether we can write a more efficient program.

### Final Thoughts

In the IT/Web industry, where technological innovation is fierce, new technologies are born one after another. This is no exception for programming languages. Recently, with the rise of artificial intelligence and data analysis, languages ​​such as Python and R have begun to attract attention.

However, no matter what language you decide to use in the future, you should be able to make a smooth transition if you thoroughly learn the algorithms that underlie programming.

If you want to learn programming from the basics including algorithms, we recommend you to go to a specialized school of IT and Web. In the programming course of the Internet Academy , the curriculum is designed so that even beginners of programming can program while thinking about the design after completion.

Hope! Now you have a better understanding of programmatic algorithm don’t forget to share with your classmates and friends. Thank you & take care.

### You may also like

What is an Abstract Data Type (ADT)?

Types of software testing: The Acceptance Test

What are the qualities of a good website design?

How many types of web design are there?

##### Social media 