日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

数据结构排序、查找算法

發(fā)布時(shí)間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构排序、查找算法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

這是數(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)“//”去掉即可;

#include<iostream> #include<ctime> using namespace std;const int maxnum = 1000; //數(shù)組結(jié)構(gòu) typedef struct sqlist {int l[maxnum];int length; }sqlist;//------插入排序-----(采用“從后向前比較”策略) void InsertSort(sqlist list, int length) {int compare_count = 0; //關(guān)鍵字比較次數(shù)int remove_count = 0; //關(guān)鍵字移動(dòng)次數(shù)int process = 1; //第n次中間過(guò)程for (int i = 2; i <= list.length; i++) {compare_count++; //關(guān)鍵字比較次數(shù)+1if (list.l[i] < list.l[i - 1]) {list.l[0] = list.l[i]; // list.l[0]: 人稱“哨兵”list.l[i] = list.l[i - 1]; //將list.[i]后移int j = 0;for (j = i - 2; list.l[0] < list.l[j]; j--) {list.l[j + 1] = list.l[j]; //將記錄逐個(gè)后移} list.l[j + 1] = list.l[0]; //將“哨兵”插入到正確的位置remove_count++; //關(guān)鍵字移動(dòng)次數(shù)+1}//中間過(guò)程輸出cout << "第" << process++ << "次中間過(guò)程輸出:" << endl;for (int i = 1; i <= list.length; i++) {cout << list.l[i] << ' ';}cout << endl;}//輸出關(guān)鍵字比較次數(shù)和關(guān)鍵字移動(dòng)次數(shù)cout << "關(guān)鍵字比較次數(shù):" << compare_count << "\t"<< "關(guān)鍵字移動(dòng)次數(shù):" << remove_count << endl; }//-----選擇排序----- void SelectSort(sqlist& list) {int k = 0;int process = 1; //第n次中間過(guò)程int compare_count = 0; //關(guān)鍵字比較次數(shù)int remove_count = 0; //關(guān)鍵字移動(dòng)次數(shù)for (int i = 1; i < list.length; i++) {k = i;for (int j = i + 1; j <= list.length; j++) {compare_count++; //關(guān)鍵字比較次數(shù)+1if (list.l[j] < list.l[k]) k = j; //尋找數(shù)組剩余未排序元素中的值最小元素的下標(biāo)if (k != i) {remove_count++; //關(guān)鍵字移動(dòng)次數(shù)+1int temp = list.l[i];list.l[i] = list.l[k];list.l[k] = temp;}}//中間過(guò)程輸出cout << "第" << process++ << "次中間過(guò)程輸出:" << endl;for (int i = 1; i <= list.length; i++) {cout << list.l[i] << ' ';}cout << endl;}//輸出關(guān)鍵字比較次數(shù)和關(guān)鍵字移動(dòng)次數(shù)cout << "關(guān)鍵字比較次數(shù):" << compare_count << "\t"<< "關(guān)鍵字移動(dòng)次數(shù):" << remove_count << endl;}//-----冒泡排序----- void BubbleSort(sqlist& list) {int n = list.length; //作為結(jié)束循環(huán)的條件之一int flag = 1; //是否排序完成的標(biāo)記int process = 1; //第n次中間過(guò)程int compare_count = 0; //關(guān)鍵字比較次數(shù)int remove_count = 0; //關(guān)鍵字移動(dòng)次數(shù)//設(shè)置兩重循環(huán)while (n--&&flag) {flag = 0;for (int j = 1; j < list.length; j++) {compare_count++; //關(guān)鍵字比較次數(shù)+1if (list.l[j] > list.l[j + 1]) {remove_count++; //關(guān)鍵字移動(dòng)次數(shù)+1flag = 1;int temp = list.l[j];list.l[j] = list.l[j + 1];list.l[j + 1] = temp;}}//中間過(guò)程輸出cout << "第" << process++ << "次中間過(guò)程輸出:" << endl;for (int i = 1; i <= list.length; i++) {cout << list.l[i] << ' ';}cout << endl;}//輸出關(guān)鍵字比較次數(shù)和關(guān)鍵字移動(dòng)次數(shù)cout << "關(guān)鍵字比較次數(shù):" << compare_count << "\t"<< "關(guān)鍵字移動(dòng)次數(shù):" << remove_count << endl; }//-----雙向冒泡---- void TwowaybubbleSort(sqlist& list) {int n = list.length;int flag = 1;int process = 1; //第n次中間過(guò)程int compare_count = 0; //關(guān)鍵字比較次數(shù)int remove_count = 0; //關(guān)鍵字移動(dòng)次數(shù)int left= 1; //初始化待排序數(shù)組的最左端元素序號(hào)int right= list.length; //初始化待排序數(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++) {compare_count++; //關(guān)鍵字比較次數(shù)+1if (list.l[j] > list.l[j + 1]) {flag = 1;remove_count++; //關(guān)鍵字移動(dòng)次數(shù)+1int temp = list.l[j];list.l[j] = list.l[j + 1];list.l[j + 1] = temp;temp_right = j;}}right = temp_right; //更新待排序數(shù)組的最右端元素序號(hào)for (int j = right; j >left; j--) {compare_count++; //關(guān)鍵字比較次數(shù)+1if (list.l[j] < list.l[j-1]) {flag = 1;remove_count++; //關(guān)鍵字移動(dòng)次數(shù)+1int temp = list.l[j];list.l[j] = list.l[j - 1];list.l[j - 1] = temp;temp_left = j;}}left = temp_left; //更新待排序數(shù)組的最左端元素序號(hào)//中間過(guò)程輸出cout << "第" << process++ << "次中間過(guò)程輸出:" << endl;for (int i = 1; i <= list.length; i++) {cout << list.l[i] << ' ';}cout << endl;cout << "left,right:" << left << ' ' << right << endl;}//輸出關(guān)鍵字比較次數(shù)和關(guān)鍵字移動(dòng)次數(shù)cout << "關(guān)鍵字比較次數(shù):" << compare_count << "\t"<< "關(guān)鍵字移動(dòng)次數(shù):" << remove_count << endl; }//-----快速排序----- int QSort_process = 1; //第n次中間過(guò)程 int QSort_compare_count = 0; //關(guān)鍵字比較次數(shù) int QSort_remove_count = 0; //關(guān)鍵字移動(dòng)次數(shù) void QSort(sqlist& list,int low, int high) {int i, j, pivotkey;i = low;j = high;if (low < high){pivotkey = list.l[low]; //設(shè)置樞軸while (i != j){while (j > i&& list.l[j] >= pivotkey){--j;QSort_compare_count++; //關(guān)鍵字比較次數(shù)+1}if (i < j){list.l[i] = list.l[j]; //交換++i;QSort_remove_count++; //關(guān)鍵字移動(dòng)次數(shù)+1}while (i < j && list.l[i] <= pivotkey){++i;QSort_compare_count++; //關(guān)鍵字比較次數(shù)+1}if (i < j){list.l[j] = list.l[i]; //交換--j;QSort_remove_count++; //關(guān)鍵字移動(dòng)次數(shù)+1}}list.l[i] = pivotkey; //樞軸記錄到位//中間過(guò)程輸出cout << "第" << QSort_process++ << "次中間過(guò)程輸出:" << endl;for (int n = 1; n <= list.length; n++) {cout << list.l[n] << ' ';}cout << endl;//遞歸QSort(list, low, i - 1);QSort(list, i + 1, high);} }//-----二路歸并排序----- int MSort_process = 1; //第n次中間過(guò)程 int MSort_compare_count = 0; //關(guān)鍵字比較次數(shù) int MSort_remove_count = 0; //關(guān)鍵字移動(dòng)次數(shù) void Merge(sqlist &list, int low, int mid, int high) {int len = high - low + 1;int* temp = new int[len];int i = low; int j = mid + 1; //i,j分別為兩個(gè)子序列的下標(biāo)int k = 0; //新合并的數(shù)組的下標(biāo)while (i <= mid && j <= high) {if (list.l[i] <= list.l[j]) {MSort_compare_count++; //關(guān)鍵字比較次數(shù)+1MSort_remove_count++; //關(guān)鍵字移動(dòng)次數(shù)+1temp[k] = list.l[i];i++;k++;}else {MSort_compare_count++; //關(guān)鍵字比較次數(shù)+1MSort_remove_count++; //關(guān)鍵字移動(dòng)次數(shù)+1temp[k] = list.l[j];j++;k++;}}while (i <= mid) {temp[k] = list.l[i];i++;k++;}while (j <= high) {temp[k] = list.l[j];j++;k++;}for (int i = 0; i < len; i++) {list.l[low+i] = temp[i];} }void MSort(sqlist &list,int low,int high) {if (low < high) {int mid = (low + high) / 2;//遞歸MSort(list, low, mid);MSort(list, mid + 1, high);//合并左右序列(子樹)Merge(list, low, mid, high);}//中間過(guò)程輸出cout << "第" << MSort_process++ << "次中間過(guò)程輸出:" << endl;for (int n = 1; n <= list.length; n++) {cout << list.l[n] << ' ';}cout << endl; }int main() {/*-----------------------------------------說(shuō)明:隨機(jī)生成數(shù)組首元素下標(biāo)為 1-----------------------------------------*/// 生成隨機(jī)數(shù)組srand(unsigned(time));sqlist list;list.length = 0;cout << "請(qǐng)輸入生成數(shù)組的長(zhǎng)度:";cin >> list.length; //輸入數(shù)組的長(zhǎng)度cout << "生成的數(shù)組為:";for (int i = 1; i <=list.length; i++){ list.l[i] =(int) rand() % 90 + 10;cout << list.l[i]<<' ';}cout << endl;//-----插入排序-----//InsertSort(list,list.length);//-----選擇排序-----//SelectSort(list);//-----冒泡排序-----//BubbleSort(list);//-----雙向冒泡-----//TwowaybubbleSort(list);//-----快速排序-----//QSort(list, 1, list.length);//輸出關(guān)鍵字比較次數(shù)和關(guān)鍵字移動(dòng)次數(shù)//cout << "關(guān)鍵字比較次數(shù):" << QSort_compare_count << "\t"// << "關(guān)鍵字移動(dòng)次數(shù):" << QSort_remove_count << endl;//-----歸并排序-----//MSort(list, 1, list.length);//輸出關(guān)鍵字比較次數(shù)和關(guān)鍵字移動(dòng)次數(shù)//cout << "關(guān)鍵字比較次數(shù):" << MSort_compare_count << "\t"//<< "關(guān)鍵字移動(dòng)次數(shù):" << MSort_remove_count << endl; }

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。