本文比第一篇,采用了类实现。增加了运算符重载等功能。本来有序数组是不能修改某个位置的值的,因为这样会打破数组的有序性;但为了演示,保留了修改的方法,但为此增加了排序。

 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();
}

最新文章

  1. PHP-Mysqli扩展库的预编译
  2. Unity Tidy Tile Pack #1
  3. 26. Binary Tree Maximum Path Sum
  4. 一个简单的 ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript 。
  5. 新浪微博数据抓取(java实现)
  6. 李洪强iOS开发之最全App上架流程
  7. Vs 2008 解决方案的目录结构设置和管理(转)
  8. [AngularJS] Error: $location:nobase
  9. Spring Uploading Files
  10. POJ--2923--Relocation--如压力DP
  11. Appium Android Bootstrap源码分析之简介
  12. JavaScript中JSON字符串和JSON对象相互转化
  13. redis的主从复制与哨兵
  14. EF Core 快速上手——EF Core 入门
  15. IIS+Tomcat功能iis端口2
  16. react+antdesign
  17. 加载XML文件到系统中
  18. 【原创】连接数据库MySQL,读取、显示、修改数据
  19. scala IDE for Eclipse开发Spark程序
  20. python用zipfile模块打包文件或是目录、解压zip文件实例

热门文章

  1. haproxy开启日志功能
  2. ASP.NET Core 返回文件、用户下载文件,从网站下载文件,动态下载文件
  3. 沉淀再出发:Bean,JavaBean,POJO,VO,PO,EJB等名词的异同
  4. Asp.Net MVC Identity 2.2.1 使用技巧(八)
  5. December 06th 2016 Week 50th Tuesday
  6. python28 excel读取模块xlrd
  7. Anaconda安装及pygame的安装
  8. RedisClient的安装及基本使用
  9. IIs和ftp
  10. 如何彻底修改eclipse中的名称