static void Main(string[] args) { MultipleArrayMemoryManagementUnit.SetMemoryManagementUnitType(); //BoundedMemoryManagementUnit.SetMemoryManagementUnitType(1000); //SwappingMemoryManagementUnit.SetMemoryManagementUnitType(150); FileStream fs = new FileStream("Debug.txt", FileMode.Create); Debug.Listeners.Add(new TextWriterTraceListener(Console.Out)); Debug.Listeners.Add(new TextWriterTraceListener(fs)); Random rnd = new Random(); SortingThread st = new SortingThread(); int[] a = new int[100]; int[] b = new int[100]; int idx = 0; for (idx = 0; idx < a.Length; idx++) { a[idx] = rnd.Next(1000); } st.CopyFrom(a); Debug.WriteLine(st); st.Start(); Thread.Sleep(2000); st.Join(); st.CopyTo(b); Debug.WriteLine(st); Debug.Assert(VerifySort(a, b)); Debug.Close(); }
//uses quick sort to sort an array public void Sort() { int iLength = m_aArrayToSort.Length; //more comfort to the eye if (iLength <= 1) { return; } Random random = new Random(); int randomNumber = random.Next(iLength); //piking a random number between 0 and the length of the current array //pick a median //partition the array into numbers larger than median and numbers smaller than median int pivot = partition(m_aArrayToSort, 0, iLength - 1, randomNumber); //Create two SortingThreads SortingThread thread_one = new SortingThread(); SortingThread thread_two = new SortingThread(); //Copy the smaller part of the array into the first thread thread_one.CopyFrom(m_aArrayToSort, 0, pivot - 1); //Copy the larger part of the array into the second thread thread_two.CopyFrom(m_aArrayToSort, pivot + 1, iLength - 1); //Run the two threads and wait for them to terminate thread_one.Start(); thread_two.Start(); thread_one.Join(); thread_two.Join(); //Copy the two sorted arrays into the original array for (int i = 0; i < pivot; i++) { m_aArrayToSort[i] = thread_one.m_aArrayToSort[i]; } int k = 0; for (int j = pivot + 1; j < iLength; j++) { m_aArrayToSort[j] = thread_two.m_aArrayToSort[k]; k++; } //Delete the arrays thread_one.DeleteArray(); thread_two.DeleteArray(); }