天天看点

(排序算法)linux c语言实现直接插入排序和冒泡排序

以下两个代码,只有排序部分是不同的,可以看到直接插入排序算法要比冒泡排序算法略快,

直接插入排序,就是将第一个元素视为已经排好序的,再将第二个元素插入到已经排好序的队列中,依次类推,

冒泡排序,就是两两比较,12,23,34,...,最后会有一个最大或者最小的数字放到两头(最前或者最后),然后剩下的元素再做如此操作。

/***************************************************
##filename      : arrinsert.c
##author        : GYZ                                             
##create time   : 2018-10-31 09:54:39
##last modified : 2018-10-31 17:04:29
##description   : NA                                
***************************************************/
#include <stdio.h>                                  
#include <stdlib.h>                                 
#include <string.h>                                 
#include <time.h>
                                                    
void arrInsert(int a[],int n)
{
  int temp = 0;
  int i = 0,j = 0;
  
  for(i = 1; i < n; ++i)
  {
    temp = a[i];  /* just a flag*/
    for(j = i-1; j >= 0; --j)  /*compare from (i-1 to 0) with the flag*/
    {
      if(temp < a[j])
      {
        a[j+1] = a[j];
        a[j] = temp;
      }
      else
      {
        break;
      }
    }
  }
}
void printArr(int a[],int n)
{
  int i;
  for(i = 0; i < n; i++)
  {
    printf("%d,",a[i]);
  }                      
  printf("\n");                             

}                                                    
int main(int argc,char *argv[])                     
{                                                   
  int length = 0;
  int begin,end;

  int a[] = {21,22,23,24,25,26,27,28,29,30,11,12,13,14,15,16,17,18,19,20,10,3,5,6,1,9,4,8,2,7};
  
  length = sizeof(a) / sizeof(a[0]);
  begin = clock();
  arrInsert(a,length);
  end = clock();
  printf("%d\n",end-begin);
  printArr(a,length);

  return 0;                                       
}                                                   
                                                    
                                                          
/***************************************************
##filename      : bubbleinsert.c
##author        : GYZ                                           
##create time   : 2018-10-31 09:54:39
##last modified : 2018-10-31 17:02:34
##description   : NA                                
***************************************************/
#include <stdio.h>                                  
#include <stdlib.h>                                 
#include <string.h>                                 
#include <time.h>
                                                    
void bubbleInsert(int a[],int n)
{
  int temp = 0;
  int i = 0,j = 0;
  
  for(i = 0; i < n; ++i) 
  {
    for(j = i+1; j < n; ++j)
    {
      if(a[i] > a[j])
      {
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
      }
    }
  }
}
void printArr(int a[],int n)
{
  int i;
  for(i = 0; i < n; i++)
  {
    printf("%d,",a[i]);
  }                      
  printf("\n");                             

}                                                    
int main(int argc,char *argv[])                     
{                                                   
  int length = 0;
  int begin,end;
  int a[] = {26,20,10,14,15,21,22,23,24,25,28,29,30,3,5,6,1,9,4,8,2,7,11,13,12,27,18,19,16,17};
  
  length = sizeof(a) / sizeof(a[0]);
  
  begin = clock();
  bubbleInsert(a,length);
  end = clock();
  printf("%d\n",end-begin);
  printArr(a,length);

  return 0;                                       
}                                                   
                                                    
                                                          

继续阅读