第七、八章 排序和查找的实现
查找:
要求:
编程实现两种查找方法:折半查找和二叉排序树;
若查找成功,返回元素在有序数组中的位置和查找次数;
若查找失败,返回出错标志和查找次数。
折半查找:
#include <bits/stdc++.h>
using namespace std;
void binarySearch(int a[8], int n, int key)
{
int low = 0;
int high = n - 1;
int times = 0;
int locate;
int midval;
int mid;
while (low <= high)
{
mid = (low + high) / 2;
midval= a[mid];
if (midval < key)
low = mid + 1;
else if (midval > key)
high = mid - 1;
times++;
if (midval == key)
break;
}
locate = mid + 1;
if (midval==key)
{
cout<< "查找成功,查找了"<<times<< "次"<<endl;
cout<< "元素所在的位置为"<<locate<<endl;
}
else
{
cout<< "查找失败,查找了"<<times<< "次"<<endl;
}
}
int main()
{
int i, val;
int a[8] = { 21,35,79,164,238,756,12140,452367 };
cout<< "数组的值如下:";
for (i = 0; i < 8; i++)
cout<<a[i]<< " ";
cout<<endl;
cout<< "请输入需要查找的元素:"<<endl;
cin>>val;
binarySearch(a, 8, val);
return 0;
}
二叉排序树:
#include <bits/stdc++.h>
using namespace std;
#define QueueMaxSize 16 //定义队列数组长度
#define StackMaxSize 8 //定义栈数组长度
typedef int ElemType;
struct BTreeNode
{
ElemType data;
struct BTreeNode* left;
struct BTreeNode* right;
};
int times=1;
//查找等于给定值x的元素,成功返回该结点值域的地址,否则返回NULL
ElemType* Find(struct BTreeNode* BST, ElemType x)
{
while (BST != NULL)
{
if (x == BST->data) //若结点值等于x则返回结点值域的地址
return &(BST->data);
else if (x < BST->data)
BST = BST->left;
else
BST = BST->right;
times++;
}
return NULL;
}
//非递归方式
void Insert(struct BTreeNode** BST, ElemType x)
{
struct BTreeNode* p;
struct BTreeNode* t = *BST, *parent = NULL;
while (t != NULL) //为插入新元素寻找插入位置
{
parent = t;
if (x < t->data)
t = t->left;
else
t = t->right;
}//循环之后parent存储的是待插入位置的双亲结点
p = (struct BTreeNode*)malloc(sizeof(struct BTreeNode));
p->data = x;
p->left = p->right = NULL;
if (parent == NULL) //若树为空,作为根结点插入
*BST = p;
else if (x < parent->data) //链接到左指针域
parent->left = p;
else
parent->right = p; //链接到右指针域
}
//二叉搜索树中可以直接用到二叉树中部分的操作,这里可以用到二叉树的输出、中序遍历和清除函数
//这里只在需要的地方将其元素类型换为int,函数名后加上_int后缀,用来区分
//输出二叉树,可在前序遍历的基础上修改。采用广义表格式,元素类型为int
void CreateBSTree(struct BTreeNode** BST, ElemType a[], int n)
{
int i;
*BST = NULL;
for (i = 0; i < n; i++)
Insert(BST, a[i]);
}
void Inorder_int(struct BTreeNode* BT)//中序遍历,元素类型为int
{
if (BT != NULL)
{
Inorder_int(BT->left);
cout<< BT->data << ",";
Inorder_int(BT->right);
}
}
int main()//其中用到二叉树操作的函数都基本没变,只是元素类型换为int
{
int x, *px,i;
ElemType a[8] = {41,78,96,235,436,875,966,1025};
struct BTreeNode* bst = NULL;
CreateBSTree(&bst,a,8); //利用数组a建立一棵树根指针为bst的二叉搜索树
cout<< "中序遍历:"<<endl;//即有小到大排序
Inorder_int(bst);
cout<<endl;
cout<< "请输入要查找的元素值:";
cin>>x;
if (px = Find(bst, x))
{
for (i = 0;i<8;i++)
{
if (*px == a[i])
break;
}
cout<<"查找成功!该元素位于不减序列的第:"<<i+1<< "个" <<endl;
cout<<"该次查询用了"<<times<< "次" <<endl;
}
else
{
cout<<"查找失败"<<endl;
cout<<"该次查询用了"<<times<< "次" <<endl;
}
return 0;
}
排序:
简单选择排序:
#include <bits/stdc++.h>
using namespace std;
void select(int a[],int length)
{
//length为数组的长度
int i,j;
int min;//记录最小值的位置
for(i = 0; i < length - 1; i++)
{
min = i;
//选择最小的值
for(j = i + 1; j < length; j++)
{
if(a[j] < a[min]) //更新最小值的坐标
min = j;
}
if(min != i)
{
int temp;
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
}
int main()
{
int a[8]={40,58,12,33,90,20,80,65};
cout<< "原序列为:"<<endl;
for (int i=0; i<8; i++)
cout<< a[i]<< " ";
cout<<endl;
select(a,8);
cout<< "简单选择排序后的数列为:"<<endl;
for(int i=0; i<8; i++)
cout<<a[i]<< " ";
return 0;
}
冒泡排序:
#include <bits/stdc++.h>
using namespace std;
void BubbleSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int s[10] = {8,1,9,7,2,4,5,6,10,3};
cout<<"初始序列:"<<endl;
for (int i=0; i<10; i++)
{
cout<<s[i]<< " ";
}
cout<<endl;
BubbleSort(s,10);
cout<<"冒泡排序结果:"<<endl;
for (int i=0; i<10; i++)
{
cout<<s[i]<< " ";
}
return 0;
}