in development, it is often necessary to arrange a set of data in an orderly manner, so master several or even more sorts . algorithm is absolutely necessary.
here to a simpler algorithm in the sorting algorithm is introduced: bubble sort.
Try to compare learning bubbles by trying to sort by the simplest idea.
has an array whose size is 10 elements (int str[10]) and the data within the array is unordered. It is now required that we program this unordered array into an array of small to large orders (starting with subscript 0) .
according to the requirements of the topic, there is no doubt that the correct result should be like this: 1 2 3 4 5 6 7 8 9 To do so, the simplest and most straightforward way to think about is to do a contrast exchange.
- First, put Ten The smallest number in the number is placed in the subscript 0 the location (str[0])
- by adding Subscript is 0 the number (str[0]) with the rest of the remaining 9 The number of the smallest one can be obtained by making a comparison exchange (placing fewer people in the position labeled 0 ).
- Ten when the smallest number is determined, the next thing you need is the rest. 9 The one with the smallest number.
- because a minimum number has been determined, so don't move . Str[0] , directly from Str[1] start by swapping with the remaining 8 numbers to find out where the smallest of the 9 numbers is placed on the subscript 1 (str[1]) .
- continue to follow this line of thought to turn these 10 numbers into an orderly one ( from small to large ) array.
Code:
- #include <stdio.h>
- void swap (int *a, int *b); Exchange two numbers
- int Main ()
- {
- int str[10];
- int I, J;
- Initialize array to 10 9 8 7 6 5 4 3 2 1
- for (i = 0; i < i++)
- {
- Str[i] = 10-i;
- }
- Sort, from a[0] start platoon, small to large
- for (i = 0; i < i++)
- {
- for (j = i + 1; j <; J + +)
- {
- if (Str[i] > Str[j])
- {
- Swap (&str[i], &str[j]);
- }
- }
- }
- 10-digit output
- for (i = 0; i < i++)
- printf ("%d\n", Str[i]);
- return 0;
- }
- void swap (int *a, int *b)
- {
- int C;
- c = *a;
- *a = *b;
- *b = C;
- }
This method is more easily thought of as an implementation method. But there is a shortage: the smaller number that is originally in front is swapped to the back.
For example:
Start: 9 4 5 6 8 3 2 7 1 (the subscript is 0~9 from left to right respectively) follow the procedure above to exchange
First time: 4 9 5 6 8 3 2 7 ten 1
Second time: 4 9 5 6 8 3 2 7 ten 1
。。。 : (No Exchange)
Fifth time: 3 9 5 6 8 4 2 7 1
Sixth time: 2 9 5 6) 8 3 4 7 1
。。。 : (No Exchange)
Tenth time: 1 9 5 6 8 3 4 7 2
As can be seen, the original small number is in the front, after a round of exchange and put in the back
So how to solve this deficiency? You can use bubble sorting.
What is bubble sort?
You can understand this: (from small to large sort) There are 10 different sizes of bubbles, from the bottom to the lower bubbles gradually upward, so after traversing once, the smallest bubbles will be raised to the top (subscript 0), and then from the bottom up to this rise, Loop until 10 bubbles are of an orderly size.
in bubble sort in which The most important idea is a 22 comparison, which will raise the two less.
Code:
- #include <stdio.h>
- void swap (int *a, int *b);
- int Main ()
- {
- int Array[10] = {15, 225, 34, 42, 52, 6, 7856, 865, 954, 10};
- int I, J;
- for (i = 0; i < i++)
- {
- Each rising from bottom to top
- for (j = 9; j > i; j--)
- {
- if (Array[j] < array[j-1])
- {
- Swap (&array[j], &array[j-1]);
- }
- }
- }
- for (i = 0; i < i++)
- {
- printf ("%d\n", Array[i]);
- }
- return 0;
- }
- void swap (int *a, int *b)
- {
- int temp;
- temp = *a;
- *a = *b;
- *b = temp;
- }
The bubble sorting algorithm will only reduce the number of progressive push-ups, and will not cause the article to be said earlier.
The bubble sort method in JavaScript