I have an array creating 10 random integers and then sorting them using quicksort..My problem is that when I change this to creating 1,000,000 random integers it wont do it..Can you help please?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RepeatAssignmentQ2
{
class Program
{
static public int Partition(int[] myArray, int left, int right)
{
int pivot = myArray[left];
while (true)
{
while (myArray[left] < pivot)
left++;
while (myArray[right] > pivot)
right--;
if (left < right)
{
int temp = myArray[right];
myArray[right] = myArray[left];
myArray[left] = temp;
}
else
{
return right;
}
}
}
static public void QuickSort_Recursive(int[] arr, int left, int right)
{
// For Recusrion
if (left < right)
{
int pivot = Partition(arr, left, right);
if (pivot > 1)
QuickSort_Recursive(arr, left, pivot - 1);
if (pivot + 1 < right)
QuickSort_Recursive(arr, pivot + 1, right);
}
}
static void Main(string[] args)
{
Random rnd = new Random();
DateTime startTime = DateTime.Now;
int ind = 0;
int length = 1000000;
int[] myArray = new int[length];
while (ind < 1000000)
{
myArray[ind] = rnd.Next(1000000);
ind++;
}
int lengthTwo = 10;
Console.WriteLine("QuickSort by recursive method");
QuickSort_Recursive(myArray,0, lengthTwo - 1 );
for (int i = 0; i < 1000000; i++)
{
Console.WriteLine(myArray[i]);
}
Console.WriteLine("Total Time: {0}\n", DateTime.Now - startTime);
Console.WriteLine();
}
}
}
Thanks
Edit - When I tun the program with the array having 10 number it will display them and have them sorted. When I change it to 1,000,000 and run the program nothing displays.
Edit 2 - Okay for some weird reason it is doing this. I changed the code above to display the changes, It is now displaying the numbers it randomly generated but its not sorting them. But when IT only have to create 10 random numbers it did sort it.