数组基本算法
来源:互联网 发布:关于网络诈骗ppt 编辑:程序博客网 时间:2024/04/29 07:07
一维数组算法:
1. 数组的遍历算法
#define NUM 10
int main(){
int array[NUM];
for (int i; i < NUM; i ++){
cout << "Please input NO."<<i<<" number of the array."<< endl;
cin >> array[i] ;
cout << endl;
}
cout << endl;
for (int i; i < NUM; i ++){
cout << array[i] << endl ;
}
return 0;
}
2. 求数组最大/最小值算法
#define NUM 10
void main(){
int max = 0;
int min = 0;
int array[NUM];
for (int i = 0; i < NUM; i ++){
cout << "The NO. "<< array[i]<<" number of the array is "<<endl;
cin >> array[i];
cout << endl;
}
for (int i = 0; i < NUM; i ++){
if (array[i] > max){
max = array[i];
}
if (array[i] < min){
min = array[i];
}
}
cout << "the max value of the array is "<<max<<endl;
cout << "the min value of the array is "<<min<<endl;
}
3. 数组排序算法
选择排序算法:
将array[0]与后面每一个数字进行比较,选出最小的存入array[0]; 将array[1] 与后面每个数字进行比较,选出最小的存入array[1]...以此类推.
#define NUM 10
void sort_direct(int array[], int size){
for (int i = 0; i < size - 1; i ++){
for (int j = i + 1; j < size ; j ++){
if (array[j] < array[i]){
t = array[i];
array[i] = array[j];
array[j] = t;
}
}
}
return array[];
}
void main(){
int array[NUM];
int a[NUM];
for (int i = 0; i < NUM; i ++){
cout << "Please input the NO. "<<array[i]<<" number of the array."<< endl;
cin >> array[i];
cout << endl;
}
sort_direct(array, NUM);
}
冒泡排序算法:
每一个元素和它之后的一个元素比较,如果较大,则交换。一趟比较下来,最大的元素沉底。
#define NUM 10
int[] sort_bubble(int array[], int size){
for (int i = 0; i < size - 1; i ++){
for (int j = 0; j < size - 1 - i; j ++){
if (array[j] > array[j + 1]){
int t = array[j];
array[j] = array[j + 1];
array[j + 1] = t;
}
}
}
return array[];
}
int main(){
int array[NUM];
int a[NUM];
for (int i = 0; i < NUM; i ++){
cout << "Please input the NO. "<<array[i]<<" number of the array."<< endl;
cin >> array[i];
cout << endl;
}
sort_bubble(array, NUM);
}
4. 数组元素逆序存放算法
#define NUM 10
void main(){
int array[NUM];
for (int i = 0; i < NUM; i ++){
cout << "Please input the NO. "<<array[i]<<" number of the array."<< endl;
cin >> array[i];
cout << endl;
}
for (int i = 0; i < NUM/2; i ++){t = array[i];
array[i] = array[NUM-i];
array[NUM-i] = t;
}
for (int i = 0; i < NUM; i ++){
cout<<array[i]<<" ";
}
cout << endl;
}
5. 数组的插入算法 (插入已经排好序的数组中)
void main(){
int array[5] = {2,4,6,7,9};
int number;
int i, j;
cout << "Please input the number you want to insert: " << endl;
cin >> number;
for ( i = 0; i < 5; i ++){
if (number < array[i]) break;
}
for ( j = 5; j <= i + 1; j -- ){
array[j] = array[j - 1];
}
array[i] = number;
for (i = 0; i < 11; i ++){
cout << array[i];
}
}
6. 数组的查找算法
折半查找
#define NUM 10
int search(int array[NUM], int number){
int left = 0;
int right = NUM - 1;
int mid;
while (array[left] <= array[right]){
mid = (right + left)/2;
if (number = array[mid]) {
return mid;
break;
}
if (number < array[mid]){
right = mid - 1;
}
if (number > array[mid]){
left = mid + 1;
}
}
}
二维数组算法:
1. 二维数组的遍历算法
void main(){
int a[2][3];
for (int i = 0; i < 2; i ++){
for (int j = 0; j < 3; j ++){
cout << "Please input the number of the array"<< endl;
cin >> a[i][j];
cout << endl;
}
}
for (int i = 0; i < 2; i ++){
for (int j = 0; j < 3; j ++){
cout <<"the numbers in the array are " << endl;
cout << a[i][j] << endl;
}
}
}
2. 求二维数组的最大/最小值算法
int main(){
int a[2][3] = {{3,5,1},{2,4,7}};
int max = 0;
int min = 0;
for (int i = 0; i < 2; i ++){
for (int j = 0; j < 3; j ++){
if (max < a[i][j] ) max = a[i][j];
if (min > a[i][j]) min = a[i][j];
}
}
return max, min;
}
3 转置矩阵
void main(){
int a[2][3] = {{3,5,1},{2,4,7}};
int b[3][2];
for (int i = 0; i < 2; i ++){
for (int j = 0; j < 3; j ++){
b[j][i]=a[i][j]
}
}
for (int i = 0; i < 3; i ++){
for (int j = 0; j < 2; j ++){
cout << b[i][j];
}
}
}
4 方阵对角线元素之和
int main(){
int a[3][3]={{3,5,1},{2,4,7},{1,4,9}};
int sum = 0;
for (int i = 0; i < 3; i ++){
for (int j = 0; j < 3; j ++){
if (i = j)
sum += a[i][i];
}
}
return sum;
}
5 方阵对角线以上/以下元素之和
int main(){
int a[3][3]={{3,5,1},{2,4,7},{1,4,9}};
int sum = 0;
for (int i = 0; i < 3; i ++){
for (int j = 0; j < 3; j ++){
if (i<=j) sum += a[i][j];
}
}
return sum;
}
- 数组基本算法
- 基本算法-翻转数组
- 数组操作,基本排序算法
- C# 数组排序 基本算法
- 数组基本的排序算法
- 赛马网基本算法之--翻转数组
- 赛码网-基本算法-2.翻转数组
- 算法学习---基本数据类型的数组二分查找实现
- 算法4-6:关联数组的基本实现
- java中数组的初始化和基本排序算法
- kmp算法中next[]数组求法的基本思想
- JAVA基础day04 数组学习 排序和查找基本算法
- 数据结构与基本算法的学习笔记-数组-3
- 【学习记录】5.2 数组相关的基本算法
- js数据结构与算法之——数组基本用法
- 基本算法:生成元素数组的所有排列(permute)
- 一些基本算法--归并两个数组,二分查找
- 数组(基本)
- ZOJ Monthly, July 2011【D】找规律
- 瞎买的代码怎么通过360卫士杀毒啊
- 2011/7/26 vs链接mysql心得
- 控制其他软件运行
- android分享到新浪微博,认证+发送微博,神马'40113token_rejected! Oauth Token不合法'错误,都TMD都是浮云,终于一切搞定!
- 数组基本算法
- 据起码科学常识推翻5千年“常识”:没最大自然数
- 比较全面的MQ 相关汇总,
- C/C++中变量的可见性与生命期区别的本质原因
- [原]Oracle Linux EL 5.0 安装Oracle Database 10g笔记 —— 草稿
- pgdac和unidac插入效率究竟差多少?
- Csdn下载频道积分规则2.1
- 图数据挖掘之基本概念
- Java中遭遇NaN