網站首頁 編程語言 正文
簡單的冒牌排序只能對一中數組的類型進行排序,現在我們用冒泡排序為基礎來改造出一個可以對任意數組排序的排序函數!
后面附有實現的源碼!
首先我們以qsort函數為例慢慢分析,然后確定我們的排序函數如何增強,第一步我們從它的參數下手,它一共4個參數。
1.第一個參數類型是void*,qsort函數可以用來對任意類型的數組排序,用void
*型指針可以直接接收各種類型的數組。
2.int?。睿酰硎菙到M的個數,
3.int?。鳎椋洌簦枋侵羔樀牟介L,后面我會進行解釋.
4.int?。ǎǎ悖铮睿螅簟。觯铮椋洌。澹?,const void*?。澹玻﹤鬟f一個函數指針,比較函數,為什么是比較函數呢,我們要編寫一個可以對任意類型的數組進行排序的函數,我們作為設計者,本身是不知道用戶要用來排序那種類型的數組,所以讓用戶來傳入一個他想排序數組的類型的比較函數,就可以按照用戶的意愿來排序數組。
一、補充一下關于void*指針的知識,易于我們對下列函數實現的理解
void 類型的指針特點
void* 是一種無類型的指針,無具體類型的指針。
void* 的指針變量可以存放任意類型的地址 void* 的指針不能直接進行解引用操作。
void* 的指針不能直接進行+、-整數。
二、實現排序函數中的核心,比較函數
下面是qsort函數對比較函數的要求:
關于qsort函數,他要求比較函數的傳入參數中的函數指針的比較函數的返回要求,返回值是一個int型,當e1小于e2時返回一個小于0的數,當e1大于e2時返回一個大于0的數,兩者相等返回0;
我們也可以通過這里來改變,排序的升降序?。?!
按照要求我們以簡單的整形為例設計一個比較整形大小的函數:
1.比較int型大小的函數
2.比較float型大小的函數
3.比較結構題中字符串大小的函數
三、實現排序函數
這里我們是以冒泡排序為基礎,來實現對各種數組的排序
1.冒泡排序的原型
2.對冒泡排序的核心進行升級
我們設計的排序中的核心便是使用用戶傳入的比較函數來判斷兩個數組元素的大小.這里就用到為啥要傳入排序數組的步長了。
我們并不知道用戶要排序的數組類型,所以我們并不能以int*或float*來直接接收數組元素,單數我們在知道數組元素的類型的步長之后,通過j*whdth來移動到下一個元素,再有用戶傳入的比較函數來比較大小。這點非常的巧妙?。?!
四、轉換函數的實現
同樣我們不知道,排序數組的類型,但是我們通過一個字節一個字節的轉換同樣可以達到交換元素值的效果。
下面是附帶的源碼
#include<stdio.h> #include<string.h> #include<stdlib.h> //實現q_sort函數用冒泡排序 //只能排序整型 //void bubbling_sort(int *arr, int se) //{ // int i = 0; // int j = 0; // for (i = 0; i < se - 1; i++) // { // for (j = 0; j < se - 1 - i; j++) // { // //升序排列 // if (arr[j] > arr[j + 1]) // { // int tem = arr[j]; // arr[j] = arr[j + 1]; // arr[j + 1] = tem; // } // } // } //} struct Stu { char name[20]; int age; float score; }; //打印排序后結果 void print_arr_int(int* arr, int se) { int i = 0; for (i = 0; i < se ; i++) { printf("%d ", *(arr + i)); } } void print_arr_float(float* arr, int se) { int i = 0; for (i = 0; i < se; i++) { printf("%.2f ", *(arr + i)); } } void printf_stu(struct Stu arr[], int se) { int i = 0; for (i = 0; i < se; i++) { printf("%s %d %.2f\n", arr[i].name, arr[i].age, arr[i].score); } printf("\n"); } void swap(char *num1, char *num2, int width) { int i = 0; for (i = 0; i < width; i++) { char tem = *num1; *num1 = *num2; *num2 = tem; num1++; num2++; } } void my_sort(void *arr, int se, int width, int (*comp)(const void* e1, const void* e2)) { int i = 0; int j = 0; for (i = 0; i < se - 1; i++) { for (j = 0; j < se - 1 - i; j++) { //升序排列 if (comp((char*)arr + j * width, (char*)arr + (j + 1) * width) > 0) { //交換數組元素 swap((char*)arr + j * width, (char*)arr + (j + 1) * width, width); } } } } int comp_int(const void* e1,const void* e2) { return (*(int*)e1)-(*(int*)e2); } int comp_float(const void* e1, const void* e2) { if ((*(float*)e1) - (*(float*)e2) > 0) { return 1; } else if ((*(float*)e1) - (*(float*)e2) < 0) { return -1; } else { return 0; } } int comp_stu_name(const void* e1, const void* e2) { return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name); } int main() { int arr[] = {9,8,7,6,5,4,3,2,1,0}; //float arr[] = { 9.1f, 5.1f, 45.0f, 1.2f, 11.3f }; //struct Stu arr[] = { {"zhangsan",18,99.2f},{"lisi",28,78.2f},{"wangwu",12,60.5f}}; int se = sizeof(arr) / sizeof(arr[0]); my_sort(arr, se, sizeof(arr[0]), comp_int); //my_sort(arr, se, sizeof(arr[0]), comp_float); //my_sort(arr, se, sizeof(arr[0]), comp_stu_name); print_arr_int(arr,se); //print_arr_float(arr,se); //printf_stu(arr, se); return 0; }
總結
原文鏈接:https://blog.csdn.net/qq_63512251/article/details/122568397
相關推薦
- 2022-01-16 對npm模塊進行調試和測試——npm link
- 2022-02-12 uni-app 自定義導航欄 圖片按鈕
- 2022-12-25 pycharm導入第三方庫的兩種方法(永不報錯)_python
- 2022-08-26 Python中True(真)和False(假)判斷詳解_python
- 2023-08-15 :prop父組件給子組件傳遞函數 子組件接收 并default子組件自己的方法 問題
- 2023-01-30 django中只使用ModleForm的表單驗證_python
- 2022-04-22 數組去重并找到所有重復的項的索引位置
- 2023-05-07 Python?Matplotlib中使用plt.savefig存儲圖片的方法舉例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支