数据结构

2022-10-14

----------------

排序

----------------

测试主函数

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #include <time.h>
 4 #include "sort.h"
 5 
 6 
 7 int main()
 8 {
 9     arraylist numlist;
10     ptarraylist pt_arraylist;
11     pt_arraylist = &numlist;
12     while(true){
13         init_data(pt_arraylist);
14         show((char *)"排序前 ", pt_arraylist);
15         
16 //        bubblesort_low(pt_arraylist);
17 //        bubblesort_mid(pt_arraylist);
18 //        bubblesort_mid_opt(pt_arraylist);
19 //        selectsort_asc(pt_arraylist);    
20         selectsort_desc(pt_arraylist);    
21         show((char *)"排序后 ", pt_arraylist);
22     } 
23 
24     
25     return 0;
26 }

 

测试头文件

 1 //sort.h
 2 #define true 1 
 3 #define false 0
 4 #define numsize 20
 5 typedef struct {
 6     int array[numsize];
 7     int len;
 8 }arraylist, *ptarraylist; 
 9 
10 void init_data(ptarraylist pt_arraylist); 
11 void show(char * info, ptarraylist * pt_arraylist); 
12 void bubblesort_low(ptarraylist * pt_arraylist);
13 void bubblesort_mid(ptarraylist * pt_arraylist);
14 void swap(int * first, int * second);
15 
16 #include "sort_function.cpp"

 

冒泡排序思想:

“比较相邻的元素。如果第一个比第二个大,就交换他们两个。 对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。 针对所有的元素重复以上的步骤,除了最后一个。 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

 

 

选择排序思想:

“从头至尾扫描序列,找出最小的一个元素,和第一个元素交换,接着从剩下的元素中继续这种选择和交换方式,最终得到一个有序序列。

 
直接插入排序思想:
“直接插入排序的基本操作是将一个记录插到已排队好的有序表中,从而得到一个新的,记录增1的有序表。(第一个有序表可认为是第一个元素)“
 

 

 

希尔排序思想:
 
堆排序思想:
 
归并排序思想:
 
快速排序思想;
 

 

 

 
 
 

《数据结构.doc》

下载本文的Word格式文档,以方便收藏与打印。