数据结构与算法之有序数组(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.二分查找 * 有 ...
随机推荐
- 毫秒级百万数据分页存储过程(mssql)
/****** Object: StoredProcedure [dbo].[up_Page2005] Script Date: 11/28/2013 17:10:47 ******/ SET ANS ...
- January 08 2017 Week 2nd Sunday
Believe not all that you see nor half what you hear. 眼见的不能全信,耳闻的也不能半信. What you hear, what you see, ...
- codeforces 703E Mishka and Divisors
codeforces 703E Mishka and Divisors 题面 给出大小为\(1000\)的数组和一个数\(k\),求长度最短的一个子序列使得子序列的元素之积是\(k\)的倍数,如果有多 ...
- 4、Node.js REPL(交互式解释器)
Node.js REPL(Read Eval Print Loop:交互式解释器) 表示一个电脑的环境,类似 Window 系统的终端或 Unix/Linux shell,我们可以在终端中输入命令,并 ...
- codeforces 917D Stranger Trees
题目链接 正解:矩阵树定理+拉格朗日插值. 一下午就搞了这一道题,看鬼畜英文题解看了好久.. 首先这题出题人给了两种做法,感觉容斥+$prufer$序列+$dp$的做法细节有点多所以没看,然而这个做法 ...
- Guava包学习--EventBus
之前没用过这个EventBus,然后看了一下EventBus的源码也没看明白,(-__-)b.反正大概就是弄一个优雅的方式实现了观察者模式吧.慢慢深入学习一下. 观察者模式其实就是生产者消费者的一个变 ...
- Guava包学习--Multiset
Multiset之前倒是没用过,后来看了下还挺有用,其实它就是支持重复元素的HashSet,相当于list+set的集合,综合了两种集合的优点. 它扩展了Collection: @GwtCompati ...
- java BigDecimal解析及注意事项
BigDecimal简介 JDK文档(中文)中的解释如下: 不可变的.任意精度的有符号十进制数.BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成.如果为零或 ...
- C/C++——new/delete和malloc/free的区别
new/delete和malloc/free的区别 扩容操作: 对于malloc是有一个realloc函数对应用于扩容的: 对于new,只能再new一个,for循环赋值过去,把原来的delete掉: ...
- EasyConnect 使用方法
一.此处以安卓系统为例进行介绍. 1.通过谷歌市场下载 EasyConnect,安装完成后,打开EasyConnect,界面如下图 1 所示 <ignore_js_op> 2.输入 SSL ...