The basic idea behind Divide and Conquer Sorting is to divide the data into two smaller groups, sort these, then combine the two small sorted groups into one larger sorted group.
/***************************************/ /* QuickSort() */ /* */ /* Sort records on integer key using */ /* a quick sort. */ /***************************************/ void QuickSort(StructType DataArray[], int startIdx, int endIdx) { int i, j; if(startIdx < endIdx) { i=startIdx; j = endIdx; Partition(DataArray, &i, &j); /* On return i will be greater than j */ QuickSort(DataArray, startIdx, j); QuickSort(DataArray, i, endIdx); } } /***************************************/ /* Partition() */ /* */ /* Partition array into two sections */ /* for quick sorting. */ /***************************************/ void Partition(StructType DataArray[], int *I, int *J) { int Pivot; /* Key data */ StructType temp; Pivot = DataArray[(*I + *J)/2].key; /* Choose an arbidrary pivot point like center of array */ /* Divide the array into < and > Pivot */ do { /* Find leftmost I such that DataArray[I].key > Pivot */ while(DataArray[*I].key < Pivot) (*I)++; /* Find rightmost J such that DataArray[J].key < Pivot */ while(DataArray[*J].key > Pivot) (*J)--; /* If i and J didn't cross over one another, then swap the structs */ if(*I <= *J) { temp = DataArray[*I]; DataArray[*I] = DataArray[*J]; DataArray[*J] = temp; (*I)++; /* Move i one space to the right */ (*J)--; /* Move j one space to the left */ } } while(*I <= *J); }