C
C#•2y ago
rallez

Generate random array and quicksort algorithm

Want to make a program that contains functions that generates random array, prints it and sorts it.
void GenArray(int[] myArray, int size, int minSize, int maxSize)
{
Random random = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < size; i++)
{
myArray[i] = random.Next(minSize, maxSize + 1);
}
}

void PrintArray(int[] myArray, int size)
{
for (int i = 0; i < size; i++)
{
Console.WriteLine("[" + i + "] " + myArray[i]);
}
}

int Partition(int[] myArray, int l, int r)
{
int pivot = myArray[l];
int count = 0;

for(int i = l + 1; i <= r; i++)
{
if(myArray[i] <= pivot)
{
count++;
}
}

int pivotIndex = l + count;
(myArray[pivotIndex], myArray[l]) = (myArray[l], myArray[pivotIndex]);

int i2 = l, j = r;
while(i2 < pivotIndex && j > pivotIndex)
{
while(myArray[i2] <= pivot)
{
i2++;
}

while(myArray[j] > pivot)
{
j--;
}

if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2++], myArray[j--]) = (myArray[j--], myArray[i2++]);
}
}

return pivotIndex;
}

void QuickSort(int[] myArray, int l, int r)
{
if (l >= r)
{
return;
}
int p = Partition(myArray, l, r);
QuickSort(myArray, l, p - 1);
QuickSort(myArray, p + 1, r);
}
Console.Write("Provide size of array: ");
int size = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Provide range of array: ");
Console.Write("From: ");
int minSize = Convert.ToInt32(Console.ReadLine());

Console.Write("To: ");
int maxSize = Convert.ToInt32(Console.ReadLine());

int[] tab1 = new int[size];

Console.WriteLine("");
Console.WriteLine("Unsorted array: ");
GenArray(tab1, size, minSize, maxSize);
PrintArray(tab1, size);

Console.WriteLine("");
Console.WriteLine("Sorted array: ");
QuickSort(tab1, 0, size - 1);
PrintArray(tab1, size);

Console.ReadLine();
void GenArray(int[] myArray, int size, int minSize, int maxSize)
{
Random random = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < size; i++)
{
myArray[i] = random.Next(minSize, maxSize + 1);
}
}

void PrintArray(int[] myArray, int size)
{
for (int i = 0; i < size; i++)
{
Console.WriteLine("[" + i + "] " + myArray[i]);
}
}

int Partition(int[] myArray, int l, int r)
{
int pivot = myArray[l];
int count = 0;

for(int i = l + 1; i <= r; i++)
{
if(myArray[i] <= pivot)
{
count++;
}
}

int pivotIndex = l + count;
(myArray[pivotIndex], myArray[l]) = (myArray[l], myArray[pivotIndex]);

int i2 = l, j = r;
while(i2 < pivotIndex && j > pivotIndex)
{
while(myArray[i2] <= pivot)
{
i2++;
}

while(myArray[j] > pivot)
{
j--;
}

if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2++], myArray[j--]) = (myArray[j--], myArray[i2++]);
}
}

return pivotIndex;
}

void QuickSort(int[] myArray, int l, int r)
{
if (l >= r)
{
return;
}
int p = Partition(myArray, l, r);
QuickSort(myArray, l, p - 1);
QuickSort(myArray, p + 1, r);
}
Console.Write("Provide size of array: ");
int size = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Provide range of array: ");
Console.Write("From: ");
int minSize = Convert.ToInt32(Console.ReadLine());

Console.Write("To: ");
int maxSize = Convert.ToInt32(Console.ReadLine());

int[] tab1 = new int[size];

Console.WriteLine("");
Console.WriteLine("Unsorted array: ");
GenArray(tab1, size, minSize, maxSize);
PrintArray(tab1, size);

Console.WriteLine("");
Console.WriteLine("Sorted array: ");
QuickSort(tab1, 0, size - 1);
PrintArray(tab1, size);

Console.ReadLine();
3 Replies
rallez
rallezOP•2y ago
My program doesn't work as I expected and I have no clue where am I wrong. Please help me 😄
Anton
Anton•2y ago
what does it do wrong?
rallez
rallezOP•2y ago
That's my example output
Provide size of array: 10
Provide range of array:
From: 1
To: 5

Unsorted array:
[0] 3
[1] 2
[2] 4
[3] 4
[4] 1
[5] 5
[6] 2
[7] 1
[8] 3
[9] 2

Sorted array:
[0] 2
[1] 4
[2] 2
[3] 2
[4] 3
[5] 4
[6] 3
[7] 3
[8] 3
[9] 4
Provide size of array: 10
Provide range of array:
From: 1
To: 5

Unsorted array:
[0] 3
[1] 2
[2] 4
[3] 4
[4] 1
[5] 5
[6] 2
[7] 1
[8] 3
[9] 2

Sorted array:
[0] 2
[1] 4
[2] 2
[3] 2
[4] 3
[5] 4
[6] 3
[7] 3
[8] 3
[9] 4
It doesn't sorts array and however elements of array after sorting are changed Found solution if someone need:
void GenArray(int[] myArray, int size, int minSize, int maxSize)
{
Random random = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < size; i++)
{
myArray[i] = random.Next(minSize, maxSize + 1);
}
}

void PrintArray(int[] myArray, int size)
{
for (int i = 0; i < size; i++)
{
Console.WriteLine("[" + i + "] " + myArray[i]);
}
}

int Partition(int[] myArray, int l, int r)
{
int pivot = myArray[l];
int count = 0;

for(int i = l + 1; i <= r; i++)
{
if(myArray[i] <= pivot)
{
count++;
}
}

int pivotIndex = l + count;
(myArray[pivotIndex], myArray[l]) = (myArray[l], myArray[pivotIndex]);

int i2 = l, j = r;
while(i2 < pivotIndex && j > pivotIndex)
{
while(myArray[i2] <= pivot)
{
i2++;
}

while(myArray[j] > pivot)
{
j--;
}

if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2], myArray[j]) = (myArray[j], myArray[i2]);
i2++;
j--;
}
}

return pivotIndex;
}

void QuickSort(int[] myArray, int l, int r)
{
if (l >= r)
{
return;
}
int p = Partition(myArray, l, r);
QuickSort(myArray, l, p - 1);
QuickSort(myArray, p + 1, r);
}
Console.Write("Provide size of array: ");
int size = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Provide range of array: ");
Console.Write("From: ");
int minSize = Convert.ToInt32(Console.ReadLine());

Console.Write("To: ");
int maxSize = Convert.ToInt32(Console.ReadLine());

int[] tab1 = new int[size];

Console.WriteLine("");
Console.WriteLine("Unsorted array: ");
GenArray(tab1, size, minSize, maxSize);
PrintArray(tab1, size);

Console.WriteLine("");
Console.WriteLine("Sorted array: ");
QuickSort(tab1, 0, size - 1);
PrintArray(tab1, size);

Console.ReadLine();
void GenArray(int[] myArray, int size, int minSize, int maxSize)
{
Random random = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < size; i++)
{
myArray[i] = random.Next(minSize, maxSize + 1);
}
}

void PrintArray(int[] myArray, int size)
{
for (int i = 0; i < size; i++)
{
Console.WriteLine("[" + i + "] " + myArray[i]);
}
}

int Partition(int[] myArray, int l, int r)
{
int pivot = myArray[l];
int count = 0;

for(int i = l + 1; i <= r; i++)
{
if(myArray[i] <= pivot)
{
count++;
}
}

int pivotIndex = l + count;
(myArray[pivotIndex], myArray[l]) = (myArray[l], myArray[pivotIndex]);

int i2 = l, j = r;
while(i2 < pivotIndex && j > pivotIndex)
{
while(myArray[i2] <= pivot)
{
i2++;
}

while(myArray[j] > pivot)
{
j--;
}

if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2], myArray[j]) = (myArray[j], myArray[i2]);
i2++;
j--;
}
}

return pivotIndex;
}

void QuickSort(int[] myArray, int l, int r)
{
if (l >= r)
{
return;
}
int p = Partition(myArray, l, r);
QuickSort(myArray, l, p - 1);
QuickSort(myArray, p + 1, r);
}
Console.Write("Provide size of array: ");
int size = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Provide range of array: ");
Console.Write("From: ");
int minSize = Convert.ToInt32(Console.ReadLine());

Console.Write("To: ");
int maxSize = Convert.ToInt32(Console.ReadLine());

int[] tab1 = new int[size];

Console.WriteLine("");
Console.WriteLine("Unsorted array: ");
GenArray(tab1, size, minSize, maxSize);
PrintArray(tab1, size);

Console.WriteLine("");
Console.WriteLine("Sorted array: ");
QuickSort(tab1, 0, size - 1);
PrintArray(tab1, size);

Console.ReadLine();
Changed
if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2++], myArray[j--]) = (myArray[j--], myArray[i2++]);
}
if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2++], myArray[j--]) = (myArray[j--], myArray[i2++]);
}
To
if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2], myArray[j]) = (myArray[j], myArray[i2]);
i2++;
j--;
}
if(i2 < pivotIndex && j > pivotIndex)
{
(myArray[i2], myArray[j]) = (myArray[j], myArray[i2]);
i2++;
j--;
}
Want results from more Discord servers?
Add your server