REGARDING MY QUESTION: I had an assignment in school, which i failed, because my code ran too slow. And now i have to fix and study it, because i will have to explain it how i fixed it and how it works.
My question is: What parts of my code can i fix to archive an average performance of O(n^2)? How can i make it run faster?
What is required:
- sort an array of integers in an ascending order,
- the algorithm must have an average performance of O(n^2),
- the
Sort()
method ofarray
must not be used, System.Collections.Generic
or other libraries are not allowed (only the system library is allowed),- strand sort algorithm must be used,
This is what i tried so far, but it works way too slow... I have to sort like 200000 numbers:
EDIT: this code doesn't work proper. I posted an updated version below.
static public int[] StrandSortAscending(int[] p1)
{
int[] p2,
p3;
int p1s1,
p2s1,
p2s2,
p3s1,
p3s2,
n;
p2 = new int[p1.Length];
p3 = new int[p1.Length];
Reset(ref p2);
Reset(ref p3);
p1s1 = p1.Length;
p3s1 = 0;
while (p1s1 != 0)
{
n = int.MinValue;
p2s1 = 0;
for (int i8 = 0; i8 < p1.Length; i8++)
{
if (p1[i8] != int.MaxValue)
{
if (p1[i8] >= n)
{
p2[p2s1] = p1[i8];
n = p1[i8];
p1[i8] = int.MaxValue;
p1s1--;
p2s1++;
}
}
}
int p3p,
zs;
bool pn = false;
for (int i5 = 0; i5 < p2.Length; i5++)
{
p3p = int.MinValue;
for (int i6 = 0; i6 < p3.Length; i6++)
{
if (pn)
{
zs = p3[i6];
p3[i6] = p3p;
p3p = zs;
}
else
{
if (p2[i5] >= p3p && p2[i5] <= p3[i6])
{
p3p = p3[i6];
p3[i6] = p2[i5];
}
}
}
}
Reset(ref p2);
}
return p3;
}
static void Reset(ref int[] a)
{
for (int i = 0; i < a.Length; i++)
a[i] = int.MaxValue;
}
The wikipedia link to the strand sort algorithm: http://en.wikipedia.org/wiki/Strand_sort
NOTE: My biggest mistake with this algorithm was, that i forgot that an array is a reference data type. And 2 arrays pointed to the same one in memory. I spend most of the time figuring out this problem. I was like... What the hell is wrong here... And then it finally clicked. :)
UPDATE (i have done some upgrades):
i got the merging algorithm idea here: How to merge two sorted arrays into a sorted array?
static public int[] StrandSortAscending2(int[] np)
{
int[] up = new int[np.Length]; // sorted array
int[] zp = new int[np.Length]; // temporary array
int[] nnp = new int[np.Length]; // new unsorted array
int[] sp = new int[np.Length]; // merged sorted array
int dvup = 0; // the length of non-empty elements of the sorted array
int dvnp = np.Length; // the length of non-empty elements of the unsorted array
//0. repeat until the unsorted array isn't empty
while (dvnp > 0)
{
//NOTE: reference data type. 2 arrays point to the same array/table in memoty (RAM),
//from the previous cycle, that's why the values in unsorted array (and temp and merged array) were wrong...
zp = new int[np.Length];
nnp = new int[np.Length];
sp = new int[np.Length];
//these counters are needed for knowing till which index of an array are the elements not-empty
//so that i don't need to always increase and decrease the size of an array, but just overwrite the old values
//the algorithm must not be slow, the higher memory usage is not a problem
int dvzp = 0; // the length of non-empty elements of the temporary array
int dvnnp = 0; // the length of non-empty elements of the new unsorted array
//1.1 fill the temporary and the new unsorted array with values
//1.2 the unsorted array should point then to the new unsorted array
int nszp = int.MinValue; // biggest number of the temporary array
for (int inp = 0; inp < dvnp; inp++) // index of unsorted array
{
if (inp == 0 || np[inp] > nszp)
{
nszp = np[inp];
zp[dvzp++] = np[inp];
}
else
{
nnp[dvnnp++] = np[inp];
}
}
np = nnp;
dvnp = dvnnp;
//2. merge temporary and sorted arrays
int izp = 0; // index/counter of temporary array
int iup = 0; // index/counter of sorted array
int isp = 0; // index/counter of merged array
if (dvup > 0)
{
while (izp < dvzp && iup < dvup)
{
if (zp[izp] < up[iup])
sp[isp++] = zp[izp++];
else
sp[isp++] = up[iup++];
}
//if there are still numbers left in the temporary array
//then add then all to the merged array
//they are all bigger then the ones already in the merged array
while (izp < dvzp)
sp[isp++] = zp[izp++];
//do the same for the sorted array
while (iup < dvup)
sp[isp++] = up[iup++];
// dfdfgdgd
up = sp;
dvup = isp;
}
else
{
up = zp;
dvup = dvzp;
}
}
return up;
}
Could i improve the performance even more? So that it would be run even faster?