算法备忘录(3)

#include
#include
using namespace std;

//1.3折半查找
int binarySearch(int A[],int x,int L){
int low=0;
int hight=L-1;
while(low<=hight){
int mid=(low+hight)/2;
if (A[mid]==x){return mid;}
else if(A[mid]>x){hight=mid-1;}
else if(A[mid]<x){low=mid+1;}
}
return -1;
}

//1.7棋盘算法
int t=1;
int Board[4][4];

void chessBoard(int tr,int tc,int dr,int dc,int size){
if(size==1) return;
int tile=t++; size/=2;
int* ti=&tile;
//左上角
if (dr<tr+size&&dc<tc+size){
chessBoard(tr,tc,dr,dc,size);
} else
{
Board[tr+size-1][tc+size-1]=tile;
cout<<“tile:”<<tile<<" “<<ti<<endl;
cout<<”----------------------"<<endl;
chessBoard(tr,tc,tr+size-1,tc+size-1,size);
}
//右上角
if (dr<tr+size&&dc>=tc+size){
chessBoard(tr,tc+size,dr,dc,size);
} else
{
Board[tr+size-1][tc+size]=tile;
cout<<“tile:”<<tile<<" “<<ti<<endl;
cout<<”----------------------"<<endl;
chessBoard(tr,tc+size,tr+size-1,tc+size,size);
}
//左下角
if (dr>=tr+size&&dc<tc+size){
chessBoard(tr+size,tc,dr,dc,size);
} else
{
Board[tr+size][tc+size-1]=tile;
cout<<“tile:”<<tile<<" “<<ti<<endl;
cout<<”----------------------"<<endl;
chessBoard(tr+size,tc,tr+size,tc+size-1,size);
}
//右下角
if (dr>=tr+size&&dc>=tc+size){
chessBoard(tr+size,tc+size,dr,dc,size);
} else
{
Board[tr+size][tc+size]=tile;
cout<<“tile:”<<tile<<" “<<ti<<endl;
cout<<”----------------------"<<endl;
chessBoard(tr+size,tc+size,tr+size,tc+size,size);
}
}

//1.8归并排序排序(未)
int* B= (int*)malloc(100* sizeof(int));//辅助数组B,大小为6,等价与 int B[6];

void Merge(int A[],int low,int mid,int hight){
int i,j,k;
for (int k = low; k <=hight; k++) {
B[k]=A[k];
}
for ( i = low,j =mid+1,k=i;i<=mid&&j<=hight;k++) {
if (B[i]<=B[j])
A[k]=B[i++];
else
A[k]=B[j++];
}
while(i<=mid) A[k++]=B[i++];
while (j<=hight) A[k++]=B[j++];
}

void MergeSort(int A[],int low,int hight){
if(low<hight){
int mid=(low+hight)/2;
MergeSort(A,low,mid);
MergeSort(A,mid+1,hight);
Merge(A,low,mid,hight);
}

}

//1.10快速排序
void QuickSort(int A[],int low,int hight){
int temp=A[low];
int i=low; int j=hight;

while(i<j){//每轮进行一次low、hight的交换,直到low=hight
    while (i<j&&A[j]>=temp)--j;
    A[i]=A[j];
    while (i<j&&A[i]<temp) ++i;
    A[j]=A[i];
}
A[i]=temp;

if (low<hight){//因为下面有递归,所以加一个if,判断跳出不进行递归
QuickSort(A,low,i-1);
QuickSort(A,i+1,hight);
}//递归不满足条件要跳出时,判断跳出的循环后不能有递归的代码,再有可递归的代码的话就会循环进入(死循环);

}

//线性时间选择排序
int randomQuickSort(int A[],int low,int hight){
srand((int)time(0));
int key=rand() % (hight-low+1)+ low,t;
int temp=A[key];
//将选中的key与第一个元素交换
t==A[key];
A[key]=A[low];
A[low]=t;

int i=low;
int j=hight;
while (i<j){
    while(i<j&&temp<=A[j]) j--;
    A[i]=A[j];
    while(i<j&&temp>A[i]) i++;
    A[j]=A[i];
}
A[i]=temp;//现在只是排好中轴位置,两边元素的顺序还未排列
return i;

}
int select(int A[],int low,int hight,int k){
if (low==hight){
return A[low];
}
int i=randomQuickSort(A,low,hight);
int j=i-low+1;//左边元素的个数
if (k<=j) return select(A,low,i,k);
else
return select(A,i+1,hight,k-j);
}
void alert2(int A[]){
cout<<&A[2]<<endl;
A[0]=8;
A[1]=9;
A[2]=3;
cout<<&A[2]<<endl;
}
void alert1(int a){//会创建一个临时变量将5赋值给a
a=2;
cout<<&a<<endl;
}
typedef struct LNode
{
int data;
struct LNode *next;
}LNode, *LinkList ;

void update_1(LinkList L1)
{
L1->data = 1;
}

void update_2(LinkList &L2)
{
L2->data = 1;
}

int main() {
LinkList L;
L = (LinkList)malloc(sizeof(LNode));
L->data=2;
cout<data<<endl;
/update_1(L);
cout<data<<endl;
/
update_2(L);
cout<data<<endl;

int A[]={5,4,2,1};
/*int A[]={8,5,1};
alert2(A);
cout<<&A[2]<<endl;*/

/* for (int i = 0; i <= 2; ++i) {
cout<<A[i]<<" “;
}/
//1,折半查找
/int A[7]={3,7,8};
int i=binarySearch(A,7,3);
cout<<i;
/
//2,棋盘算法
chessBoard(0,0,0,1,4);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout<<Board[i][j]<<" ";
}
cout<<endl;
}
//3,归并排序
/int A[]={10,1,2,20,5,30,9};
MergeSort(A,0,6);
cout<<“归并排序:”;
for (int i = 0; i <= 6; ++i) {
cout<<A[i]<<" ";
}
/
//4,快速排序
/
int A[]={5,1,2,8,9,10,11,2,1};
QuickSort(A,0,8);
cout<<“快速排序:”;
for (int i = 0; i <= 8; ++i) {
cout<<A[i]<<” ";
}*/
//5,时间选择排序
/int t=select(A,0,3,3);
cout<<“第k小的元素为:”<<t;
/
return 0;
}
-------------------------------------------------------------------那年考研的时光

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值