static void Main(string[] args) { InputData data = new InputData("me_at_the_zoo.txt"); foreach (var cache in data.Caches) { Results.Add(new Result { CacheId = cache.CacheId, VideoIds = new List <int>() }); } while (data.requestDescriptions.Count > 0 && AreFreeChaches(data.Caches, data.minVideoSize)) { foreach (var endPoint in data.endPoints.OrderByDescending(xx => xx.DataCenterLatency).ThenByDescending(xx => xx.TotalRequests)) { List <RequestDesc> endPointRequests = data.requestDescriptions.Where(xx => xx.EndPointId == endPoint.Id).ToList(); TimSortExtender.TimSort(endPointRequests, CompareRequests); if (endPointRequests.Count <= 0) { continue; } int videoWithHighestProb = data.videoSizes[endPointRequests.Last().VideoId]; List <CacheEndpointLatency> endpointLatencies = endPoint.CacheLatencies; TimSortExtender.TimSort(endpointLatencies, CompareLatencys); //int cacheWithLargestDifference = endpointLatencies.First().CacheId; foreach (var cacheLatency in endpointLatencies) { Cache toAddVideo = data.Caches.First(xx => xx.CacheId == cacheLatency.CacheId); if (toAddVideo.CacheSize > videoWithHighestProb) { Results.First(xx => xx.CacheId == cacheLatency.CacheId).VideoIds.Add(endPointRequests.Last().VideoId); toAddVideo.CacheSize -= videoWithHighestProb; break; } } data.requestDescriptions.Remove(endPointRequests.Last()); } } using (StreamWriter sw = new StreamWriter("output.txt")) { sw.WriteLine(Results.Count); foreach (var result in Results) { string value = result.CacheId.ToString(); foreach (int id in result.VideoIds) { value += " " + id.ToString(); } sw.WriteLine(value); } sw.Flush(); sw.Close(); } }
public ExperementForm(OpenFileDialog openFileDialog1) { double countFiles = 0; InitializeComponent(); chart1.Series.Clear(); chart2.Series.Clear(); chart3.Series.Clear(); foreach (String file in openFileDialog1.FileNames) { int R_c = 0; int B_c = 0; int G_c = 0; this.tabPage4.Text = "Пересылки"; Terzia TimTerz = new Terzia(0, 0, 0); Terzia MergTerz = new Terzia(0, 0, 0); using (var sr = new StreamReader(file)) { int[] arr = new int[File.ReadAllLines(file).Length]; for (int i = 0; i < arr.Length; i++) { arr[i] = Convert.ToInt32(sr.ReadLine()); } int[] tsv = new int[arr.Length]; for (int i = 0; i < arr.Length; i++) { tsv[i] = arr[i]; } TimSortExtender.TimSort <int>(arr, ref TimTerz.changes, ref TimTerz.compares, ref TimTerz.time); MergeSortAlgorithm.MergeSort(arr, ref MergTerz); string fileName = file.Substring(43); string f1 = fileName + "TimSort"; string f2 = fileName + "MergeSort"; int midTimeTsort = 0; int midTimeMsort = 0; int[] qwerty = new int[15]; int MX = 0, MN = 0; int MX_index = 0, MN_index = 0; Random rnd = new Random(); R_c = rnd.Next(256); B_c = 0; G_c = rnd.Next(256); chart3.Series.Add(f2); chart3.Series[f2].Points.AddXY(countFiles, MergTerz.changes); chart2.Series.Add(f2); chart2.Series[f2].Points.AddXY(countFiles, MergTerz.compares); chart1.Series.Add(f2); for (int uio = 0; uio < 15; uio++) { MergeSortAlgorithm.MergeSort(arr, ref MergTerz); qwerty[uio] = MergTerz.time; } MX = 0; for (int uio = 0; uio < 15; uio++) { if (MX <= qwerty[uio]) { MX = qwerty[uio]; MX_index = uio; } } qwerty[MX_index] = 0; MN = MX; for (int uio = 0; uio < 15; uio++) { if (MN >= qwerty[uio]) { MN = qwerty[uio]; MN_index = uio; } } qwerty[MN_index] = 0; for (int uio = 0; uio < 15; uio++) { midTimeMsort += qwerty[uio]; } MergTerz.time = midTimeMsort / 13; chart1.Series[f2].Points.AddXY(countFiles, MergTerz.time); chart3.Series[f2].Color = Color.FromArgb(R_c, B_c, G_c); chart2.Series[f2].Color = Color.FromArgb(R_c, B_c, G_c); chart1.Series[f2].Color = Color.FromArgb(R_c, B_c, G_c); R_c = rnd.Next(256); B_c = rnd.Next(256); G_c = 0; chart3.Series.Add(f1); chart3.Series[f1].Points.AddXY(countFiles, TimTerz.changes); chart2.Series.Add(f1); chart2.Series[f1].Points.AddXY(countFiles, TimTerz.compares); chart1.Series.Add(f1); for (int uio = 0; uio < 15; uio++) { TimSortExtender.TimSort <int>(arr, ref TimTerz.changes, ref TimTerz.compares, ref TimTerz.time); qwerty[uio] = TimTerz.time; for (int i = 0; i < arr.Length; i++) { arr[i] = tsv[i]; } } MX = 0; for (int uio = 0; uio < 15; uio++) { if (MX <= qwerty[uio]) { MX = qwerty[uio]; MX_index = uio; } } qwerty[MX_index] = 0; MN = MX; for (int uio = 0; uio < 15; uio++) { if (MN >= qwerty[uio]) { MN = qwerty[uio]; MN_index = uio; } } qwerty[MN_index] = 0; for (int uio = 0; uio < 15; uio++) { midTimeTsort += qwerty[uio]; } TimTerz.time = midTimeTsort / 13; chart1.Series[f1].Points.AddXY(countFiles, TimTerz.time); chart3.Series[f1].Color = Color.FromArgb(R_c, B_c, G_c); chart2.Series[f1].Color = Color.FromArgb(R_c, B_c, G_c); chart1.Series[f1].Color = Color.FromArgb(R_c, B_c, G_c); sr.Close(); } } }
/// <summary> /// Sorts a list using the faster TimSort /// We use TimSort because the list is usually already (mostly) sorted and /// TimSort has a best time of O(n) vs the built-in QuickSort's O(n log(n)) /// Common cases are for inserting events and long notes (Holds, Rolls, etc.) /// in their correct locations among the sea of Tap notes /// </summary> public void SortList <T>(IList <T> list) { //list.Sort(); TimSortExtender.TimSort <T>(list, false); }
public void sortRequestDescriptionsByProbability(List <RequestDesc> input) { TimSortExtender.TimSort(input); }