public static void Test_Array_List(int seed, int n) { Console.WriteLine("elementų kiekis: " + n); Console.WriteLine("\n Operatyviojoje atmintyje suliejimo rykiavimas\n"); Stopwatch watch = new Stopwatch(); MyDataArray myarray = new MyDataArray(n, seed); MyDataArray MySortedArray; MyDataList MySortedList; Console.WriteLine("\n masyvas \n"); myarray.Print(n); watch.Start(); MySortedArray = (MyDataArray)MergeSort(myarray); watch.Stop(); var arrayTime = watch.Elapsed; Console.WriteLine("\n isrykiuotas \n"); MySortedArray.Print(n); MyDataList mylist = new MyDataList(n, seed); Console.WriteLine("\n sąrašas \n"); mylist.Print(n); watch.Start(); MySortedList = (MyDataList)MergeSort(mylist); watch.Stop(); var listTime = watch.Elapsed; Console.WriteLine("\n isrykiuotas \n"); MySortedList.Print(n); Console.WriteLine("masyvo vykdymo laikas: " + arrayTime + " sąrašo vykdymo laikas: " + listTime); }
//-------------------------------------------------------- //----------------------------------------------------- //susietojo sąrašo rikiavimas private static DataList MergeSort(DataList unsorted) { if (unsorted.Length <= 1) { return(unsorted); } DataList left; DataList right; if (unsorted.GetType() == typeof(MyDataList)) { left = new MyDataList(); right = new MyDataList(); } else { left = new MyFileList(); right = new MyFileList(); } MyDataStruct data; int middle = unsorted.Length / 2; data = unsorted.First(); for (int i = 0; i < middle; i++) //dalinam sarasa { left.Add(data); data = unsorted.Next(); } for (int i = middle; i < unsorted.Length; i++) { right.Add(data); if (unsorted.Length != left.Length + right.Length) { data = unsorted.Next(); } } left = MergeSort(left); right = MergeSort(right); return(Merge(left, right)); }
private static DataList Merge(DataList left, DataList right) { DataList result; if (left.GetType() == typeof(MyDataList)) { result = new MyDataList(); } else { result = new MyFileList(); } while (left.Length > 0 || right.Length > 0) { if (left.Length > 0 && right.Length > 0) { if (left.First() <= right.First()) { result.Add(left.First()); left.RemoveFirst(); } else { result.Add(right.First()); right.RemoveFirst(); } } else if (left.Length > 0) { result.Add(left.First()); left.RemoveFirst(); } else if (right.Length > 0) { result.Add(right.First()); right.RemoveFirst(); } } return(result); }
public static void Test_Array_List_Comparison(int seed, int n) { Stopwatch watch = new Stopwatch(); MyDataArray myarray = new MyDataArray(n, seed); watch.Start(); MyDataArray MySortedArray = (MyDataArray)MergeSort(myarray); watch.Stop(); var arrayTime = watch.Elapsed; MyDataList mylist = new MyDataList(n, seed); watch.Start(); MyDataList MySortedList = (MyDataList)MergeSort(mylist); watch.Stop(); var listTime = watch.Elapsed; Console.WriteLine("{0, -10} {1, -20} {2, -20}", n, arrayTime, listTime); }