因為資料量太小結果O n 比O n 2 慢

2021-09-26 10:42:16 字數 895 閱讀 3187

題目:

實現 strstr() 函式。

給定乙個 haystack 字串和乙個 needle 字串,在 haystack 字串中找出 needle 字串出現的第乙個位置 (從0開始)。如果不存在,則返回  -1。

o(n^2)

#include#includeint strstr(char * haystack, char * needle) 

int i = 0;

int count = 0;

int tmp = 0;

while (*haystack != '\0' && len<=strlen(haystack))

char *p = needle;

char *q = haystack;

while (*p == *q && *p!='\0')

if (tmp == len)

tmp = 0;

haystack++;

i++;

} return -1;

}

時間及空間:

o(n)

int strstr(char * haystack, char * needle)

char *p = haystack;

char *q = needle;

int count = 0;

int tmp = 0;

while (strlen(p) >= strlen(q) && *q != '\0')

else if(*q != '\0')

p++;

} if (count == len)

return -1;

}int main()

時間及空間

排序演算法大資料量測試結果

2014 01 06 14 53 06 mergesorter排序40000個數1次所用平均時間為 19.0011 毫秒 2014 01 06 14 53 06 mergesorter排序60000個數1次所用平均時間為 26.0015 毫秒 2014 01 06 14 53 06 mergesor...

大資料量下排序演算法小結

堆排序和快速排序的比較 堆排序是接近nlgn的下界,而快排有效能壞的情況,為何還是快排表現更優秀呢?1.堆排序是處理陣列中相隔較遠的資料,快速排序是根據兩個 指標按序遍歷的,根據暫存器 快取記憶體的熱cache 區域性性原理,快排更好 2.快排的極端情況太難復現,而且可以 用隨機基準數 3.快排還有...

大資料量演算法

給40億個不重複的unsigned int的整數,沒排過序的,然後再給乙個數,如何快速判斷這個數是否在那40億個數當中 位圖思想解法 include stdio.h include stdlib.h include memory.h define max num 4294967295 int mai...