题目要求:
问题1:编写实现链表排序的一种算法。
问题2:编写实现数组排序的一种算法。
问题3:编写能直接实现strstr()功能的代码。
问题分析:
问题1分析:
方法1 首先想到的是冒泡排序,因为简单;
方法2 如果内存空间允许,可以通过一个数组来辅助排序,时间复杂度O(NlogN),但是需要耗费空间复杂度.具体如下:
问题2分析:
对数组排序有很多种,根据时间复杂度和空间复杂度的具体要求具体选择,通常选择快排,时间复杂度O(NlogN).
问题3分析:
strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。找到所搜索的字符串,则该函数返回第一次匹配的字符串的地址;如果未找到所搜索的字符串,则返回NULL。
可以采用BF算法和KMP算法,时间复杂度分别为O(M*N)和O(M+N).
代码实现:
问题1代码:
<pre name="code" class="cpp">//方法2
/*
chengzhuwei
*/
#include <iostream>
using namespace std;
typedef struct ListNode
{
struct ListNode *next;
int data;
}ListNode;
void InitList(ListNode **head);
int FindListLength(ListNode *head);
void InitArray(ListNode **tmpArray,ListNode *head);
void PrintList(ListNode *head);
void QuickSort(ListNode **a,int low,int high);
int main(void)
{
ListNode *head;
InitList(&head);
cout << "排序前:";
PrintList(head);
int listLen = FindListLength(head);
ListNode **tmpArray = new ListNode *[listLen];
InitArray(tmpArray,head);
QuickSort(tmpArray,0,listLen-1);
cout << "排序后:";
for(int i = 0;i<listLen;i++)
cout << tmpArray[i]->data << "->";
cout << "NULL" << endl;
return 0;
}
void PrintList(ListNode *head)
{
while(head)
{
cout << head->data << "->";
head = head->next;
}
cout << "NULL" << endl;
}
void InitArray(ListNode **tmpArray,ListNode *head)
{
int i = 0;
while(head)
{
tmpArray[i] = head;
head = head->next;
i++;
}
}
int FindListLength(ListNode *head)
{
int len = 0;
while(head)
{
len++;
head = head->next;
}
return len;
}
//111-->22-->3-->NULL
void InitList(ListNode **head)
{
ListNode *tmp = new ListNode;
tmp->data = 111;
*head = tmp;
tmp = new ListNode;
tmp->data = 22;
(*head)->next = tmp;
ListNode *tmp1 = new ListNode;
tmp1->data = 3;
tmp1->next = NULL;
tmp->next = tmp1;
}
int FindPos(ListNode **a,int low,int high);
void QuickSort(ListNode **a,int low,int high)
{
int pos;
if(low<high)
{
pos = FindPos(a,low,high);
QuickSort(a,low,pos-1);
QuickSort(a,pos+1,high);
}
}
int FindPos(ListNode **a,int low,int high)
{
ListNode *val = a[low];
while(low<high)
{
while( (low<high) && (a[high]->data >= val->data) )
high--;
a[low] = a[high];
while( (low<high) && (a[low]->data <= val->data) )
low++;
a[high] = a[low];
}
a[low] = val;
return low;
}
问题2代码:
#include <stdio.h>
int FindPos(int *a,int low,int high);
void QuickSort(int *a,int low,int high);
int main(void)
{
int a[6] = {5,2,19,4,12,45};
int i;
QuickSort(a,0,5);
for(i=0;i<6;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
void QuickSort(int *a,int low,int high)
{
int pos;
if(low<high)
{
pos = FindPos(a,low,high);
QuickSort(a,low,pos-1);
QuickSort(a,pos+1,high);
}
}
int FindPos(int *a,int low,int high)
{
int val = a[low];
while(low<high)
{
while( (low<high) && (a[high]>=val) )
high--;
a[low] = a[high];
while( (low<high) && (a[low] <= val) )
low++;
a[high] = a[low];
}
a[low] = val;
return low;
}
问题3代码://BF算法
#include <stdio.h>
const char *my_strstr(const char *str, const char *sub_str)
{
if(str==NULL || sub_str==NULL)
return NULL;
for(int i = 0; str[i] != '\0'; i++)
{
int tem = i; //tem 保留主串中的起始判断下标位置
int j = 0;
while(str[tem++] == sub_str[j++])
{
if(sub_str[j] == '\0' )
{
return &str[i];
}
}
}
return NULL;
}
int main()
{
char *s = "1233345hello" ;
char *sub = "345" ;
printf( "%s\n", my_strstr(s, sub));
return 0;
}
//KMP算法
#include <stdio.h>
#include <string.h>
void compute_prefix(int *next, char *p);
char *kmp_match(char *text, char *p, int *next);
int main()
{
int next[101], n;
char *p = "345" ;
char *text = "1233345hello" ;
compute_prefix(next, p);
printf( "%s\n",kmp_match(text, p, next));
return 0;
}
void compute_prefix(int *next, char *p)
{
int i, n, k;
n = strlen(p);
next[1] = next[0] = 0;
k = 0; /* 第i 次迭代开始之前, k表示next[i-1] 的值*/
for (i = 2; i <= n; i++) {
if (p[k] == p[i-1])
k++;
else
k = 0;
next[i] = k;
}
}
char *kmp_match(char *text, char *p, int *next)
{
int m, n, s, q,num=0;
m = strlen(p);
n = strlen(text);
q = s = 0; /* q表示上一次迭代匹配了多少个字符,
s 表示这次迭代从 text的哪个字符开始比较*/
while (s < n) {
for (q = next[q]; q < m && p[q] == text[s]; q++, s++);
if (q == 0) s++;
else if (q == m) {
return text+s-m;
}
}
return NULL;
}