private void GenerateData(int length) { ButtonControl(false); Random rnd = new Random(); GeneratedArray = new MyArrayRAM(length); GeneratedList = new MyListRAM(); GeneratedTable = new LinearTable(length); if (GeneratedArrayD != null) { (GeneratedArrayD as MyArrayIO).Close(); } GeneratedArrayD = new MyArrayIO("data2.dat"); GeneratedArrayD.Length = length; for (int i = 0; i < length; i++) { GeneratedArray[i] = rnd.Next(50000); GeneratedList.AddData(GeneratedArray[i]); GeneratedArrayD[i] = GeneratedArray[i]; GeneratedTable.Insert(i, GeneratedArray[i]); } ButtonControl(true); }
private void PrintTextBox(TextBox tb, MyArray text) { if (checkBox2.Checked) { return; } String tbText = ""; int i = 0; int end = int.MaxValue; if (checkBox1.Checked) { i = int.Parse(textBox5.Text); end = int.Parse(textBox6.Text); } for (i = i; i < text.Length && i < end; i++) { tbText += text[i].ToString() + "\r\n"; } tb.Text = tbText; }
static private void Heapify(MyArray arr, int index) { int left = 2 * index; int right = 2 * index + 1; int largest = index; OperationCount += 3; if (left <= heapSize && arr[left] > arr[index]) { OperationCount += 2; largest = left; } if (right <= heapSize && arr[right] > arr[largest]) { OperationCount += 2; largest = right; } if (largest != index) { OperationCount++; Swap(arr, index, largest); Heapify(arr, largest); } }
public override void CopyTo(MyArray other, int index) { for (int i = 0; i < Length; i++) { other[index + i] = arr[i]; } }
static private void Swap(MyArray arr, int x, int y) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; OperationCount += 3; }
public static MyArray Sort(MyArray arr) { //MyArray arr = new MyArrayRAM(data.Length); //data.CopyTo(arr, 0); OperationCount = 0; QuickSort(arr, 0, arr.Length); return(arr); }
static private void BuildHeap(MyArray arr) { heapSize = arr.Length - 1; OperationCount++; for (int i = heapSize / 2; i >= 0; i--) { OperationCount++; Heapify(arr, i); } }
static public MyArray Sort(MyArray arr) { OperationCount = 0; BuildHeap(arr); for (int i = arr.Length - 1; i >= 0; i--) { Swap(arr, 0, i); heapSize--; Heapify(arr, 0); } return(arr); }
private static void QuickSort(MyArray arr, int left, int right) { if (arr == null || arr.Length <= 1) { return; } OperationCount += 2; if (left < right) { int pivot = MyPartition(arr, left, right); OperationCount++; QuickSort(arr, left, pivot - 1); OperationCount++; QuickSort(arr, pivot, right); OperationCount++; } }
private void PrintGeneratedTextBox(TextBox tb, MyArray text) { if (checkBox2.Checked) { return; } String tbText = ""; for (int i = 0; i < text.Length; i++) { tbText += text[i].ToString() + "\r\n"; } tb.Text = tbText; }
private static int MyPartition(MyArray arr, int left, int right) { int start = left; int pivot = arr[start]; left++; right--; OperationCount += 4; while (true) { while (left <= right && arr[left] <= pivot) { left++; OperationCount++; } while (left <= right && arr[right] > pivot) { right--; OperationCount++; } int temp; if (left > right) { temp = arr[left - 1]; arr[left - 1] = arr[start]; arr[start] = temp; OperationCount += 3; return(left); } temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; OperationCount += 7; } }
private void CallSort() { ButtonControl(false); switch (listBox1.SelectedIndex * 100 + listBox2.SelectedIndex * 10 + listBox3.SelectedIndex) { case 0: timer.Restart(); GeneratedArray = Heapsort.Sort(GeneratedArray); timer.Stop(); PrintTextBox(textBox2, GeneratedArray); label7.Text = "Operations:" + Heapsort.OperationCount; label8.Text = "Time elapsed:" + timer.ElapsedMilliseconds; break; case 10: timer.Restart(); GeneratedArrayD = Heapsort.Sort(GeneratedArrayD); timer.Stop(); PrintTextBox(textBox2, GeneratedArrayD); label7.Text = "Operations:" + Heapsort.OperationCount; label8.Text = "Time elapsed:" + timer.ElapsedMilliseconds; break; case 100: timer.Restart(); GeneratedArray = Quicksort.Sort(GeneratedArray); timer.Stop(); PrintTextBox(textBox2, GeneratedArray); label7.Text = "Operations:" + Quicksort.OperationCount; label8.Text = "Time elapsed:" + timer.ElapsedMilliseconds; break; case 110: timer.Restart(); //PrintTextBox(textBox2, GeneratedArrayD); GeneratedArrayD = Quicksort.Sort(GeneratedArrayD); timer.Stop(); PrintTextBox(textBox2, GeneratedArrayD); label7.Text = "Operations:" + Quicksort.OperationCount; label8.Text = "Time elapsed:" + timer.ElapsedMilliseconds; break; case 101: timer.Restart(); GeneratedList = Quicksort.Sort(GeneratedList); timer.Stop(); PrintTextBox(textBox2, GeneratedList); label7.Text = "Operations:" + Quicksort.OperationCount; label8.Text = "Time elapsed:" + timer.ElapsedMilliseconds; break; case 111: timer.Restart(); GeneratedListD = Quicksort.Sort(GeneratedListD); Quicksort.Sort(GeneratedListD); PrintTextBox(textBox2, GeneratedListD); label7.Text = "Operations:" + Quicksort.OperationCount; label8.Text = "Time elapsed:" + timer.ElapsedMilliseconds; break; } ButtonControl(true); }
public abstract void CopyTo(MyArray data, int v);
public override void CopyTo(MyArray data, int v) { throw new NotImplementedException(); }