常用的一些算法

 

一、排序

排序算法基本是各大面试或者说笔试的必考之题,所以掌握好排序算法是必备的。

1.冒泡排序

void bubblesort(int arry[],int n)
{
  int i,j,temp;
  for(i=0;i<n-1;i++)
  {
     for(j=0;j<n-i-1;j++)
       if(a[j]>a[j+1])
         {
            temp = a[j+1];
            a[j+1] = a[j];
            a[j] = temp;
          }    
    }  

}        
View Code

2.快速排序

/*
 * 快速排序
 *
 * 参数说明:
 *     arry -- 待排序的数组
 *     low-- 数组的左边界(例如,从起始位置开始排序,则l=0)
 *     high -- 数组的右边界(例如,排序截至到数组末尾,则r=a.length-1)
 */

 void quicksort(int arry[],int low,int high)
 {
  if(low < high)
  {
   int i,j,temp;
   i= low;
   j= high;
   temp = a[i];
   while(i<j)
   {
     while(i<j && a[j]>temp)
       j--;
     if(i<j)
      a[i++] = a[j];
     while(i<j && a[i]<temp)
       i++;
     if(i<j)
       a[j--] = a[i];
    
      }
    a[i]=temp;
    quicksort(arry,low,i-1);
    quicksort(arry,i+1,high);
  }

  }
View Code
posted @ 2014-04-24 11:51  冬虫草。  阅读(304)  评论(0)    收藏  举报