public MyFileList(string filename, MyFileList original) { length = original.length; this.filename = filename; if (File.Exists(filename)) { File.Delete(filename); } try { using (BinaryWriter writer = new BinaryWriter(File.Open(filename, FileMode.Create))) { writer.Write(4); writer.Write(original.Head().Item1); writer.Write(original.Head().Item2); writer.Write(20); for (int j = 1; j < length; j++) { original.Next(); writer.Write(original.CurrentData(original.currentNode).Item1); writer.Write(original.CurrentData(original.currentNode).Item2); writer.Write(original.nextNode); } } this.fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite); } catch (IOException ex) { Console.WriteLine(ex.ToString()); } }
public void Replace(int index, MyFileList listas, int index2) { Byte[] data = new Byte[12]; listas.fs.Seek(index2 * 16 + 4, SeekOrigin.Begin); listas.fs.Read(data, 0, 12); fs.Seek(index * 16 + 4, SeekOrigin.Begin); fs.Write(data, 0, 12); }
public static void Test_File_List(int seed) { int n = 100; string filename; filename = @"myddatalist.dat"; MyFileList myFileList = new MyFileList(filename, n, seed); myFileList.Print(n); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); myFileList.mergeSort3Way(); stopwatch.Stop(); Console.WriteLine("sorted"); myFileList.Print(n); Console.WriteLine("Number of data {0}", n); Console.WriteLine("Time: {0}", stopwatch.Elapsed); Console.WriteLine("Ticks: {0}", stopwatch.ElapsedTicks); }
public static void mergeSort3WayRec(MyFileList gArray, int low, int high, MyFileList destArray) { // If array size is 1 then do nothing if (high - low < 2) { return; } // Splitting array into 3 parts int mid1 = low + ((high - low) / 3); int mid2 = low + 2 * ((high - low) / 3) + 1; // Sorting 3 arrays recursively mergeSort3WayRec(destArray, low, mid1, gArray); mergeSort3WayRec(destArray, mid1, mid2, gArray); mergeSort3WayRec(destArray, mid2, high, gArray); // Merging the sorted arrays merge(destArray, low, mid1, mid2, high, gArray); }
// Function for 3-way merge sort process public void mergeSort3Way() { // if array of size is zero returns null if (this == null) { return; } GetReady(); // creating duplicate of given array // copying elements of given array into // duplicate array MyFileList fArray = new MyFileList(@"mydatalistcopy.dat", this); //// sort function mergeSort3WayRec(fArray, 0, this.length, this); //// copy back elements of duplicate array //// to given array for (int i = 0; i < fArray.Length; i++) { this.Replace(i, fArray, i); } fArray.fs.Close(); File.Delete(@"mydatalistcopy.dat"); }
/* Merge the sorted ranges [low, mid1), [mid1, * mid2) and [mid2, high) mid1 is first midpoint * index in overall range to merge mid2 is second * midpoint index in overall range to merge*/ public static void merge(MyFileList gArray, int low, int mid1, int mid2, int high, MyFileList destArray) { int i = low, j = mid1, k = mid2, l = low; // choose smaller of the smallest in the three ranges while ((i < mid1) && (j < mid2) && (k < high)) { if (gArray.Nodedata(i).CompareTo(gArray.Nodedata(j)) < 0) { if (gArray.Nodedata(i).CompareTo(gArray.Nodedata(k)) < 0) { destArray.Replace(l++, gArray, i++); } else { destArray.Replace(l++, gArray, k++); } } else { if (gArray.Nodedata(j).CompareTo(gArray.Nodedata(k)) < 0) { destArray.Replace(l++, gArray, j++); } else { destArray.Replace(l++, gArray, k++); } } } // case where first and second ranges have // remaining values while ((i < mid1) && (j < mid2)) { if (gArray.Nodedata(i).CompareTo(gArray.Nodedata(j)) < 0) { destArray.Replace(l++, gArray, i++); } else { destArray.Replace(l++, gArray, j++); } } // case where second and third ranges have // remaining values while ((j < mid2) && (k < high)) { if (gArray.Nodedata(j).CompareTo(gArray.Nodedata(k)) < 0) { destArray.Replace(l++, gArray, j++); } else { destArray.Replace(l++, gArray, k++); } } // case where first and third ranges have // remaining values while ((i < mid1) && (k < high)) { if (gArray.Nodedata(i).CompareTo(gArray.Nodedata(k)) < 0) { destArray.Replace(l++, gArray, i++); } else { destArray.Replace(l++, gArray, k++); } } // copy remaining values from the first range while (i < mid1) { destArray.Replace(l++, gArray, i++); } // copy remaining values from the second range while (j < mid2) { destArray.Replace(l++, gArray, j++); } // copy remaining values from the third range while (k < high) { destArray.Replace(l++, gArray, k++); } }