简单算法的实现(三)

9.快速排序:

Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, on average, makes Θ(n log n) comparisons to sort n items. However, in the worst case, it makes Θ(n2) comparisons. Typically, quicksort is significantly faster in practice than other Θ(n log n) algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data it is possible to make design choices which minimize the possibility of requiring quadratic time. Quicksort sorts by employing a divide and conquer strategy to divide a list into two sub-lists. The steps are: 1. Pick an element, called a pivot, from the list. 2. Reorder the list so that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. 3. Recursively sort the sub-list of lesser elements and the sub-list of greater elements. The base case of the recursion are lists of size zero or one, which are always sorted. The algorithm always terminates because it puts at least one element in its final place on each iteration (the loop invariant).  Quicksort in action on a list of random numbers. The horizontal lines are pivot values. Write a program to sort ascending int number by QuickSort ,n less than 50000.

 
#include<iostream>    
#include<algorithm>    
#include<cstring>    
#include<cstdio>    
using       namespace       std;    
int       a[50005];    
void       quickSort(       int       l,       int       h)    
{    
      int       i,j,key,t;    
      if       (l>=h)       return       ;    
      i = l,j=h;    
      key = a[l];    
      while       (i!=j)    
      {    
      while       (a[j]>=key &&i<j)       //从右开始找    
      j--;    
      while       (a[i]<=key && i<j)       //再找右边的    
      i++;    
      //交换位置    
      if       (i<j)    
      {    
      t=a[i];    
      a[i]=a[j];    
      a[j]=t;    
      }    
      }    
   
      //交换基数    
      a[l]=a[i];    
      a[i]=key;    
   
      quickSort(l,i-1);       //处理左边的    
      quickSort(i+1,h);       //处理右边的    
}    
      int       main()    
{    
      int       m,i;    
      scanf       (       "%d"       ,&m);    
      for       (i=1;i<=m;i++)    
      {    
      scanf       (       "%d"       ,&a[i]);    
      }    
      quickSort(1,m);    
      for       (i=1;i<=m;i++)    
      {    
      printf       (       "%d "       ,a[i]);    
      }    
      printf       (       "\n"       );    
      return       0;    
}    

 

10:公共子序列:

问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列。令给定的字符序列X=“x0,x1,…,xm-1,序列Y=“y0,y1,…,yk-1X的子序列,存在X的一个严格递增下标序列<i0,i1,…,ik-1>,使得对所有的j=0,1,…,k-1,有xij=yj。例如,X=“ABCBDAB”,Y=“BCDB”是X的一个子序列。

 
#include<algorithm>    
#include<cstring>    
#include<cstdio>    
using        namespace        std;    
#define MAX 100    
 
int        LCS(       char        *x,       char        *y,       int        m,       int        n,       int        arr[][MAX])    
{    
 
           int        i , j;    
           for       (i=0;i<=m;i++)    
           {    
            arr[i][0]=0;    
           }    
           for       (j=0;j<=n;j++)    
           {    
            arr[0][j]=0;    
           }    
           for       (i=1;i<=m;i++)    
           {    
             for       (j=1;j<=n;j++)    
             {    
                 if       (x[i-1] == y[j-1])    
                 {    
                  arr[i][j]=arr[i-1][j-1]+1;    
                 }    
                 else        if       (arr[i][j-1]>arr[i-1][j])    
                 {    
                  arr[i][j]=arr[i][j-1];    
                       else    
                 {    
                 arr[i][j]=arr[i-1][j];    
                 }    
             }    
           }    
return        arr[m][n];    
}    
 
int        main()    
{    
           char        x[MAX] = {       "ABCBDAB"       };    
           char        y[MAX] = {       "BDCABA"       };    
           int        a[MAX][MAX];    
           printf       (       "%d\n"       ,LCS(x,y,       strlen       (x),       strlen       (y),a));    
           return        0;    
}    

你可能感兴趣的:(简单算法的实现(三))