C Language Note: + improved bubble sort succinctly

Original link: http://c.biancheng.net/c/

+ Realization algorithm description

  • The first round: N elements from top to bottom scanning of the array, a first pair of adjacent first comparison element (the first element and the second element), if the reverse order (first element greater than the second element) is exchange of small elements on the move, and then comparing the second pair of adjacent element (second element and third element), if the reverse is still switching, and so on, after the first round of the processing, the small elements move slowly , while the largest element is moved to the last position, which is that it should be in the final position (the bottom)
  • Second round: For the N-1 element of disorder, is still an element from a first adjacent pair to the last pair of adjacent elements in sequence comparison, if the reverse exchanged, the first wheel is different, the comparison less a pair of the results of the second element is moved to a large penultimate position, after which its final position is sorting, i.e. the precipitates to the bottom of the N-1 element of disorder.

such as:
Here Insert Picture Description

Implementation code:

#include <stdio.h>
#define N 6
void Input(int r[],int n);//输入数组的n个元素
void Output(int r[],int n);//输出数组的n个元素
void BubbleSort(int r[],int n);//使用冒泡排序法对数组的n个元素进行排序
void Swap(int *pa,int *pb);//交换pa和pb所指变量的值 
int main(void)
{
 int a[N];//定义大小为N的数组
 printf("输入数组的%d个元素:\n",N);
 Input(a,N);//输入数组的N个元素 
 printf("排序前数组a的元素为:");
 Output(a,N);//输出排序前数组的元素
 BubbleSort(a,N);//冒泡排序
 printf("排序后数组a的元素为:");
 Output(a,N);
 return 0; 
} 
void Input(int r[],int n)
{
 for(int i=0;i<n;i++)
 {
  scanf("%d",&r[i]);
 }
}
void Output(int r[],int n)
{
 for(int i=0;i<n;i++)
 {
  printf("%-4d",r[i]);
 }
 printf("\n");
}
void BubbleSort(int r[],int n)
{
 for(int i=1;i<n;i++)//控制轮数 
 {
  for(int j=0;j<=n-1-i;j++)//每一轮对于无序区进行比较 
  {
   if(r[j]>r[j+1])
   {
    Swap(&r[j],&r[j+1]); 
   }
  }
 } 
}
void Swap(int *pa,int *pb)
{
 int temp;
 temp=*pa;
 *pa=*pb;
 *pb=temp;
}

The result:
Here Insert Picture Description
the first wheel (i = 1), 6 need to compare the five elements (j cycles from 0 to 4; j = 0, the comparison r [0] and r [1]; when j = 1, Comparative r [1] and r [2], and so on), then the last indexing array elements stored maximum, then compares the first five elements

The second round (i = 2), 5 need to compare the elements 4 (j cycles from 0 to 3; j = 0, the comparison r [0] and r [1]; when j = 1, Comparative r [1 ] [2], and R & lt so on), this time the inverse of the array and the second subscript the second largest storage element, the first four elements are then compared

Improved a

It is seen from above and FIG codes, in the third round of the bubble sort, the sort which the entire array is completed, but still above code scanning cycle to the fourth round, the fifth wheel. That is, whether the case if the bubble sort, N-1 loop must round all the sorted array is improved good, no longer cyclic scan

#include <stdio.h>
#define N 6
void Input(int r[],int n);//输入数组的n个元素
void Output(int r[],int n);//输出数组的n个元素
void BubbleSort(int r[],int n);//使用冒泡排序法对数组的n个元素进行排序
void Swap(int *pa,int *pb);//交换pa和pb所指变量的值 
int main(void)
{
 int a[N];//定义大小为N的数组
 printf("输入数组的%d个元素:\n",N);
 Input(a,N);//输入数组的N个元素 
 printf("排序前数组a的元素为:");
 Output(a,N);//输出排序前数组的元素
 BubbleSort(a,N);//冒泡排序
 printf("排序后数组a的元素为:");
 Output(a,N);
 return 0; 
} 
void Input(int r[],int n)
{
 for(int i=0;i<n;i++)
 {
  scanf("%d",&r[i]);
 }
}
void Output(int r[],int n)
{
 for(int i=0;i<n;i++)
 {
  printf("%-4d",r[i]);
 }
 printf("\n");
}
void BubbleSort(int r[],int n)
{
 int swapflag=1;//设置swapflag为某轮是否有元素交换的标记,有交换则其值为1,否则为0,保证第1次必须扫描,初始值为1 
 for(int i=1;swapflag&&i<n;i++)//控制轮数,在某一轮排序结束后如果swapflag=0,则表示整个数组的排序完成,不再循环后面的轮数 
 {
  swapflag=0;
  for(int j=0;j<=n-1-i;j++)//每一轮对于无序区进行比较 
  {
   if(r[j]>r[j+1])
   {
    Swap(&r[j],&r[j+1]); 
    swapflag=1; 
   }
  }
  printf("%d轮排序结果:",i);
  Output(r,n);
 } 
}
void Swap(int *pa,int *pb)
{
 int temp;
 temp=*pa;
 *pa=*pb;
 *pb=temp;
}

operation result:
Here Insert Picture Description

Improved two

We know that every round of sorting, the largest element will be selected from random sequence of N-1, such as we proceed to the third round of sorting, then the maximum element r [5], a second large element r [4], third element r [. 3], already occupy three reciprocal position r subscript of an array, during the fourth round of sorting, without the reciprocal value of the loop r 3 Comparative array elements, based on this idea, we setting a variable lastSwapIndex record one pair of switching elements on the last one of the index of the preceding elements , the first one should start the exchange, and if there is a range switching position according to the determined round on a comparison.

#include <stdio.h>
#define N 6
void Input(int r[],int n);//输入数组的n个元素
void Output(int r[],int n);//输出数组的n个元素
void BubbleSort(int r[],int n);//使用冒泡排序法对数组的n个元素进行排序
void Swap(int *pa,int *pb);//交换pa和pb所指变量的值 
int main(void)
{
 int a[N];//定义大小为N的数组
 printf("输入数组的%d个元素:\n",N);
 Input(a,N);//输入数组的N个元素 
 printf("排序前数组a的元素为:");
 Output(a,N);//输出排序前数组的元素
 BubbleSort(a,N);//冒泡排序
 printf("排序后数组a的元素为:");
 Output(a,N);
 return 0; 
} 
void Input(int r[],int n)
{
 for(int i=0;i<n;i++)
 {
  scanf("%d",&r[i]);
 }
}
void Output(int r[],int n)
{
 for(int i=0;i<n;i++)
 {
  printf("%-4d",r[i]);
 }
 printf("\n");
}
void BubbleSort(int r[],int n)
{
 int lastSwapIndex=n-1;//初始化为数组r最后一个下标
 for(int i=1;lastSwapIndex>0;i++)
 {
  int temp=-1;//每次进行排序temp值重置为-1, 
  for(int j=0;j<lastSwapIndex;j++)
  {
   if(r[j]>r[j+1])
   {
    Swap(&r[j],&r[j+1]);
    temp=j;
   }
  }
  printf("%d轮排序结果:",i);
  Output(r,lastSwapIndex+1);//输出本轮扫描的元素以及交换后的结果 
  lastSwapIndex=temp;//本轮最后一对交换前面一个元素下标赋值给lastSwapIndex,由于第3次的时候没有进行任何交换操作,lastSwapIndex=temp=-1退出最外层循环 
 } 
}
void Swap(int *pa,int *pb)
{
 int temp;
 temp=*pa;
 *pa=*pb;
 *pb=temp;
}

Output:
Here Insert Picture Description

Stability issues on bubble sort of

First, what is the stability of the algorithm to sort it? For example, a group of students (there are school, grades) according to results in ascending order, if two equal student achievement, by location sort before sorting , which requires sorting algorithm must be stable. For bubble sort algorithm, any movement that occurs when the element [j]> neighbors r r [j + 1] condition is established, so when r [j] == r [j + 1] , the exchange does not meet condition, without changing their relative position, the bubble sort is stable.

Guess you like

Origin blog.csdn.net/weixin_42124234/article/details/102474771