Ejemplo n.º 1
0
void QuickSort(rde::vector<void *> & SortList, intptr_t L, intptr_t R,
  CompareFunc SCompare)
{
  intptr_t Index;
  do
  {
    Index = L;
    intptr_t J = R;
    void * P = SortList[(L + R) >> 1];
    do
    {
      while (SCompare(SortList[Index], P) < 0)
        Index++;
      while (SCompare(SortList[J], P) > 0)
        J--;
      if (Index <= J)
      {
        if (Index != J)
        {
          void * T = SortList[Index];
          SortList[Index] = SortList[J];
          SortList[J] = T;
        }
        Index--;
        J--;
      }
    }
    while (Index > J);
    if (L < J)
      QuickSort(SortList, L, J, SCompare);
    L = Index;
  }
  while (Index >= R);
}
Ejemplo n.º 2
0
void TList::Sort(CompareFunc Func)
{
  if (GetCount() > 1)
  {
    QuickSort(FList, 0, GetCount() - 1, Func);
  }
}
Ejemplo n.º 3
0
void QuickSort(rde::vector<void *> & SortList, intptr_t L, intptr_t R,
  CompareFunc SCompare)
{
  intptr_t I;
  do
  {
    I = L;
    intptr_t J = R;
    void * P = SortList[(L + R) >> 1];
    do
    {
      while (SCompare(SortList[I], P) < 0)
        I++;
      while (SCompare(SortList[J], P) > 0)
        J--;
      if (I <= J)
      {
        if (I != J)
        {
          void * T = SortList[I];
          SortList[I] = SortList[J];
          SortList[J] = T;
        }
        I--;
        J--;
      }
    } while (I > J);
    if (L < J)
      QuickSort(SortList, L, J, SCompare);
    L = I;
  } while (I >= R);
}
Ejemplo n.º 4
0
void Pedigree::MakeSibships()
   {
   Person ** sibs = new Person * [count];
   for (int i = 0; i < count; i++)
      sibs[i] = persons[i];

   QuickSort(sibs, count, sizeof (Person *),
             COMPAREFUNC Pedigree::CompareParents);

   for (int first = 0; first < count; first++)
      if (!sibs[first]->isFounder())
         {
         int last = first + 1;
         while (last < count)
            if (sibs[first]-> mother != sibs[last]->mother ||
                sibs[first]-> father != sibs[last]->father)
               break;
            else last++;
         last --;

         for (int j = first; j <= last; j++)
            {
            if (sibs[j]->sibCount) delete [] sibs[j]->sibs;
            sibs[j]->sibCount = last - first + 1;
            sibs[j]->sibs = new Person * [sibs[j]->sibCount];
            for (int k = first; k <= last; k++)
               sibs[j]->sibs[k - first] = sibs[k];
            }
         first = last;
         }
   delete [] sibs;
   }
Ejemplo n.º 5
0
void QuickSort(int* arr, size_t size) {
    // Complexity T(n) = O(n*log(n))
    if (size <= 1)
        return;

    // amazing not naive partition implementation
    int key = rand() % size, i = 1; // i points on pivot element
    swap(arr[0], arr[key]);
    for (size_t j = 1; j < size; j++) { // j points on unsorted element
        if (arr[j] < arr[0])
            swap(arr[i++], arr[j]);
    }
    swap(arr[0], arr[i-1]);

    QuickSort(arr, i-1);
    QuickSort(arr + i, size - i);
}
Ejemplo n.º 6
0
/* This function does the quicksort
   Arguments :
             array - the array to be sorted
             startIndex - index of the first element of the section
             endIndex - index of the last element of the section
   */
void QuickSort(int* array, int startIndex, int endIndex)
{
    int pivot = array[startIndex];                    //pivot element is the leftmost element
    int splitPoint;
    
    if(endIndex > startIndex)                         //if they are equal, it means there is
                                                      //only one element and quicksort's job
                                                      //here is finished
    {
        splitPoint = SplitArray(array, pivot, startIndex, endIndex);
                                                      //SplitArray() returns the position where
                                                      //pivot belongs to
        array[splitPoint] = pivot;
        QuickSort(array, startIndex, splitPoint-1);   //Quick sort first half
        QuickSort(array, splitPoint+1, endIndex);     //Quick sort second half
    }
}
Ejemplo n.º 7
0
void QuickSort(int arr[], int left, int right) {
    int pivot = Median3forQuickSort(arr, left, right);
    int* l = &arr[0];
    int* r = &arr[N-1];
    while(1) {
        while (arr[++l] < pivot) {;}
        while (arr[--r] > pivot) {;}
        if (l < r) {
            swap(&arr[l], &arr[r]);
        } else {
            break;
        }
    }
    swap(&arr[l], &arr[right-1])
    QuickSort(arr, left, l-1);
    QuickSort(arr, l+1, right);
}
Ejemplo n.º 8
0
void QuickSort(int a[], int left, int right)
{
        if(left < right)
        {
                int i = left, j = right, p = a[i];
                while(i < j)
                {
                        while(i < j && p < a[j]) j--;
                        if(i < j) a[i++] = a[j];
                        while(i < j && p > a[i]) i++;
                        if(i < j) a[j--] = a[i];
                }
                a[i] = p;
                QuickSort(a, left, i-1);
                QuickSort(a, i+1, right);
        }
}
Ejemplo n.º 9
0
void QuickSort(void** Table, int Size, CompCallback Callback) {
	void* Pivot = NULL;
	int PivotIdx = 0;

    //A table consisting of one element is always sorted.
    if(Size <= 1)
        return;
    /**
     * Find the pivot, then put all elements that are less than the pivot on the left of its
     * and all elements that are greater on the right of the pivot, then subdivde the table
     * in to two and repeat.
     */
    Pivot = MedianPivot(Table, Size);
    PivotIdx = QuickSortPartition(Table, Size, Pivot, Callback);
    QuickSort(Table, Size - (Size - PivotIdx), Callback);
    QuickSort(Table + PivotIdx, (Size - PivotIdx), Callback);
}
Ejemplo n.º 10
0
/*Quicksort - to sort jobs */
int QuickSort(Jobs A[],int low, int high,int k)
{
	int pivot;
	int n;
	n = high-low+1;

	if(n > 1)
	{
		pivot = partition(A,low,high,k);
		if(low < pivot-1)
		QuickSort(A,low,pivot-1,k);
		if(pivot+1 < high)
		QuickSort(A,pivot+1,high,k);	
	}

	return 0;
}
Ejemplo n.º 11
0
void main()
{
	int dat[] = { 9,2,65,3,5,2,8,6,10,4 };
	QuickSort(dat, 0, 9);
	for (int i = 0; i < 10; i++)
		std::cout << dat[i] << std::endl;
	getchar();
}
Ejemplo n.º 12
0
int Q_Sort(queue *q, int (*Comp)(const void *, const void *))
{
   int         i;
   void        *d;
   datanode    *dn;

   /* if already sorted free memory for tag array */

   if(q->sorted) {
      efree(queue_index);
      efree(queue_posn_index);
      q->sorted = False_;
   }

   /* Now allocate memory of array, array of pointers */

   queue_index = emalloc(q->size * sizeof(q->cursor->data));
   if(queue_index == NULL)
      return False_;

   queue_posn_index = emalloc(q->size * sizeof(q->cursor));
   if(queue_posn_index == NULL) {
      efree(queue_index);
      return False_;
   }

   /* Walk queue putting pointers into array */

   d = Q_Head(q);
   for(i=0; i < q->size; i++) {
      queue_index[i] = d;
      queue_posn_index[i] = q->cursor;
      d = Q_Next(q);
   }

   /* Now sort the index */

   QuickSort(queue_index, 0, q->size - 1, Comp);

   /* Rearrange the actual queue into correct order */

   dn = q->head;
   i = 0;
   while(dn != NULL) {
      dn->data = queue_index[i++];
      dn = dn->next;
   }

   /* Re-position to original element */

   if(d != NULL)
      Q_Find(q, d, Comp);
   else  Q_Head(q);

   q->sorted = True_;

   return True_;
}
void QuickSort(int *pnArr, int nLeft, int nRight)
{
    if (nLeft < nRight)
    {
        if (nRight - nLeft > K)
        {
            int nTmpPos = RandomPartition(pnArr, nLeft, nRight);

            QuickSort(pnArr, nLeft, nTmpPos - 1);
            QuickSort(pnArr, nTmpPos + 1, nRight);
        }
        else
        {
            InsertSort(pnArr, nLeft, nRight);
        }
        
    }
}
Ejemplo n.º 14
0
void TStringList::CustomSort(TStringListSortCompare ACompareFunc)
{
  if (!GetSorted() && (GetCount() > 1))
  {
    Changing();
    QuickSort(0, GetCount() - 1, ACompareFunc);
    Changed();
  }
}
Ejemplo n.º 15
0
int main(){

int v[TAM] = {7, 13, 5, 8, 9, 2, 3};	

QuickSort(v, 0, TAM-1);
ImprimeVetor(v, TAM);
	
return(0);
}
Ejemplo n.º 16
0
// 퀵 소트
void CRankDlg::QuickSort(int first, int last)
{
    int pivot, left, right ;

     if (first < last) {
         pivot =(last +first) /2 ; 
		 left =first ; right =last ;
         while (left < right) {
			 while (_ttoi (rankList.GetItemText (left, 0)) <= _ttoi (rankList.GetItemText (pivot, 0)) && left < last) left++ ;
             while (_ttoi (rankList.GetItemText (right, 0)) > _ttoi (rankList.GetItemText (pivot, 0))) right-- ;
             if (left < right) SwapItem (left, right) ;	// 교환 
         }
		 SwapItem (pivot, right) ;
     
         QuickSort (first, right -1) ;
         QuickSort (right +1, last) ;
    }
}
Ejemplo n.º 17
0
// 3. sort the word list according to the word frequency (P)
errorType SortWordForBook(bookType* book)
{
 wordFreq* wf = NULL;

   wf = book->wordList;
   QuickSort(wf, NULL);

   return ERROR_NONE;
}
Ejemplo n.º 18
0
int main() {
    int A[] = {10,23,3,42,55,7,7,8,9,12};
    QuickSort(A,0,9);
    int i = 0;
    for(; i < 10; i ++) {
        printf("%d ",A[i]); 
    }
    return 0;
}
Ejemplo n.º 19
0
// add rubi data and sort the data
BOOL scRubiArray::AddRubiData( scRubiData& rd )
{
	if ( IsRubiData( rd.fStartOffset, rd.fEndOffset ) )
		return false;
	
	AppendData( (ElementPtr)&rd );
	QuickSort( rubi_sort );
	return true;
}
Ejemplo n.º 20
0
int main()
{
    int array[] = {7,6,5,3,4,2,1,10,9,8};
    int len = sizeof(array) / sizeof(int);
    PrintArray(array,len,"快速排序前:");
    QuickSort(array,0,len - 1);
    PrintArray(array,len,"快速排序后:");
    return 0;
}
Ejemplo n.º 21
0
main() {
	int n = 0, a;
	while(scanf("%d", &Data[n]) == 1)
		n++;
	QuickSort(0, n-1);
	for(a = 0; a < n; a++)
		printf("%d ", Data[a]);
	return 0;
}
Ejemplo n.º 22
0
void TClist::Sort()
{
    int inicio = SDL_GetTicks();
    if (numPalabras > 0)
    {
        QuickSort(CList, numPalabras-1, 0, numPalabras-1);
        printf("Objetos ordenados en: %d\n",(SDL_GetTicks()-inicio));
    }
}
Ejemplo n.º 23
0
void QuickSort(int e[], int first, int end)
{
	int i=first,j=end,temp=e[first];
	while(i<j)
  {
		while(i<j && e[j]>=temp)
			j--;
		e[i]=e[j];
		while(i<j && e[i]<=temp)
			i++;		
		e[j]=e[i];
  }
	e[i]=temp;
  if(first<i-1)
  	QuickSort(e,first,i-1);
	if(end>i+1)		
		QuickSort(e,i+1,end);
}
Ejemplo n.º 24
0
/*
* This function is used to find the median of medians the given array
*
* @param arrayToSort input as reference array to sort
* @param startOfArray start of the array to sort
* @param endOfArray end of the array to sort
* @return int median value found
*/
int MedianOfMedians(int arrayToSort[], int startOfArray, int endOfArray)
{
 	int ii = 0;

	//get the number of groups.. this will ignore the last array of size < 5
	int numGroups = (int)floor(((endOfArray - startOfArray) - 1) / 5);
	int *medianArray = (int*)malloc((numGroups + 1) * sizeof(int));
	int *tempArray = (int*)malloc(5 * sizeof(int));

	memset(medianArray, 0, (numGroups + 1) * sizeof(int));
	memset(tempArray, 0, (5 * sizeof(int)));

	for (ii = 0; ii <= numGroups; ii++)
	{
		int kk = 0;

		for (int jj = startOfArray + (ii * 5); jj < (startOfArray + (ii * 5 + 5)); jj++)
		{
			tempArray[kk] = arrayToSort[jj];
			kk++;
		}

		//if our element total is less than 5, we cant sort the whole 5 elements
		//either way we get the middle element
		if ((endOfArray - startOfArray) < 5)
		{
			QuickSort(tempArray, 0, (endOfArray - startOfArray));
			int index = (int)floor((endOfArray - startOfArray) / 2);
			medianArray[ii] = tempArray[index];
		}
		else
		{
			QuickSort(tempArray, 0, 4);
			medianArray[ii] = tempArray[2];
		}
	}

	//sort of the median array generated from the sub groups
	QuickSort(medianArray, 0, numGroups - 1);

	//return middle element of sorted array
	int medianIndex = (int)floor(numGroups / 2);
	return medianArray[medianIndex];
}
Ejemplo n.º 25
0
void main(void)
{
   int i, y[16] = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 30};
   //int i, y[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,30};
   int n = 15;
   QuickSort(y,n);
   for (i = 0; i < n; i++)
      cout << y[i] << ' ';
   cout << endl;
}
Ejemplo n.º 26
0
void main()
{
    srand((int)time(NULL));
    int A[]={6,3,4,2,1,5,8,7,9,0};
    int len=sizeof(A)/4;
    QuickSort(A,0,len-1);
    int i;
    for(i=0;i<len;i++)
        printf("%d,",A[i]);
}
Ejemplo n.º 27
0
int main(){

int v[TAM] = {2, 5, 32, 21, 102, 1, 11, 24, 35, 44, 56};	

QuickSort(v, 0, TAM-1);
ImprimeVetor(v, TAM);
	printf("Comparações: %d\nTrocas: %d\n", comparacoes, troca);
	
return(0);
}
Ejemplo n.º 28
0
void TestQuickSort()
{
	int array[] = {2,5,6,9,1,3,4,7,0,8};
	QuickSort(array,0,sizeof(array)/sizeof(array[0])-1);
	for(int i = 0; i<sizeof(array)/sizeof(array[0]); i++)
	{
		cout<<array[i] <<" " ;
	}
	cout <<endl;
}
Ejemplo n.º 29
0
int main()
{
    int a[]={19,25,13,10,2,5,1,8,7,9};
    int len=sizeof(a)/sizeof(a[0]);
    QuickSort(a,0,len-1);
    int sum;
    printf("Please input the sum:");
    scanf("%d",&sum);
    Judge(a,len,sum);
}
Ejemplo n.º 30
0
static void QuickSort (Collection* C, int Lo, int Hi,
                       int (*Compare) (void*, const void*, const void*),
                       void* Data)
/* Internal recursive sort function. */
{
    /* Get a pointer to the items */
    void** Items = C->Items;

    /* Quicksort */
    while (Hi > Lo) {
        int I = Lo + 1;
        int J = Hi;
        while (I <= J) {
            while (I <= J && Compare (Data, Items[Lo], Items[I]) >= 0) {
                ++I;
            }
            while (I <= J && Compare (Data, Items[Lo], Items[J]) < 0) {
                --J;
            }
            if (I <= J) {
                /* Swap I and J */
                void* Tmp = Items[I];
                Items[I]  = Items[J];
                Items[J]  = Tmp;
                ++I;
                --J;
            }
        }
        if (J != Lo) {
            /* Swap J and Lo */
            void* Tmp = Items[J];
            Items[J]  = Items[Lo];
            Items[Lo] = Tmp;
        }
        if (J > (Hi + Lo) / 2) {
            QuickSort (C, J + 1, Hi, Compare, Data);
            Hi = J - 1;
        } else {
            QuickSort (C, Lo, J - 1, Compare, Data);
            Lo = J + 1;
        }
    }
}