数组中元素旋转移位的三种实现方法 --By LXW
来源:互联网 发布:织梦网站数据采集器 编辑:程序博客网 时间:2024/06/05 05:17
本文主要陈述实现数组中元素旋转移位(以左移为例)的三种方法!其中第一种方法和第三种方法的时间复杂度为O(n),空间复杂度为1。第二种方法方法的时间复杂度为O(n),空间复杂度为i。【其中i为移动的位数,n为总位数】具体程序如下:
@1:
//【编程珠玑第二版P13上 移位的杂技表演法时间复杂度为O(n),空间复杂度为1】这种方法,笔者感觉非常好!笔者将针对这种方法专门写一篇BLOG!#include <iostream>using namespace std;int gcd(int a, int b){ if(a * b == 0){ return 0; } while(a != b){ if(a > b) a -= b; else b -= a; } return a; }void rotate(int * x, int n, int rotdist){ //时间复杂度为O(n),空间复杂度为1【t = x[i]】,效率高 int i, l = gcd(rotdist, n), k, t, j; for(i = 0; i < l; ++ i){ t = x[i]; j = i; while(1){ k = j + rotdist; if(k >= n) k -= n; if(k == i) break; x[j] = x[k]; j = k; } x[j] = t; //t不能换成是x[k]或者x[i],因为x[i]已经被赋值为新的内容了!这也正是引入变量t的原因 }}int main(){ int array[] = {1, 3, 6, 2, 29, 3, 4, 5, 17}; int length = sizeof(array)/sizeof(array[0]); int j; for(j = 0; j < length; ++ j){ printf("%d\t", array[j]); } printf("\n"); rotate(array, length, 3); //间隔为3 for(j = 0; j < length; ++ j){ printf("%d\t", array[j]); } printf("\n"); return 0;}
@2:
//【移位的普通方法,时间复杂度为O(n),空间复杂度为i】#include <stdio.h>#include <stdlib.h>void rotate(int * x, int n, int i){ //此算法每个元素只移动一次,所以时间复杂度为O(n),空间复杂度为i【数组arrtem】 int j, t, k, temp, temp1, *arrtem; arrtem = (int *)malloc(i * sizeof(int)); for(j = 0; j < i; ++ j){ t= x[j]; k = 0; temp1 = j; while(1){ temp = temp1 ; //temp1可换为k * i % n。此处写为temp1可能让人不太容易懂,但有效的避免了重复计算。 ++k; temp1 = (k * i + j)% n ; if(temp1 < temp){ break; }else{ x[temp] = x[temp1]; } } arrtem[j] = t; } for(j = 0; j < i; ++ j){ x[n - i + j] = arrtem[j]; } free(arrtem);}void main(){ int array[] = {1, 3, 6, 2, 29, 13, 5}; int length = sizeof(array)/sizeof(array[0]); int j; for(j = 0; j < length; ++ j){ printf("%d\t", array[j]); } printf("\n"); rotate(array, length, 4); //间隔为4 for(j = 0; j < length; ++ j){ printf("%d\t", array[j]); } printf("\n");}
@3:
//【编程珠玑第二版P13下手摇法实现移位:三次调用reverse函数】此算法时间复杂度为O(n),空间复杂度为1#include <iostream>using namespace std;void reverse(int * array, int beg, int end){ int front = beg; int behind = end; int temp; while(front < behind){ temp = array[front]; array[front] = array[behind]; array[behind] = temp; ++ front; -- behind; }}int main(){ int arr[] = {1, 29, 38, 2, 8, 12}; int i = 2; int n = sizeof(arr)/sizeof(arr[0]); int j; for(j = 0; j < n; ++ j){ cout<<arr[j]<<'\t'; } cout<<endl; reverse(arr, 0, i - 1); reverse(arr, i, n - 1); reverse(arr, 0, n - 1); for(j = 0; j < n; ++ j){ cout<<arr[j]<<'\t'; } cout<<endl; return 0;}
如果您对本博文有什么意见,欢迎您与我联系!【我的邮箱】:lxw0109@gmail.com
- 数组中元素旋转移位的三种实现方法 --By LXW
- 比较数组移位问题的三种方法
- 访问数组元素的三种方法
- 旋转数组中元素的查找
- 找出旋转数组中最小的元素
- 找出词典中的所有的变位词 --By LXW
- Matlab中数组元素引用——三种方法
- Matlab中数组元素引用——三种方法
- Matlab中数组元素引用——三种方法
- Matlab中数组元素引用——三种方法
- 旋转数组中查找元素
- 程序员面试题目总结--数组(三)【旋转数组的最小数字、旋转数组中查找指定数、两个排序数组所有元素中间值、数组中重复次数最多的数、数组中出现次数超过一半的数】
- C语言数组元素的循环移位
- js实现图片旋转的三种方法
- MATLAB数组元素引用的三种方法
- java数组元素倒序的三种方法
- 旋转数组的最小元素
- 旋转数组的最小元素
- 无限制免费版,完美您的WEB应用--PAZU在线打印
- OpenGL函数思考-glMatrixMode
- cc++ 常见试题(1)
- 增加系统调用层次(比如使得系统调用可以传递参数)
- eval解析JSON中的注意点
- 数组中元素旋转移位的三种实现方法 --By LXW
- Oracle Instant Client与Oracle full client
- Struts2的一些常量和含义的应用
- JAVA算法字符串统计
- 写现在女孩的要求
- Solaris重启bfagent服务
- PDF在线编辑器的实现
- js中innerHTML,innerText,outerHTML的用法与区别
- 关于PB调用写有事务开始的存储过程出错的原因