Professional Documents
Culture Documents
ASA1 Laboras
ASA1 Laboras
INFORMATIKOS FAKULTETAS
2021 m. kovo 21 d.
Priėmė:
KAUNAS 2021
1. Laboratorinio darbo užduotis:
Palyginti rikiavimo algoritmus, kai rikiavimas atliekamas masyve ir sąraše (linked list), t. y.
galimos tik tai struktūrai būdingos operacijos. Šiuos algoritmus reikia realizuojami dviem atvejais:
1) rikiuojami elementai (elemento struktūrą apibrėžia dėstytojas) saugomi operatyvinėje; 2)
rikiuojami duomenų elementai visą laiką saugojami išorinėje (diskinėje) atmintyje, o operatyvinėje
atmintyje gali būti saugojami tik neindeksuotuose kintamuosiuose (neturi būti masyvo, sąrašo ar jų
analogų operatyvinėje atmintyje). Mano atvėju buvo duoti a ir l atvėjai.
Rikiavimas „Merge Sort“, kai skaidoma į dvi dalis.
l) Paieškos uždavinys su Juodai Raudonu paieškos medžiu.
DMArray.cs
using System;
namespace DMLibrary.Array
{
public class DMArray<T> : IDMArray<T> where T : IComparable
{
private System.Array _array;
}
public override IDMArray<T> Copy()
{
var tempArray = new DMArray<T>(Length);
for (int i = 0; i < Length; i++)
{
tempArray[i] = this[i];
}
return tempArray;
}
DMArrayFile.cs
using DMLibrary.Array;
using DMLibrary.Helper;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace DMLibrary.Array
{
public class DMArrayFile<T> : IDMArray<T> where T : IComparable
{
private FileStream _fs;
private int _start;
private int _start_data;
private BackConverter<T> _converter;
private ToBuffer<T> _tobuffer;
private readonly int _n;
private readonly int _size; //Elemento dydis baitais;
private readonly OSVersion _version;
private readonly ArrayType _type = ArrayType.external;
_fs.Seek(possition, SeekOrigin.Begin);
_fs.Read(help, 0, sizeof(int));
var size = BitConverter.ToInt32(help, 0);
//http://www.morgantechspace.com/2013/08/convert-object-to-byte-array-
and-vice.html
// Convert an object to a byte array
public byte[] ObjectToByteArray(T obj)
{
if (obj == null)
return null;
return ms.ToArray();
}
// Convert a byte array to an Object
public T ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
Object obj = (Object)binForm.Deserialize(memStream);
return (T)obj;
}
}
}
IDMArray.cs
using System;
namespace DMLibrary.Array
{
public abstract class IDMArray<T>: IDisposable where T : IComparable
{
public abstract T this[int i] { get; set; }
public abstract int Length { get; }
IDMArray<T> L, R;
switch (this)
{
case DMArrayFile<T> file:
L = file.CopyToFile("L.bin");
R = file.CopyToFile("R.bin");
break;
default:
L = Copy();
R = Copy();
break;
}
L.Dispose();
R.Dispose();
}
MergeSort(l, m);
MergeSort(m + 1, r);
Merge(l, m, r);
}
}
ArrayType.cs
namespace DMLibrary.Helper
{
public enum ArrayType
{
inside = 0,
external = 1
}
}
Delegates.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace DMLibrary.Helper
{
public delegate T BackConverter<T>(byte[] buffer);
public delegate byte[] ToBuffer<T>(T item);
}
OSVersion.cs
namespace DMLibrary.Helper
{
public enum OSVersion
{
v32 = 32,
v64 = 64
}
}
RedBlackTree.cs
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace OpinionatedCode.Collections
{
public sealed class RedBlackTree<TKey, TValue>
{
private readonly RedBlackTreeNode<TKey, TValue> _leaf =
RedBlackTreeNode<TKey, TValue>.CreateLeaf();
public RedBlackTree()
{
Root = _leaf;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TValue Get(TKey key)
{
try
{
int hashedKey = key.GetHashCode();
RedBlackTreeNode<TKey, TValue> node = Root;
do
{
if (node.HashedKey == hashedKey)
return node.Value;
node = hashedKey < node.HashedKey ? node.Left : node.Right;
} while (true);
}
catch (NullReferenceException)
{
throw new KeyNotFoundException();
}
}
internal RedBlackTreeNode<TKey, TValue> Root { get; private set; }
z.Parent = y;
if (y == _leaf)
{
Root = z;
}
else if (z.HashedKey < y.HashedKey)
{
y.Left = z;
}
else
{
y.Right = z;
}
z.Left = _leaf;
z.Right = _leaf;
z.Color = RedBlackTreeNode<TKey, TValue>.ColorEnum.Red;
InsertFixup(z);
}
z.Parent.Color = RedBlackTreeNode<TKey,
TValue>.ColorEnum.Black;
z.Parent.Parent.Color = RedBlackTreeNode<TKey,
TValue>.ColorEnum.Red;
RotateRight(z.Parent.Parent);
}
}
else
{
var y = z.Parent.Parent.Left;
if (y.Color == RedBlackTreeNode<TKey, TValue>.ColorEnum.Red)
{
z.Parent.Color = RedBlackTreeNode<TKey,
TValue>.ColorEnum.Black;
y.Color = RedBlackTreeNode<TKey,
TValue>.ColorEnum.Black;
z.Parent.Parent.Color = RedBlackTreeNode<TKey,
TValue>.ColorEnum.Red;
z = z.Parent.Parent;
}
else
{
if (z == z.Parent.Left)
{
z = z.Parent;
RotateRight(z);
}
z.Parent.Color = RedBlackTreeNode<TKey,
TValue>.ColorEnum.Black;
z.Parent.Parent.Color = RedBlackTreeNode<TKey,
TValue>.ColorEnum.Red;
RotateLeft(z.Parent.Parent);
}
}
}
y.Parent = x.Parent;
if (x.Parent == _leaf)
{
Root = y;
}
else if (x == x.Parent.Left)
{
x.Parent.Left = y;
}
else
{
x.Parent.Right = y;
}
y.Left = x;
x.Parent = y;
}
y.Right = x;
x.Parent = y;
}
}
}
namespace OpinionatedCode.Collections
{
internal sealed class RedBlackTreeNode<TKey, TValue>
{
public enum ColorEnum
{
Red,
Black
};
internal RedBlackTreeNode()
{
IsLeaf = true;
Color = ColorEnum.Black;
HashedKey = 0;
}
Program.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using DMLibrary.Array;
using DMLibrary.Helper;
using OpinionatedCode.Collections;
namespace Pavyzdys__Array_
{
[Serializable]
class Data : IComparable<Data>, IComparable
{
public string str { get; set; }
public double frac { get; set; }
return frac.CompareTo(other.frac);
}
return arr;
}
return (Data)binForm.Deserialize(memStream);
}
class Program
{
static void Main(string[] args)
{
int n = 1400;
var mas = new DMArray<Data>(n);
var rnd = new Random(1000);
Console.WriteLine("=masf======================");
for (int i = 0; i < n; i++)
{
Console.WriteLine(masf[i]);
}
Console.WriteLine("=masf2=====================");
for (int i = 0; i < n; i++)
{
Console.WriteLine(masf2[i]);
}
var sw = Stopwatch.StartNew();
mas.MergeSort(0, masf.Length - 1);
sw.Stop();
var sort_list_ram = sw.ElapsedMilliseconds;
sw.Restart();
llist.MergeSort(0, masf.Length - 1);
sw.Stop();
var sort_llist_ram = sw.ElapsedMilliseconds;
sw.Restart();
masf.MergeSort(0, masf.Length - 1);
sw.Stop();
var sort_list = sw.ElapsedMilliseconds;
sw.Restart();
masf2.MergeSort(0, masf2.Length - 1);
sw.Stop();
var sort_llist = sw.ElapsedMilliseconds;
Console.WriteLine("=masf=sorted=====================");
for (int i = 0; i < n; i++)
{
Console.WriteLine(masf[i]);
}
Console.WriteLine("=masf2=sorted====================");
for (int i = 0; i < n; i++)
{
Console.WriteLine(masf2[i]);
}
var found = 0;
sw.Restart();
for (int i = 0; i < mas.Length; i++)
{
var key = mas[i].str;
try
{
rbl.Get(key);
found++;
}
catch (KeyNotFoundException)
{ }
}
sw.Stop();
3. Rezultatai:
Eksperimento metu buvo generuojami bei rikiuojami 200, 400, 600, 800, 1000, 1200, 1400,
1600 bei 1800 duomenų su string‘o 3 raidėm, bei double skaičiumi. Duomenys buvo rikiuojami ir
kaip masyvas, ir kaip sąrašas. Eksperimento rezultatai pateikti grafikuose (rikiavimo laiko
priklausomybė nuo duomenų kiekio):
Rekurentinė išraiška:
Array LinkedList
Grafikas:
25.000
20.000
15.000
10.000
5.000
0.000
0 200 400 600 800 1000 1200 1400 1600 1800
Array LinkedList
Raudonai-Juodas paieškos medis.
n Rbmedis
0
200 0.000
400 0.000
600 0.000
800 0.000
1000 0.001
1200 0.001
1400 0.001
1600 0.001
1800 0.001
0.001
0.000
0 200 400 600 800 1000 1200 1400 1600 1800
4. Išvados:
Remiantis svetainių https://www.journaldev.com/31541/merge-sort-algorithm-java-c-python
ir https://en.wikipedia.org/wiki/Merge_sort duomenimis, teorinis įterpimo rikiavimo algoritmo
efektyvumas visais trimis atvėjais yra toks pat. Galime pamatyti, kad operatyvėje atmintyje Linked
List yra daug spartesnis nei masyvo. Diskinėje atmintyje Linked List yra spartesnis nei masyvo.