数据结构排序、查找算法
前言
這是數(shù)據(jù)結(jié)構(gòu)的實(shí)驗(yàn)四的題目。
為了自己能在繁雜的文件中順利、快速地找到自己熟悉的排序、查找算法代碼,故借CSDN平臺(tái)存放本人寫的代碼。另外,還請(qǐng)有緣看到此文章的同行們能多多指點(diǎn)。
非常感謝。
1 各種排序算法的實(shí)現(xiàn)
用隨機(jī)函數(shù)生成16個(gè)2位正整數(shù)(10~99),實(shí)現(xiàn) 插入排序、 選擇排序、 冒泡排序、 雙向冒泡、 快速排序、 二路歸并排序 等多種排序算法,輸出排序中間過(guò)程、統(tǒng)計(jì)關(guān)鍵字的比較次數(shù)和記錄的移動(dòng)次數(shù)。
1.1 代碼
代碼說(shuō)明:
各排序算法在同一個(gè)cpp文件中,要查看某個(gè)算法時(shí)可以把main函數(shù)中的相應(yīng)排序算法的注釋符號(hào)“//”去掉即可;
1.2 運(yùn)行結(jié)果
- 插入排序
- 選擇排序
- 冒泡排序
- 雙向冒泡排序
- 快速排序
- 歸并排序
2 各種查找算法的實(shí)現(xiàn)
2.1 順序查找
使用數(shù)組或鏈表結(jié)構(gòu)。用隨機(jī)函數(shù)生成16個(gè)不重復(fù)的字母(’a’~’z’),鍵盤輸入待查找的字母,返回查找成功與否,若成功則返回該字母所在的位置(序號(hào)),并計(jì)算比較次數(shù)。
2.1.1 代碼
#include<iostream> #include<ctime> using namespace std;//-----順序查找----- void Search_Seq(char* s) {cout << "請(qǐng)輸入想尋找的字母:";char x;cin >> x; //輸入想找的字母int index = 0;int count = 0; //比較次數(shù),初始化為0while (s[index] != '\0') {++count;if (s[index++] == x) {cout << "已找到字母 " << x << " ,它的下標(biāo)為:" << index - 1 << endl;break;}}if (count == 17) {cout << "沒有找到該字母,共比較了:" << count-1 << " 次"<< endl;}else cout << "共比較了:" << count << " 次"; }int main() {/*-----------------------------------------說(shuō)明:隨機(jī)生成數(shù)組首元素下標(biāo)為 0-----------------------------------------*/// 生成隨機(jī)數(shù)組srand(unsigned(time));char* s=new char[17]; //字符數(shù)組for (int i = 0; i < 16; i++) s[i] = '\0';cout << "生成的數(shù)組為:";int k = 0;while (k != 16) {int flag = 0; //用于檢測(cè)是否有相同的字母出現(xiàn)s[k] = 'a' + (rand() % 26);for (int i = 0; i < 16; i++) {if (k != i) {if (s[k] == s[i]) flag = 1; //若flag=1,則重新加入一個(gè)字母}}if (flag == 0) ++k; //若flag=0,往數(shù)組下一個(gè)位置添加字母}for (int i = 0; i < 16; i++) cout << s[i] << " "; //打印隨機(jī)生成的字符數(shù)組s[16] = '\0'; //字符串的最后一位是'\0'cout << endl;//-----順序查找-----Search_Seq(s); }2.1.2 運(yùn)行結(jié)果
- 查找成功
- 查找失敗
2.2 折半查找
用數(shù)組實(shí)現(xiàn),查找前元素先排序。計(jì)算比較次數(shù)。分別用查找成功、不成功進(jìn)行測(cè)試。
2.2.1 代碼
#include<iostream> #include<ctime> using namespace std;//-----順序查找-----void TwowaybubbleSort(char* s) {int n = 16;int flag = 1; //檢測(cè)是否已排序完成int left = 1; //初始化待排序數(shù)組的最左端元素序號(hào)int right = 15; //初始化待排序數(shù)組的最右端元素序號(hào)int temp_right = 0; //暫存待更新待排序數(shù)組的最右端元素序號(hào)int temp_left = 0; //暫存待更新待排序數(shù)組的最左端元素序號(hào)//設(shè)置兩重循環(huán)while (n-- && flag) {flag = 0; for (int j = left; j < right; j++) {if (s[j] > s[j + 1]) {flag = 1;int temp = s[j];s[j] =s[j + 1];s[j + 1] = temp;temp_right = j;}}right = temp_right; //更新待排序數(shù)組的最右端元素序號(hào)for (int j = right; j > left; j--) {if (s[j] < s[j - 1]) {flag = 1;int temp = s[j];s[j] = s[j - 1];s[j - 1] = temp;temp_left = j;}}} } void Search_Bin(char* s) {TwowaybubbleSort(s); //先使用雙向冒泡進(jìn)行排序int compare_count = 0; //記錄比較次數(shù)cout << "請(qǐng)輸入想尋找的字母:";char x;cin >> x; //輸入待尋找的字母int low = 0; int high = 15;int mid = 0;while (low <= high) {mid = (low + high) / 2;if (x == s[mid]) {compare_count++;cout << "已找到字母 " << x << endl;break;}else if (x < s[mid]) {compare_count++;high = mid - 1;}else {compare_count++;low = mid + 1;}}if(low>high&&x!=s[low]) cout<< "未找到該字母,總共比較了 " << compare_count << " 次" << endl;else cout << "總共比較了 " << compare_count << " 次" << endl; }int main() {/*-----------------------------------------說(shuō)明:隨機(jī)生成數(shù)組首元素下標(biāo)為 0-----------------------------------------*/// 生成隨機(jī)數(shù)組srand(unsigned(time));char* s = new char[17]; //字符數(shù)組for (int i = 0; i < 16; i++) s[i] = '\0';cout << "生成的數(shù)組為:";int k = 0;while (k != 16) {int flag = 0; //用于檢測(cè)是否有相同的字母出現(xiàn)s[k] = 'a' + (rand() % 26);for (int i = 0; i < 16; i++) {if (k != i) {if (s[k] == s[i]) flag = 1; //若flag=1,則重新加入一個(gè)字母}}if (flag == 0) ++k; //若flag=0,往數(shù)組下一個(gè)位置添加字母}for (int i = 0; i < 16; i++)cout << s[i] << " "; //打印隨機(jī)生成的字符數(shù)組s[17] = '\0'; //字符串的最后一位是'\0'cout << endl;//-----折半查找-----Search_Bin(s); }2.2.2 運(yùn)行結(jié)果
- 查找成功
- 查找失敗
2.3 二叉查找樹
手工輸入10個(gè)字母,生成一棵二叉查找樹,用遞歸算法打印樹結(jié)構(gòu)或分別輸出先序和中序遍歷序列以確認(rèn)其結(jié)構(gòu)。鍵盤輸入待查找的字母,計(jì)算比較次數(shù)。分別用查找成功、不成功進(jìn)行測(cè)試.
2.3.1 代碼
#include<iostream> using namespace std;//二叉查找樹結(jié)構(gòu) typedef struct BSTNode {char data;BSTNode* lchild, * rchild; }BSTNode,*BSTree;//二叉排序樹的插入 void InsertBST(BSTree& T, char x) {if (!T) {BSTNode* new_tree = new BSTNode; //生成新結(jié)點(diǎn)new_tree->data = x;new_tree->lchild = new_tree->rchild = NULL;T = new_tree;}else if (x < T->data) InsertBST(T->lchild, x);else if (x > T->data) InsertBST(T->rchild, x); } //二叉排序樹的創(chuàng)建 void CreatBST(BSTree& T) {T = NULL;char x;cin >> x;int i = 0; //用于提示已經(jīng)輸入了多少個(gè)字母while (x != '#') {InsertBST(T, x);cout << "這是輸入的第 " << ++i << " 個(gè)字母" << endl;cin >> x;} } //二叉樹的先序遍歷 void Preorder(BSTree T)//前序遍歷(遞歸實(shí)現(xiàn)) {if (T != NULL){cout << T->data << " ";Preorder(T->lchild);Preorder(T->rchild);} } //二叉樹的中序遍歷 void Mreorder(BSTree T)//前序遍歷(遞歸實(shí)現(xiàn)) {if (T != NULL){Mreorder(T->lchild);cout << T->data << " ";Mreorder(T->rchild);} }//-----二叉查找樹----- int compare_count = 0; //全局變量。記錄比較次數(shù) BSTree SearchBST(BSTree T, char x) {compare_count++;if (!T|| x == T->data) {if (!T)cout << "查詢失敗,共查找了 " << compare_count << " 次\n";elsecout << "已找到字母 " << T->data << " ,共查找了 " << compare_count << " 次\n";return T;}else if (x < T->data) return SearchBST(T->lchild, x);else if (x >= T->data) return SearchBST(T->rchild, x); }int main() {//創(chuàng)建二叉查找樹BSTree T = new BSTNode;cout << "請(qǐng)輸入十個(gè)字母(輸入#結(jié)束): " << endl;CreatBST(T); cout<<"輸入完成\n";//中序遍歷二叉查找樹cout << "\n先序遍歷輸出:";Preorder(T);//中序遍歷二叉查找樹cout << "\n中序遍歷輸出(按照各字母的ASCⅡ碼大小):"; Mreorder(T);//輸入待查找字母cout << "\n請(qǐng)輸入想查詢的字母(如果想退出查找,請(qǐng)輸入$) ";char letter;cin >> letter; //查找字母while (1 && letter != '$') {SearchBST(T, letter);cout << "\n請(qǐng)輸入想查詢的字母(如果想退出查找,請(qǐng)輸入$) ";cin >> letter;} }2.3.2 運(yùn)行結(jié)果
查找成功和失敗都在同一張截圖里了。
總結(jié)
以上是生活随笔為你收集整理的数据结构排序、查找算法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于深度学习的异构时序事件患者数据表示学
- 下一篇: 作者:吴书(1982-),男,中国科学院