实验题目:
1)编写程序输出在顺序表{1,2,3,4,5,6,7,8,9,10}中采用折半查找法查找关键字9的过程。
2)编写快速排序算法,并输出{6,8,7,9,0,1,3,2,4,5}的排序过程。
要求: 请写明:存储结构、基本算法(可以使用程序流程图)、源程序、测试数据和结果、算法的时间复杂度、另外可以提出算法的改进方法。
bash.h (顺序表的基本操作)
//
// Created by win10 on 2022/11/29.
//
#ifndef LINETABLE_CPP_BASE_H
#define LINETABLE_CPP_BASE_H
#include<stdlib.h>
#define MaxSize 10
typedef struct {
int data[MaxSize];
int length;
} SqList;
//初始化
void InitList(SqList *&L) {
L = (SqList *) malloc(sizeof(SqList));
L->length = 0;
}
//创建
void CreateList(SqList *&l, int a[], int n) {
int i;
l = (SqList *)malloc(sizeof(SqList));
for ( i = 0; i < n; i++) {
l->data[i] = a[i];
}
l->length = n;
}
#endif //LINETABLE_CPP_BASE_H
BinSearch.h (二分查找的源码)
//
// Created by win10 on 2022/11/27.
//
#ifndef LINETABLE_CPP_BINSEARCH_H
#define LINETABLE_CPP_BINSEARCH_H
#include <stdio.h>
#include <stdlib.h>
#include "base.h"
int BinSearch(SqList *l, int n, int target)
{
int left = 0, right = l->length - 1,mid;
int time = 1;
while(left <= right){
printf("第%d次查找:\n", time);
printf("查找范围:%d~%d\n", left + 1, right + 1);
mid = (left + right) / 2;
if(l->data[mid] < target){
printf("未找到,目标在此范围的右半边\n");
left = mid + 1;
}else if(l->data[mid] > target){
printf("未找到,目标在此范围的左半边\n");
right = mid - 1;
}else{ //相等
return mid;
}
time++;
}
return -1;
}
#endif //LINETABLE_CPP_BINSEARCH_H
quickSort.h (快排的源码)
//
// Created by win10 on 2022/11/27.
//
#ifndef LINETABLE_CPP_QUICKSORT_H
#define LINETABLE_CPP_QUICKSORT_H
#include <stdio.h>
#include "base.h"
//交换
void swap(SqList *&q,int left,int right) {
int temp;
temp = q->data[left];
q->data[left] = q->data[right];
q->data[right] = temp;
}
//记录排序次数
int time = 1;
void DisplayList(SqList *&q, int n) {
int i;
for (i = 0; i < n; i++)
printf("%d\t", q->data[i]);
printf("\n");
}
//
int partition(SqList *&q, int left, int right) {
int pivot = q->data[left];//选取最左边的为界 ...
while (left < right) {
while (left < right && q->data[right] >= pivot)
right--;
swap(q,left,right);
while (left < right && q->data[left] <= pivot)
left++;
swap(q,left,right);
}
swap(q,left,right);
printf("第%d次排序:\n", time);
time++;
DisplayList(q, 10);
return left;
}
// 核 心
void QSort(SqList *&q, int left, int right) {
if (left < right) {
int pi = partition(q, left, right);//q.data[pi]
QSort(q, left, pi - 1);
QSort(q, pi + 1, right);
}
}
#endif //LINETABLE_CPP_QUICKSORT_H
maind.cpp (二分查找和快排的main函数)
//
// Created by win10 on 2022/11/27.
//
#include "BinSearch.h"
#include "quickSort.h"
#include "base.h"
#define N 10
int main() {
printf("1. 编写程序输出在顺序表{1,2,3,4,5,6,7,8,9,10}中采用折半查找法查找关键字9的过程。\n");
printf("2.编写快速排序算法,并输出{6,8,7,9,0,1,3,2,4,5}的排序过程。\n");
printf("请问你选择的是(1 or 2 or 其他【结束】)\n");
int n = 0;
scanf("%d", &n);
while (n == 1 or n == 2) {
if (n == 1) {
SqList *l;
int array[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
InitList(l);
CreateList(l,array,N);
int target = 9;
int result = BinSearch(l, 10, target);
if (result != -1 )
printf("找到了!数字%d在数组的第%d位。\n", target, result+1);//+1
else
printf("没有要找的元素。\n");
printf("*********折半查找结束**********\n");
} else if (n == 2) {
SqList *q;
int array2[10] = {6, 8, 7, 9, 0, 1, 3, 2, 4, 5};
// int array2[N] = {6, 8, 7, 7, 0, 1, 3, 2, 4, 9};
InitList(q);
CreateList(q,array2,N);
printf("排序前:\n");
DisplayList(q, 10);
QSort(q, 0, 9);
printf("************快速排列结束**************\n");
} else {
printf("结束");
exit(0);
}
scanf("%d",&n);
}
printf("结束");
return 0;
}