数据结构与算法之有序数组(2)——in dart
本文比第一篇,采用了类实现。增加了运算符重载等功能。本来有序数组是不能修改某个位置的值的,因为这样会打破数组的有序性;但为了演示,保留了修改的方法,但为此增加了排序。
import 'dart:math' show Random; final _rnd = Random();
final _seed = 100; class OrderedArray {
List<int> _array;
int _realLength; OrderedArray(int capacity) {
_array = List<int>(capacity);
_realLength = 0;
} int get capacity => _array.length;
int get length => _realLength; void fill(double percent) {
for (var i = 0; i < capacity * percent; i++) {
insert(_rnd.nextInt(_seed));
}
} bool insert(int v) {
if (_realLength == capacity) {
return false;
} else {
if (_realLength == 0) {
_array[0] = v;
} else {
int i;
for (i = 0; i < _realLength; i++) {
if (_array[i] >= v) break;
}
for (var j = _realLength; j > i; j--) {
_array[j] = _array[j - 1];
}
_array[i] = v;
} _realLength++;
return true;
}
} int find(int key) {
if (_realLength == 0) {
return -1;
} if (key < _array[0] || key > _array[_realLength - 1]) {
return -1;
} int lower = 0, upper = _realLength - 1, mid;
while (lower <= upper) {
mid = (lower + upper) ~/ 2;
if (_array[mid] == key) {
return mid;
} else if (_array[mid] > key) {
upper = mid - 1;
} else {
lower = mid + 1;
}
}
return -1;
} // Attention! after modifed the array is maybe not ordered ever.
bool modify(int pos, int newValue) {
if (pos < 0 || pos > _realLength - 1) {
return false;
} else {
_array[pos] = newValue;
// sort(0, _realLength - 1);
return true;
}
} bool delete(int key) {
var pos = find(key);
if (pos < 0) {
return false;
} else {
for (var i = pos; i < _realLength - 1; i++) {
_array[i] = _array[i + 1];
}
_realLength--;
return true;
}
} int operator [](int pos) {
if (pos < 0 || pos > _realLength - 1) {
return null;
} else {
return _array[pos];
}
} // the below is equal to modify, but don't have return type;
void operator []=(int pos, int newValue) {
if (pos >= 0 && pos < _realLength) {
_array[pos] = newValue;
// sort(0, _realLength - 1);
}
} void sort(int start, int end) {
if (start >= end) return;
var pl = start, pr = end, key = _array[pl];
while (pl < pr) {
while (_array[pr] >= key && pr > pl) pr--;
if (pr > pl) {
_array[pl] = _array[pr];
pl++;
}
while (_array[pl] <= key && pl < pr) pl++;
if (pl < pr) {
_array[pr] = _array[pl];
pr--;
}
}
_array[pl] = key; sort(start, pl - 1);
sort(pl + 1, end);
} void display() {
var sb = StringBuffer();
sb.write('[');
if (_realLength > 0) {
for (var i = 0; i < _realLength - 1; i++) {
sb.write('${_array[i]}, ');
}
sb.write('${_array[_realLength - 1]}');
}
sb.write(']');
print(sb.toString());
}
} void main() {
var arr = OrderedArray(100);
arr.fill(0.2);
arr.display(); var key = _rnd.nextInt(_seed);
if (arr.insert(key)) {
print('insert \'$key\' successed.');
arr.display();
} else {
print('insert \'$key\' failed.\n');
} key = _rnd.nextInt(_seed);
var pos = arr.find(key);
if (pos >= 0) {
print('found the key: $key at $pos\n');
} else {
print('can not find the key: $key\n');
} key = _rnd.nextInt(_seed);
pos = arr.length ~/ 2;
var oldValue = arr[pos];
if (arr.modify(pos, key)) {
print('modified the old value ($oldValue) to the new: $key at $pos');
}
pos = arr.length;
oldValue = arr[pos];
if (arr.modify(pos, key)) {
print('modified the old value ($oldValue) to the new: $key at $pos\n');
} else {
print('the position to be modified is out of bound.\n');
}
arr.display(); arr[0] = _rnd.nextInt(_seed);
arr[arr.length] = _rnd.nextInt(_seed);
arr.display(); print('now sort the array:');
arr.sort(0, arr.length - 1);
arr.display(); key = _rnd.nextInt(_seed);
if (arr.delete(key)) {
arr.display();
print('has deleted the key: $key');
} else {
print('can not find the key to delete: $key');
} print('now delete the key: ${arr[arr.length ~/ 2]}');
arr.delete(arr[arr.length ~/ 2]);
arr.display();
}
数据结构与算法之有序数组(2)——in dart的更多相关文章
- java版数据结构与算法第二章数组
数组由一组具有相同类型的数据元素组成,并存储在一组连续存储单元中.一维数组是常量. 二维数组:若一维数组中的数据元素又是一堆数据结构,我们称之为二维数组.二维数组可以看成是n个列向量组成的线性表. 数 ...
- java数据结构和算法01(数组的简单使用)
一直都对这一块没有什么想法,加上不怎么理解,只是懂个大概:最近突然感觉对数据结构和算法这块有点儿兴趣,决定还是尽量详细的看看这些结构和算法: 话说什么事数据结构和算法呢?现在我也说不上来,等我学的差不 ...
- Java成神之路:第二帖---- 数据结构与算法之稀疏数组
数据结构与算法--稀疏数组 转换方法 记录数组有几行几列,有多少个不同的值 把不同的值的元素的行列,记录在一个小规模的数组中,以此来缩小数组的规模 如图: 二维数组转稀疏数组 对原始的二维数组进行遍历 ...
- javascript数据结构和算法 第二章 (数组) 二
字符串表示的数组 join() 和 toString() 函数返回数组的字符串表示.这两个函数通过将数组中的元素用逗号分隔符切割,返回字符串数组表示. 这里有个样例: var names = [&qu ...
- Android版数据结构与算法(二):基于数组的实现ArrayList源码彻底分析
版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 本片我们分析基础数组的实现--ArrayList,不会分析整个集合的继承体系,这不是本系列文章重点. 源码分析都是基于"安卓版" ...
- golang数据结构和算法之StackArray数组堆栈
用数组实现的堆栈, 另一种,是用链表实现的堆栈, 在各种不同的编程语言上, 实现都是类似的. StackArray.go package StackArray //基于数组实现的堆栈 const ar ...
- Java数据结构和算法(二)树的基本操作
Java数据结构和算法(二)树的基本操作 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 一.树的遍历 二叉树遍历分为:前序遍 ...
- 数据结构与算法(九):AVL树详细讲解
数据结构与算法(一):基础简介 数据结构与算法(二):基于数组的实现ArrayList源码彻底分析 数据结构与算法(三):基于链表的实现LinkedList源码彻底分析 数据结构与算法(四):基于哈希 ...
- 《Java数据结构与算法》笔记-CH2有序数组
/** * 上个例子是无序数组,并且没有考虑重复元素的情况. * 下面来设计一个有序数组,我们设定不允许重复,这样提高查找的速度,但是降低了插入操作的速度. * 1.线性查找 * 2.二分查找 * 有 ...
随机推荐
- oracle给用户分配特定用户下特定表的只读权限
以下是测试过程,测试环境oracle 11.2.0.3 linux平台: 模拟将HR用户下的employees表的只读权限非配给test_ycr创建用户:SQL> create user tes ...
- Git Hub 使用手册参考
参考信息 1.http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0002.http://ww ...
- codeforces 811E Vladik and Entertaining Flags(线段树+并查集)
codeforces 811E Vladik and Entertaining Flags 题面 \(n*m(1<=n<=10, 1<=m<=1e5)\)的棋盘,每个格子有一个 ...
- day13 多线程建立方法
#01创建多线程 继承Thread类 覆盖run方法:run方法里面运行要执行的代码 创建对象 调用start方法,start方法会开启线程,然后调用run方法 获取线程名字: ...
- java 包(package)
package packageDemo2_5; public class packageDemo1 { String name;//同一个包里的类可以直接访问 //不同包里的类是不可以使用默认修饰符的 ...
- Redis.md
rpm 包安装 CentOS 系列系统安装redis可以通过第三方提供的rpm包进行安装: # yum install -y epel-release # yum install -y redis 源 ...
- C# - 静态类和静态构造函数
一个类只能有一个静态构造函数,该构造函数不能有访问修饰符,不能带任何参数,不能直接调用,只能在: 1 创建包含静态构造函数的类实例时 2 访问包含静态构造函数的静态成员时 会先调用静态构造函数,无论创 ...
- PHP面试系列之Linux(一) ----- Linux基础
一.系统安全 sudo:以系统管理者的身份执行指令,也就是说,经由 sudo 所执行的指令就好像是 root 亲自执行. su:用于变更为其他使用者的身份,除 root 外,需要键入该使用者的密码. ...
- HDU 1208 Pascal's Travels 经典 跳格子的方案数 (dp或者记忆化搜索)
Pascal's Travels Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- 【Linux资源管理】iotop命令监控磁盘使用情况
一.iotop工具介绍 I/O可谓是数据库\服务器的最大瓶颈问题了,在使用top.nmon.zabbix.sar等工具监控I/O时,要么没有I/O监控(如top.zabbix),要么仅仅监控到磁盘层面 ...