quick sorting a dynamic array

no one seems to want to talk about how to work with variable length arrays so im having trouble figuring it out.

i used this code to generate the arrays. the "Dice" values are in the several thousands.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
int* humFight(int humDice)
{
	int* a1=new int[humDice];
	int rolled=0;
	srand (time(NULL));
	while(rolled<humDice)
	{
		a1[rolled]=(rand()%6)+1;
		rolled=rolled++;
	}
	
	int i,j;
    for(i=0;i<sizeof(a1);i++)
    {
        for(j=0;j<i;j++)
		{
            if(a1[i]>a1[j])
            {
                int temp=a1[i];
                a1[i]=a1[j];
                a1[j]=temp;
            }

        }

    }
	
	return a1;
}
int* posFight(int posDice)
{
	int* a2=new int[posDice];
	int rolled=0;
	srand (time(NULL));
	while(rolled<posDice)
	{
		a2[rolled]=(rand()%6)+1;
		rolled=rolled++;
	}
	
	int i,j;
    for(i=0;i<sizeof(a2);i++)
    {
        for(j=0;j<i;j++)
		{
            if(a2[i]>a2[j])
            {
                int temp=a2[i];
                a2[i]=a2[j];
                a2[j]=temp;
            }

        }

    }
	
	return a2;
}

i used this algorithm to make the arrays the same length with 0s at the end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	
if (sizeof(a1)>sizeof(a2))
{
	for(int i=sizeof(a2)+1;i<sizeof(a1);i++)
	{
		a2[i]=0;
	}
}
if (sizeof(a2)>sizeof(a1))
{
	for(int i=sizeof(a1)+1;i<sizeof(a2);i++)
	{
		a1[i]=0;
	}
}

and this is the base quicksort that is built for fixed length arrays

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void quickSort(int arr[], int left, int right) 
{
      int i = left, j = right;
      int tmp;
      int pivot = arr[(left + right) / 2];
 
      /* partition */
      while (i <= j) 
	  {
            while (arr[i] < pivot)
                  i++;
            while (arr[j] > pivot)
                  j--;
            if (i <= j) 
			{
                  tmp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = tmp;
                  i++;
                  j--;
            }
      };
 
      /* recursion */
      if (left < j)
            quickSort(arr, left, j);
      if (i < right)
            quickSort(arr, i, right);
}


if you guys know how to convert the quicksort so it will accept my arrays that would be great. thanks.
Wait, what are you sorting here? If it's numerical, I know that there's a function in the algorithm library that does it for you... Also, in the case of dynamic arrays, you already know the values of humDice and posDice, since they have to be sent specifically to those functions to create arrays... can't you just pass their values over to quickSort as well?
Topic archived. No new replies allowed.