Bubble sorting algorithm, selection sorting algorithm, insertion sorting algorithm, binary search algorithm

1. Bubble sorting algorithm:

Compare the two adjacent numbers, if the first one beats the second one, then swap the positions
of the two to do the same for each group of adjacent elements, from the first pair to the last pair at the end, here One point, the last element should be the largest number;
the comparison must be repeated every time, until there is no set of numbers that need to be compared
. The order of the same elements has not changed, so bubble sort is a stable sorting algorithm

Code screenshot:

 

 

 operation result:

 

 

 2. Select the sorting method:

 * Each time the smallest (or largest) element is selected from the data elements to be sorted, the order is placed at the end of the sorted sequence, until all the data elements to be sorted are sorted out. Selection sorting is an unstable sorting method.

3. Direct insertion algorithm

 Direct insertion sorting algorithm (find a suitable position from back to front and insert it)
 * Basic idea: insert a record to be sorted into
 the appropriate position of the subsequence that has been sorted * in front of each record (from back to front After finding a suitable position), until all insertion sorting is completed;

Code screenshot:

 

 

 

operation result:

 

 

 4. Binary search (half search):

It can only be used in an ordered sequence; compare through the middle of the sequence element to be found, if it is greater than the middle, search from the right, if it is less than the middle, then search from the left; high efficiency!

 

 

Learning experience: Learning algorithms is really a test of your logical ability, you can learn yourself crazy! Not even how to define it! It's still a lot of work, especially when I choose a sorting algorithm, it hurts me! In the end, it's cool to clear it! It is recommended that the learning algorithm is to bring the value into it and look at it while comparing! This will make it clear quickly and won't get stuck for a long time!

 

Guess you like

Origin www.cnblogs.com/LZz089/p/12716179.html