//BestFit Decreasing Algorithm Using Linear Search Button private void button8_Click(object sender, EventArgs e) { if (InputFolder == null) { button1.Focus(); errorProvider1.SetError(textBox1, "Please Choose Input Path"); } else if (OutputFolder == null) { button2.Focus(); errorProvider1.SetError(textBox2, "Please Choose Input Path"); } else if (textBox3 == null) { textBox3.Focus(); errorProvider1.SetError(textBox3, "Please Enter Folder Capacity"); } else { List <Sound> BestFitDec = new List <Sound>(sounds); MergeSortSounds(BestFitDec, 0, BestFitDec.Count - 1); //Displaying Sounds after sorting them in descending order /*for (int i = 0; i < BestFitDec.Count; i++) * { * MessageBox.Show(BestFitDec[i].GetName() + " -- " + BestFitDec[i].GetSeconds().ToString()); * } */ string BestFitDecDir = OutputFolder + @"\[8] BesttFit Decreasing (L-S)"; if (Directory.Exists(BestFitDecDir)) { Directory.Delete(BestFitDecDir, true); } timer1.Start(); Directory.CreateDirectory(BestFitDecDir); List <Folder> folders = new List <Folder>(); Folder f = new Folder("F1", BestFitDecDir + @"\", MaxFolerSeconds); folders.Add(f); folders[0].FolderSounds.Add(BestFitDec[0]); folders[0].SecondsCapacity = folders[0].SecondsCapacity - BestFitDec[0].GetSeconds(); for (int i = 1; i < BestFitDec.Count; i++) { int index = -1; double c = 1000000; for (int j = 0; j < folders.Count; j++) { if (BestFitDec[i].GetSeconds() <= folders[j].SecondsCapacity && folders[j].SecondsCapacity <= c) { index = j; c = folders[j].SecondsCapacity; } } if (index > -1) { folders[index].FolderSounds.Add(BestFitDec[i]); folders[index].SecondsCapacity = folders[index].SecondsCapacity - BestFitDec[i].GetSeconds(); } else { Folder s = new Folder("F" + (folders.Count + 1).ToString(), BestFitDecDir + @"\", MaxFolerSeconds); folders.Add(s); folders[folders.Count - 1].FolderSounds.Add(BestFitDec[i]); folders[folders.Count - 1].SecondsCapacity = folders[folders.Count - 1].SecondsCapacity - BestFitDec[i].GetSeconds(); } } //Display the folders names and the count of stored seconds after filling them for (int i = 0; i < folders.Count; i++) { folders[i].writing(InputFolder, MaxFolerSeconds); //MessageBox.Show(folders[i].GetFolderName() + " -- " + TimeSpan.FromSeconds(MaxFolerSeconds - folders[i].SecondsCapacity).Duration().ToString()); } timer1.Stop(); label13.Text = TimeSpan.FromSeconds(time).Duration().ToString(); time = 0; MessageBox.Show("BestFit Decreasing Algorithm\nUsing Linear Search\nIs Done", "Congratulations!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } }
//WorstFit Decreasing Algorithm Using Linear Search Button private void button5_Click(object sender, EventArgs e) { if (InputFolder == null) { button1.Focus(); errorProvider1.SetError(textBox1, "Please Choose Input Path"); } else if (OutputFolder == null) { button2.Focus(); errorProvider1.SetError(textBox2, "Please Choose Input Path"); } else if (textBox3 == null) { textBox3.Focus(); errorProvider1.SetError(textBox3, "Please Enter Folder Capacity"); } else { List <Sound> WorstFitDec = new List <Sound>(sounds); //O(1) MergeSortSounds(WorstFitDec, 0, WorstFitDec.Count - 1); //mergesort O(nlogn) //Displaying Sounds after sorting them in descending order for (int i = 0; i < WorstFitDec.Count; i++) /* for (int i = 0; i < WorstFitDec.Count; i++) * { * MessageBox.Show(WorstFitDec[i].GetName() + " -- " + WorstFitDec[i].GetSeconds().ToString()); * } */ string WorstFitDecDir = OutputFolder + @"\[3] WorstFit Decreasing (L-S)"; //O(1) if (Directory.Exists(WorstFitDecDir)) //O(1) { Directory.Delete(WorstFitDecDir, true); //O(1) } timer1.Start(); Directory.CreateDirectory(WorstFitDecDir); //O(1) List <Folder> folders = new List <Folder>(); //O(1) Folder f = new Folder("F1", WorstFitDecDir + @"\", MaxFolerSeconds); //O(1) folders.Add(f); //O(1) folders[0].FolderSounds.Add(WorstFitDec[0]); //O(1) folders[0].SecondsCapacity = folders[0].SecondsCapacity - WorstFitDec[0].GetSeconds(); //O(1) for (int i = 1; i < WorstFitDec.Count; i++) //O(n)*(entire)=O(n*m) n: number of sounds, m=number of folders { int index = -1; //O(1*n) double c = 0; //O(1*n) for (int j = 0; j < folders.Count; j++) //O(m*n) { if (WorstFitDec[i].GetSeconds() <= folders[j].SecondsCapacity && folders[j].SecondsCapacity >= c) //O(1*m*n) { index = j; //O(1*1*1m*n) c = folders[j].SecondsCapacity; //O(1*1*m*n) } } if (index > -1) //O(1*n) { folders[index].FolderSounds.Add(WorstFitDec[i]); //O(1*1*n) folders[index].SecondsCapacity = folders[index].SecondsCapacity - WorstFitDec[i].GetSeconds(); //O(1*1*n) } else { Folder s = new Folder("F" + (folders.Count + 1).ToString(), WorstFitDecDir + @"\", MaxFolerSeconds); //O(1*n) folders.Add(s); //O(1*n) folders[folders.Count - 1].FolderSounds.Add(WorstFitDec[i]); //O(1*n) folders[folders.Count - 1].SecondsCapacity = folders[folders.Count - 1].SecondsCapacity - WorstFitDec[i].GetSeconds(); //O(1*n) } } //Display the folders names and the count of stored seconds after filling them for (int i = 0; i < folders.Count; i++) //O(n)*(entire)=O(n*m) { folders[i].writing(InputFolder, MaxFolerSeconds); //this function is O(m)*O(n), =number of sounds in the folder // MessageBox.Show(folders[i].GetFolderName() + " -- " + TimeSpan.FromSeconds(MaxFolerSeconds - folders[i].SecondsCapacity).Duration().ToString()); } timer1.Stop(); label9.Text = TimeSpan.FromSeconds(time).Duration().ToString(); time = 0; MessageBox.Show("WorstFit Decreasing Algorithm\nUsing Linear Search\nIs Done", "Congratulations!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } }