常用算法代码16个

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
else {
move(n-1, a, c, b);
printf("从%c移动到%c\n",a,c);
move(n-1, b, a, c);
}
}
int main() {
int n;
char a = 'A', b = 'B', c = 'C';
while(scanf("%d",&n)!=EOF) {
move(n,a,b,c);
const int MAX = 100;
int main() {
int n, num[MAX];
printf("Pleace input the number:");
scanf("%d",&n);
for(int i=0;i<n;i++) {
num[i] = 0;
}
grayCode(num,n);
printf("\n");
for(int i = 0; i<n1; i++) {
b[i] = a[start + i];
}
for(int i = 0; i<n2; i++) {
c[i] = a[mid + 1 + i];
}
int i = 0, j = 0;
int k = start;
while(i < n1 && j < n2) {
grayCode_add(num,n);
return 0;
}
----
void print(int num[],int n) {
for(int i=0; i<n; i++) {
printf("%d ",num[i]);
}
printf("\n");
}
void grayCode(int num[],int n) {
elemType p = a[l];
int s = l;
for(int i=l+1; i<r; i++) {
if(a[i]<p) {
s++;
swap(a[s],a[i]);
}
}
swap(a[l],a[s]);
return s;
}
elemType quickSelect(elemType a[],int l,int r,int k) {
int i, j;
elemType p;
if(l > r)
return ;
p = a[l];
i = l;
j = r;
while(i!=j) {
//一定要先找右边
while(a[j] >= p && i<j)
j--;
while(a[i] <= p && i<j)
i++;
if(i<j)
swap(a[i],a[j]);
p = a[l];
i = l;
j = r;
while(i!=j) {
while(a[j] >= p && i<j)
j--;
while(a[i] <= p && i<j)
i++;
if(i<j)
swap(a[i],a[j]);
}
swap(a[l], a[i]);
return i;
}
int lomutoPartition(elemType a[],int l,int r) {
}
swap(a[l], a[i]);
quickSort(a, l, i-1);
quickSort(a, i+1, r);
}
6.
#include<stdio.h>
#include"QuickSelect.h"
#include"QuickSelect_non-recursive.h"
const int MAX = 9;
typedef int elemType;
int main() {
elemType a[MAX];
printf("input a[]:");
for(int i = 0; i < MAX; i++) {
scanf("%d",&a[i]);
}
quickSort(a, 0, MAX - 1);
printf("output a[]:");
typedef int elemType;
int main() {
elemType A[MAX+1];
int k;
printf("input:");
for(int i=0; i<MAX; i++) { //输入无序数组
scanf("%d",&A[i]);
}
printf("input K:") ;
scanf("%d",&k); //求k小的数
printf("总移动%d步\n",hanoi(n));
}
return 0;
}
2.
#include<stdio.h>
int jie(int n) {
if(n==0||n==1) return 1;
else return jie(n-1)*n;
}
int main() {
int n;
while(scanf("%d",&n)!=EOF)
}
int main() {
int A[MAX] = {1,8,7,9,5,4,3,6,2,0};
printf("分治算法求最最大元素:%d\n",FiMax(A));
return 0;
}
5.
#include<stdio.h>
#include"QuickSort.h"
const int MAX = 10;
print(num,n);
}
num[n-1] = !num[n-1];
print(num,n);
}
//增0/1
void grayCode_add(int num[],int n) {
int i, j, t;
for(i = 0; i < (1<<n);ቤተ መጻሕፍቲ ባይዱi++) {
if(b[i] < c[j])
a[k++] = b[i++];
else a[k++] = c[j++];
}
while(i<n1) {
a[k++] = b[i++];
}
while(j<n2) {
a[k++] = c[j++];
}
}
void Mergesort(int A[], int start, int end) {
a_end = a_start + i;
b_end = a_end + i;
k = 0;
while(a_start < a_end && b_start <b_end) {
if(A[a_start] < A[b_start])
tmp[k++] = A[a_start++];
else
tmp[k++] = A[b_start++];
for(int i=0; i<MAX;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
------
typedef int elemType;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void quickSort(elemType a[], int l, int r) {
emelType p = a[l];
int s = l;
for(int i = l+1; i<r; i++) {
if(p>a[i]) {
s++;
swap(a[s],a[i]);
}
}
swap(a[s],a[l]);
return s;
}
emelType quickSelect_nonRec(emelType a[],int l,int r,int k) {
int s = lomutoPartition(a,l,r);
if(s == l+k-1) {
return a[s];
} else if(s > l+k-1) {
quickSelect(a,l,s,k);
} else {
quickSelect(a,s+1,r,k-(s+1-l));
}
}
----非递归
b[i] = a[i];
c[i] = a[5+i];
}
int M_b = b[0], M_c = c[5];
for(int i = 1; i<5;i++) {
if(M_b < b[i]) M_b = b[i];
if(M_c < c[i]) M_c = c[i];
}
return M_b>M_c?M_b:M_c;
typedef int emelType;
const int infinite = 0x7fffffff;
//
//void swap(emelType &a,emelType &b) {
//emelType tmp = a;
//a = b;
//b = tmp;
//}
int partition(emelType a[],int l,int r) {
a[r] = infinite;
while(l<=r) {
int j = partition(a,l,r);
if(j < k-1) {
l=j+1;
} else if(j>k-1) {
r = j-1;
} else {
return a[k-1];
}
}
}
7.
#include<stdio.h>
#include"GrayCode.h"
printf("recursive output: %d\n",quickSelect(A,0,MAX,k)); //递归输出第k小数
printf("ono-recursive output: %d\n",quickSelect_nonRec(A,0,MAX,k));
return 0;
}
-----递归
typedef int elemType;
void swap(elemType &a, elemType &b) {
elemType tmp = a;
a = b;
b = tmp;
}
int lomutoPartition1(elemType a[],int l,int r) {
int i, j;
elemType p;
while(a_start <a_end)
A[--b_start] = A[--a_end];
while(k > 0)
A[--b_start] = tmp[--k];
}
}
}
}
int main() {
int a[MAX] = {1,8,7,9,5,4,3,6,2,0};
Mergesort(a, 0, 9);
for(int i = 0; i < MAX; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
4.
#include<stdio.h>
const int MAX = 10;
int FiMax(int a[]) {
int b[MAX], c[MAX];
for(int i=0; i<5; i++) {
printf("合并算法递归方法:");
for(int i = 0; i < MAX; i++)
printf("%d ",a[i]);
printf("\n");
int b[MAX] = {1,8,7,9,5,4,3,6,2,0};
Mergesort2(b, 10);
printf("合并算法非递归方法:");
1.
#include<stdio.h>
int hanoi(int n) {
if(n==1) return 1;
else return hanoi(n-1)*2 + 1;
}
void move(int n, char a, char b, char c) {
if(n==1) printf("从%c移动到%c\n",a,c);
int i, a_start, a_end, b_start, b_end, k;
int tmp[MAX];
for(i = 1; i<n; i=i*2) {
for(a_start = 0; a_start < n - i; a_start = b_end) {
b_start = a_start + i;
printf("%d! = %d\n",n,jie(n));
return 0;
}
3.
#include<stdio.h>
const int MAX = 10;
void Merge(int a[], int start, int mid, int end) {
int b[MAX], c[MAX];
int n1 = mid - start + 1, n2 = end - mid;
print(num,n);
for(int i=0; i<(1<<(n-1))-1; i++) {
num[n-1]=!num[n-1];
print(num,n);
for(int j=n-1; j>0; j--) {
if(num[j]==1) {
num[j-1] = !num[j-1];
break;
}
}
int mid;
相关文档
最新文档