C language detailed bubble sort
1. The basic idea
Bubble sort is the easiest and most classic sort of exchange sort. The basic idea is to compare the sizes of two adjacent elements from left to right. If the left side is larger than the right side, then exchange. After sorting, you can make the largest The value floats to the far right. Keep repeating this operation to keep the array in order.
2. Algorithm implementation and optimization
To achieve bubble sorting, we first write a bubble sorting. It's very simple. You can compare from left to right. As long as the left is greater than the right, the exchange will occur.
Note: The
array has n elements from the array we start the first element compared to the array end of the first n-1 elements were compared n-1 times. For example, in the figure below, starting from the first to the end of the sixth. The sorting code can be written in one trip:
//1.第1趟排序,从第1个元素开始到n-1个元素结束,比较n-1次
for (int j = 0; j < n-1; ++j)
{
//如果左边大于右边,则交换位置
if (a[j] > a[j + 1])
{
Swap(&a[j], &a[j + 1]);
}
}
After sorting in this way, the largest number can be popped to the far right. If we repeat this operation n-1 times, the next largest number and the next largest number can be popped to the far right, and a loop is completed.
void BubbleSort1(int* a, int n)
{
//排n趟就可以让数组有序
int end=n-1;
while(end>0)
{
//一趟排序,从第一个元素开始到n-1个元素结束,
for(int j = 0; j<n-1; ++j)
{
//如果左边大于右边,就交换
if(a[j]>a[j+1])
{
Swap(&a[j],&a[j+1]);
}
}
--end;
PrintArr(a,n);
}
}
Although this code can make the array in order, it has redundant sorting, because the largest element in the array can be determined after one pass of sorting, and the position of the next largest element is also determined after the second pass of sorting , so we can continue Optimized code
As shown in the figure, the first pass starts from one element for n-1 times, and 56 goes to the far right; the
second pass, the position of the last element is determined, only compare n-2 times, and 55 goes to the second right;
… …
The sixth pass only needs to compare once to make the array in order.
Assuming that there are n elements in the array, you need to arrange n-1 times, and each pass is represented by i. The i-th pass needs to compare ni-1 times , the code
void BubbleSort2(int* a, int n)
{
//n-1趟排序
for(int i = 0; i < n-1; ++i)
{
//每一趟比较n-i-1次
for(int j=0; j<n-i-1; ++j)
{
if(a[j]>a[j+1])
{
Swap(&a[j],&a[j+1]);
}
}
PrintArr(a,n);
}
}
This algorithm can still be optimized. Assuming that the array is already ordered after the i-th sorting, then don’t compare it later. This optimization is simpler. You only need to define a flag. If the flag does not change in one sorting pass, We can think that this array is already in order and exit the loop.
void BubbleSort(int* a, int n)
{
//n-1趟排序
for(int i = 0; i < n-1; ++i)
{
int flag = 0; //设置flag
//每一趟比较n-i-1次
for(int j=0; j<n-i-1; ++j)
{
if(a[j]>a[j+1])
{
flag = 1; //如果该趟排序发生交换,flag置1
Swap(&a[j],&a[j+1]);
}
}
PrintArr(a,n);
//一趟排序过后没有发生变化,说明数组已经有序
if(flag == 0)
break;
}
}
Three, the code list
#include <stdio.h>
void PrintArr(int* a, int n)
{
for(int i = 0; i < n; ++i)
{
printf("%d ",a[i]);
}
printf("\n");
}
void Swap(int* px, int* py)
{
int tmp = *px;
*px = *py;
*py = tmp;
}
void BubbleSort1(int* a, int n)
{
//排n趟就可以让数组有序
int end=n-1;
while(end>0)
{
//一趟排序,从第一个元素开始到n-1个元素结束,
for(int j = 0; j<n-1; ++j)
{
//如果左边大于右边,就交换
if(a[j]>a[j+1])
{
Swap(&a[j],&a[j+1]);
}
}
--end;
PrintArr(a,n);
}
}
void BubbleSort2(int* a, int n)
{
//n-1趟排序
for(int i = 0; i < n-1; ++i)
{
//每一趟比较n-i-1次
for(int j=0; j<n-i-1; ++j)
{
if(a[j]>a[j+1])
{
Swap(&a[j],&a[j+1]);
}
}
PrintArr(a,n);
}
}
void BubbleSort(int* a, int n)
{
//n-1趟排序
for(int i = 0; i < n-1; ++i)
{
int flag = 0; //设置flag
//每一趟比较n-i-1次
for(int j=0; j<n-i-1; ++j)
{
if(a[j]>a[j+1])
{
flag = 1; //如果该趟排序发生交换,flag置1
Swap(&a[j],&a[j+1]);
}
}
PrintArr(a,n);
//一趟排序过后没有发生变化,说明数组已经有序
if(flag == 0)
break;
}
}
int main()
{
int a[] = {
25,6,56,24,9,12,55};
int b[] = {
25,6,56,24,9,12,55};
int c[] = {
25,6,56,24,9,12,55};
int n = sizeof(a)/sizeof(a[0]);
printf("-----BubbleSort------\n");
BubbleSort(a,n);
printf("-----BubbleSort1------\n");
BubbleSort1(b,n);
printf("-----BubbleSort2------\n");
BubbleSort2(c,n);
//PrintArr(a,n);
return 0;
}