Quick sort (Ascending Order)

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
void database::quickSortInt(int top,int bottom, char order)
{
      // top = subscript of beginning of array
      // bottom = subscript of end of array
     int middle;

     if (top < bottom)
     {
          middle = partitionInt(top, bottom,order);
          quickSortInt(top,middle,order);   // sort first section
          quickSortInt(middle+1,bottom,order);    // sort second section
     }
     return;
}

int database::partitionInt(int top, int bottom, char order)
{
     int x = indexField[top];
     int i = top - 1;
     int j = bottom + 1;
     int temp;

     if(order == 'd' || order == 'D')
     {
     do
     {
         do      
         {
	      j--;
         }while (x >indexField[j]);

         do  
         {
	      i++;
         }while (x <indexField[i]);

         if (i < j)
         { 
	     temp = indexField[i];    
             indexField[i] = indexField[j];
             indexField[j] = temp;

	     temp = primaryKey[i];
	     primaryKey[i] = primaryKey[j];
	     primaryKey[j] = temp;
         }

    }while (i < j);     
    return j;           // returns middle subscript  
    }
}


I got my Quick Sorting algorithm working but I'm having problems recreating it in ascending order. If anyone could offer any suggestions it would be much appreciated.
I got it to go into descending order but for some reason its returning some random negative huge number. Ex. -3945934 and I'm missing my last index value.

I figure it's because my array is off by one to the left and it's accessing outside the array boundaries however i can't seem to figure out why it is doing so.

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
void database::quickSortInt(int top,int bottom, char order)
{
      // top = subscript of beginning of array
      // bottom = subscript of end of array
     int middle;

     if (top < bottom)
     {
          middle = partitionInt(top, bottom,order);
          quickSortInt(top,middle,order);   // sort first section
          quickSortInt(middle+1,bottom,order);    // sort second section
     }
     return;
}

int database::partitionInt(int top, int bottom, char order)
{
     int x = indexField[top];
     int i = top - 1;
     int j = bottom + 1;
     int temp;

     if(order == 'd' || order == 'D')
     {
     do
     {
         do      
         {
	      j--;
         }while (x <indexField[j]); //just had to reverse

         do  
         {
	      i++;
         }while (x >indexField[i]); // just had to reverse

         if (i < j)
         { 
	     temp = indexField[i];    
             indexField[i] = indexField[j];
             indexField[j] = temp;

	     temp = primaryKey[i];
	     primaryKey[i] = primaryKey[j];
	     primaryKey[j] = temp;
         }

    }while (i < j);     
    return j;           // returns middle subscript  
    }
}


I believe i fixed it my J= was storing something outside of the array because of the bottom+1. I removed it and it fixed as far as i can tell for now. If anyone thinks their is something else wrong just let me know but for now its off to bed.
Last edited on
Topic archived. No new replies allowed.