Summary: 1, bubble sort bubble sort is a simple sorting method, the algorithm is as follows: 1. First put all the numbers you want to sort into your work list. 2. From the first number of the list to the second-to-last number, check each: If the number on one is greater than the next, then swap it with the next one.
1. Bubble sort
Bubble sort is a simple sort method, with the following algorithm:
1. First put all the numbers you want to sort into your work list.
2. From the first number of the list to the second-to-last number, check each: If the number on one is greater than the next, then swap it with the next one.
3. Repeat step 2nd (the countdown number plus 1. For example: the first to the penultimate number, the second to the last third number, and so on ...), until it can no longer be exchanged.
The following are implemented in the C language:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int BubbleSort( int *a, int n) //a是待排序的整型数组,n是待排序数组的元素个数
{
int i,j,temp;
for (j=0;j<n-1;j++)
for (i=0;i<n-1-j;i++)
{
if (a[i]>a[i+1]) //数组元素大小按升序排列
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
|
2. Insert Sort
The insertion sort is also a simple sort method, with the following algorithm:
1. Starting with the first element, it is assumed that the element is already sequenced.
2. Remove an element and scan from the back forward in the sequence of elements that are already sequenced.
3. If the element in the ordered sequence is greater than the new element, move the element to the right one position.
4. Repeat step 3 until the ordered element is less than or equal to the new element.
5. Insert the new element at the current location.
6. Repeat step 2.
The following are implemented in C:
???????
Original link
Summary of sorting algorithms