This article is an original article by Hu Bingliang.

Algorithm of a.

In the Art of Computer Programming, Gartner summarized algorithms as follows:

  1. Input: An algorithm must have zero or more inputs
  2. Output: An algorithm should have one or more outputs
  3. Clarity: The description of the algorithm must be unambiguous, and the actual results of the operation must be certain
  4. Finiteness: Must end in a finite number of steps
  5. Validity: also known as feasibility, can be implemented by the implementation if you want to study the algorithm in detail recommend “Data Structure and Algorithm Analysis”

2. Definition problem

Demand:

Array Array contains N positive integers. The input quantity is array. The output quantity is a sorted array

The code example

Var array =,2,4,6,8 [5]function sort(){your code} sort(array) // [2,4,5,6,8]Copy the code

What do you do when you run into a mental block?

  • Turn abstract problems into concrete ones
  • Turn unseen problems into seen problems

Sorting algorithm

A demo of all algorithms can be viewed here

1. BUBBLE sort

Repeatedly compare the sequence to be sorted, comparing two elements at a time and swapping them if they are in the wrong order. The task of comparing a sequence is repeated until no more exchanges are needed, that is, the sequence has been sorted. For each full round of comparisons, the largest one appears at the end of the name – bubble sort

The process is as follows:

  1. We get an array

  2. I start comparing the first two, and I find 44 is greater than 3, so I don’t swap

  3. And then we go back and we find 38 is less than 44, so we switch them

  4. And so on until the end of the first round, we get the biggest one —-50(the first bubble)

    End of the first round

  5. Then in the next round, we repeat the first round, and we get the second largest ——48

    End of the second round

  6. We do this multiple times and we get an array from small to large

2. SELECT sort

Each time the smallest (or largest) element is selected from the data elements to be sorted and stored at the beginning of the sequence until all the data elements to be sorted are sorted. The process is as follows:

  1. Get an array
  2. We’re going to pick the smallest element in the array and swap it with the first one, so we’re going to take 3 as the smallest, and then compare it to the next one.
  3. When we get to 2, we find that 3 is greater than 2, so we assume that 2 is the minimum, and we should compare everything to 2.
  4. When you compare all the elements and determine that 2 is the minimum value, switch the minimum value, which is 2, with the position of the first element.
  5. Then start a new round of comparison from the second element, the same process as the first round. Let’s take 44 as the minimum and compare it to everything else.
  6. After several rounds of comparison, we get an array from small to large.

3. INSERT sort

The algorithm is suitable for sorting a small amount of data by inserting a data into the ordered data that has already been ordered, thus obtaining a new, number plus one ordered data. It’s a stable sorting method. The process is as follows:

  1. Get an array

  2. Treat the first element as a new array, and then compare the second element in turn to the elements of the new array (although there is only one…). And then insert it into the appropriate position.

    Compare with the elements of the new array

    Insert into the appropriate position

  3. Then, by analogy, the first two elements are treated as a new array, and the third element is compared to the new array in turn and inserted into the appropriate position.

    To compare

    Insert in the appropriate position

  4. Insert the remaining elements in order, resulting in the smallest to largest array.

4. MERGE sort

The ordered subsequences are combined to obtain a fully ordered sequence. That is, each subsequence is ordered first, and then the subsequence segments are ordered. The process is as follows:

  1. Get an array
  2. We divide the array equally into the left and right, we get two new arrays, and then we divide each array equally into two parts, until we get only two elements in each array, and then we compare the first group
  3. So 3 is less than 44 so it stays the same and then the second group, 38 is more than 5 so we switch places.
  4. So the point is, at this point we’re not going to compare the third group but we’re going to put the first and second groups together.
  5. Then you compare the third group and the fourth group, and you put them together again.
  6. Then, instead of comparing the fifth and sixth groups, the new arrays generated by the first and second groups are sorted together with the new arrays generated by the third and fourth groups.
  7. Do the same for the rest of the process. We have two sorted arrays. And we’re done sorting these two arrays.

    After the order:

5. Quicksort

Each element finds its position (the first one is smaller than me, the second one is larger than me) as follows:

  1. Get an array
  2. Compare the first element with the following element, find all the elements that are smaller than the first element, put them to the right of the first element and swap the first element with the last of these elements that are smaller.

    Only 2 is less than 3

    swap

  3. The first two elements are already in the right place, and the third element is used as the standard to compare with the following elements.
  4. Put the element smaller than it to its right (green) and make it swap places with the last one green.
  5. Then start with the element (not orange) on the left that has no definite position —-, which is 19
  6. Until all the elements are in the right place.

6. Random quicksort

As the name suggests, it is based on quicksort, adding randomness mechanism. In quicksort we pick from left to right, in random quicksort we pick at random. The process is as follows:

  1. Get an array
  2. You pick an element at random, you pick 44 at random.
  3. And he put the one younger than he on his right
  4. And then swap it with the smaller, right-most element
  5. Then select a random element and repeat the process until all elements are in the correct position

    Add wechat signal: Xiedaimala03 code: write code

A daily question, weekly resource recommendation, wonderful blog recommendation, work, written test, interview experience exchange answers, free live class, group friends share light… Countless benefits are given away for free