数据结构与算法之有序数组(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.二分查找 * 有 ...
随机推荐
- cmd命令提示符大全(干货)
cmd是command的缩写.即命令提示符(CMD),是在OS / 2 , Windows CE与Windows NT平台为基础的操作系统(包括Windows 2000和XP中, Vista中,和Se ...
- Retrieving failed records after an SqlBulkCopy exception
Let me start by saying that the idea I used in this article is not originally mine, but since I have ...
- [转]unix/linux中的dup()系统调用
[转]unix/linux中的dup()系统调用 在linux纷繁复杂的内核代码中,sys_dup()的代码也许称得上是最简单的之一了,但是就是这么一个简单的系统调用,却成就了unix/linu ...
- scala当中的特质trait
1.将trait作为接口使用 Scala中的trait是一种特殊的概念: 首先先将trait作为接口使用,此时的trait就与Java中的接口 (interface)非常类似: 在trait中可以定义 ...
- Java遇到的问题、错误——持续更新
内容:dead code.关于eclipse没有js代码提示的解决 持续更新 ######################################################## dead ...
- angular2 Router类中的路由跳转navigate
navigate是Router类的一个方法,主要用来路由跳转. 函数定义 navigate(commands: any[], extras?: NavigationExtras) : Promise` ...
- Eclipse html 编辑器插件下载安装
需要在eclipse里面编辑html和jsp,语法高亮和语法提示,自动补全等. 一.下载GEF(依赖包): 1.下载地址:http://www.eclipse.org/downloads/downlo ...
- Cordova Android源代码分析系列一(项目总览和CordovaActivity分析)
版权声明:本文为博主offbye西涛原创文章.未经博主同意不得转载. https://blog.csdn.net/offbye/article/details/31776833 PhoneGap/Co ...
- Django的时区设置问题
1.Django的时区问题 django默认的时区是UTC,平时是没有什么影响的,但是在需要将时间戳转换成本时区的时间或者是获取当前的本地的localtime的时候就出现了问题.之前程序在测试时是运行 ...
- [19/04/17-星期三] Java的动态性_反射(Reflection)机制
一.前言 动态语言:程序运行时,可以改变程序结构或变量类型.典型的代表:Python,ruby,JavaScript 如JavaScript代码: function test(){ var s=&qu ...