Sort comprehensive Report
Date:2019年11月30日
One. Experimental purpose
-
Familiar with the basic operations of the sort.
-
Master the operation of various internal sorting.
-
Deepen the understanding of the sort, and to develop the programming ability of solving practical problems gradually.
Two. Experimental environment
A computer with Visual Studio.
This experiment has 4 class hours in all.
Three. Experimental content
A series of strings are stored in a two-dimensional array. Try to sort them with some sorting algorithms (at least two algorithms, such as insert sorting, bubble sorting, quick sorting, and heap sorting). You should sort them to dictionary order finally.
For example: two-dimensional array is :
char s[][20]={“while”,”if”,“else”,”do”,“for”,”switch”,“case”};
Four. Important data structures
Importantdata structures
int count(char s[][20]); //计数函数
void Cout(char s[][20], int n); //输出函数
void Bubbling_sort(char s[][20],int n); //冒泡
void Insert_sort(char s[][20], int n); //插入
void Quick_sort(char s[][20], int r); //快排
void Set_sort(char s[][20], int n); //选择
void Shell_sort(char s[][20], int n); //希尔
void Base_sort(char s[][20], int n); //基数
void Merge_sort(char s[][20], int n); //归并
Some other function
1):Swap
template<typename T> //模板
void swap(T &a, T&b)
{
T t;
t = a;
a = b;
b = t;
}
2):Printf
void Cout(char s[][20], int n)
{
for (int i = 0; i < n; i++)
cout << s[i] << " ";
cout << endl;
}
3):Count
int i = 0;
int count(char s[][20])
{
i++;
if (s[i][20]&&s[i][20]!='\0')
return count(s);
else
return i+1;
}
Five.Implementation analysis
Program Execution Structure Diagram
The main function
int main()
{
char s[100][20] = { "while","if","else","do","for","switch","case" ,"a"}; //不确定s的长度就会造成返回说栈被破坏
int n = count(s);
Bubbling_sort(s,n);
Quick_sort(s, n);
Insert_sort(s, n);
Set_sort(s, n);
Shell_sort(s, n);
Base_sort(s, n);
Merge_sort(s, n);
}
Bubbling_sort
void Cout(char s[][20], int n);
void Bubbling_sort(char s[][20],int n) //每次往前蹦跶一下
{
for (int i = 0; i < n; i++)
for (int j = 0; j < 20; j++)
if (strcmp(s[i], s[j]) < 0) //这里讲下,strcmp是按照字符串的字母逐个比较,记住是逐个,一旦遇到比他小的立马返回负数
swap(s[i], s[j]); //所以这里用来字典排序就很快乐,后面都是用这个方式判断。
Cout(s, n );
}
Set_sort
void Set_sort(char s[][20], int n)
{
char Max[20];
int j;
for (int i = 0; i < n; i++)
{
strcpy(Max, s[i]);
for (j = 0; j <i ; j++) //每次都找最大的,然后交换过去,看不懂的话建议先去看下数字类型的,这里比数字类型多了一步
{
if (strcmp(Max, s[j]) <0)
{
strcpy(Max, s[j]);
swap(s[i], Max);
strcpy(s[j], Max);
}
}
}
Cout(s, n);
}
Shell_sort
void Shell_sort(char s[][20], int n)
{
for (int step = n; step != 0; step =step/ 2)
{
for (int i =0; i < n; i++)
{
for (int j = 0; j < n; j += step)
{
if (strcmp(s[i], s[j]) < 0) //说实话真的只是多了个预处理,你选择冒泡或者插入都可以实现
swap(s[i], s[j]);
}
}
}
Cout(s, n);
}
Quick_sort
void Quick_sort1(char s[][20], int left, int right);
void Cout(char s[][20], int n);
void Quick_sort(char s[][20], int n) //多写个函数好看点不会混乱,这里是入口
{
Quick_sort1(s, 0, n);
Cout(s, n);
}
void Quick_sort1(char s[][20], int left,int right)
{
if (left >= right)
return;
int i, j;
char t[20];
strcpy(t, s[left]);
i = left;
j = right;
while (i != j) {
while ( i < j&&(strcmp(s[j], t) <= 0) ) //找到比t大的
j--;
while (i < j&&(strcmp(s[j], t) > 0)) //找到比t小的
i++;
if (i < j)
swap(s[i], s[j]); //互换
}
strcpy(s[left], s[i]); //把找到位置的下表点的值放到第一个
strcpy(s[i], t); //把中间值t放到中间,这样左边就是比他小的,右边比他大
Quick_sort1(s,left, i-1 ); //递归处理
Quick_sort1(s,i+1 , right);
}
Merge_sort
void Cout(char s[][20], int n);
void Msort(char s[][20], int l, int r); //拆分函数
void Msort_merge(char s[][20], int l, int center, int r, int n); //归并函数
void Merge_sort(char s[][20], int n) //投机函数,因为我后面发现第一个数据也就是s[0]为空,但是却到了s[9],这就令人很奇怪,有机会就找出原因
{
Msort(s, 0, n);
for (int i = 0; i < n; i++)
strcpy(s[i] , s[i + 1]);
Cout(s, n);
}
void Msort(char s[][20], int l,int r)
{
if (l < r) //要保证左边小于右边。尾递归
{
int center = (l + r) / 2;
Msort(s, l, center);
Msort(s, center + 1, r);
Msort_merge(s, l, center, r, r - l + 1);//归并
}
}
void Msort_merge(char s[][20], int left, int center, int right, int n)
{
char t[100][20]; //中转,可以new和delete懒得写,直接赋值
int l = left;
int r = center+1;
int k = 0;
while (l <= center&&r <= right)
{
if (strcmp(s[l],s[r])<0) //如果左边的值小于右边,就把左边的值给t ,else同理
strcpy(t[k++], s[l++]);
else
strcpy(t[k++], s[r++]);
}
if (l == center + 1) //剩下的值要倒入t中
{
while (r <= right)
strcpy(t[k++], s[r++]);
}
else
while (l <= center)
strcpy(t[k++], s[l++]);
for (int i = left, k = 0; i <= right; i++, k++) //把t中排好序的还给s,进行下一轮
strcpy(s[i], t[k]);
}
Six.Debugging problem analysis
(1)Problem:
When I initialized the two-dimensional array, I found that the stack was damaged because the size of one-dimensional array was not specified at the beginning, and the code could not run.
Solvetion
The range is specified by specifying the size of the array. Otherwise, the array will be out of bounds, causing the stack to be destroyed. At the same time, a count function is added to get the data length of each time
(2)Problem:
When merging and sorting, the same set of data is less compared, resulting in the error of the last sorting, and the last merging does not appear sorting, but a dead cycle
Solvetion
After finding the error, the equal sign is added, and then the subscript is found to be wrong. After correction, it can operate normally and stably
Seven.Summary
Sorting is relatively simple, and the previous sorting functions are basically error free, but the following details need to be paid attention to, such as: whether to specify the array size, whether to select and insert sorting needs to be paid attention to whether the following table is consistent, and whether to assign values back is not required. It is recommended to single step debugging or set the output function to judge. In quick sorting and merging sorting, attention should be paid to the end of tail recursion Point, it is recommended to write more functions to distinguish, and pay attention to the subscript after exchange. Merge can be new and then delete, so it will not occupy too much space. In cardinality sorting, attention should be paid to the number of buckets.
Eight.Crew Division
Membername | Work done | Completionsituation |
---|---|---|
All | Done |