Пример #1
0
  /* クイックソートを行う */
void QSort(float x[ ], int left, int right)
{
    int i, j;
    int pivot;

    i = left;                      /* ソートする配列の一番小さい要素の添字 */
    j = right;                     /* ソートする配列の一番大きい要素の添字 */

    pivot = x[(left + right) / 2]; /* 基準値を配列の中央付近にとる */

    while (1) {                    /* 無限ループ */

        while (x[i] < pivot)       /* pivot より大きい値が */
            i++;                   /* 出るまで i を増加させる */

        while (pivot < x[j])       /* pivot より小さい値が */
            j--;                   /*  出るまで j を減少させる */
        if (i >= j)                /* i >= j なら */
            break;                 /* 無限ループから抜ける */

        Swap(x, i, j);             /* x[i] と x[j]を交換 */
        i++;                       /* 次のデータ */
        j--;
    }

    if (left < i - 1)              /* 基準値の左に 2 以上要素があれば */
        QSort(x, left, i - 1);     /* 左の配列を Q ソートする */
    if (j + 1 <  right)            /* 基準値の右に 2 以上要素があれば */
        QSort(x, j + 1, right);    /* 右の配列を Q ソートする */
}
void QSort(int *Elements,int NumOfElements, int startIndex, int finalIndex)
{
	if (startIndex >= finalIndex) return;

	int i = startIndex+1, j = finalIndex;

	while (i <= j)
	{
		while (i <= finalIndex && Elements[startIndex] >= Elements[i]) i++;
		while (j > startIndex && Elements[startIndex] <= Elements[j]) j--;

		if (i <= j)
		{
			Swap(Elements, i, j);
		}
	}

	Swap( Elements, startIndex, j);

	QSort(Elements, NumOfElements, startIndex, j - 1);
	QSort(Elements, NumOfElements, i, finalIndex);

	//cprintf("qs,after sorting: start = %d, end = %d\n", startIndex, finalIndex);

}
Пример #3
0
void QSort(ElementType a[], int left, int right)
{
	int i, j;
	ElementType pivot;

	if(right-left+1 > 10)
	{
		pivot = Median3(a, left, right);
		i = left;
		j = right - 1;
		while(1)
		{
			while(a[++i] < pivot);
			while(a[--j] > pivot);
			if(i < j)
				Swap(&a[i], &a[j]);
			else
				break;
		}
		Swap(&a[i], &a[right-1]);
		QSort(a, left, i-1);
		QSort(a, i+1, right);
	}
	else	//对于小数组,直接采用插入排序
		InsertionSort(a+left, right-left+1);
}
Пример #4
0
/* start QuickSort */
void QSort(int x[], int left, int right)
{
	int i, j;
	int pivot;

	i = left;							/* ソートする配列の一番小さい要素の添え字 */
	j = right;							/* ソートする配列の一番大きな要素の添え字 */

	pivot = x[(left+right) / 2];		/* 基準値を配列の中央付近に取る */

	while(1){
		while(x[i] < pivot)				/* pivot より大きい値が出るまで */
			i++;						/* 増加 */

		while (pivot < x[j])			/* pivotより小さい値が出るまで */
			j--;						/* 減少させる */

		if(i >= j)						/* i >= j なら */
			break;						/* 無限ループから出る */

		Swap(x, i, j);					/* x[i] と x[j] を交換 */
		i++;							/* 次のデータ */
		j--;
	}
	ShowData(x, 10);					/* 途中経過を表示 */

	if (left < i-1)						/* 基準値の左に2以上要素があれば */
		QSort(x, left, i-1);			/* 左の配列を Qsort */
	if (j+1 < right)					/* 基準値の右に2以上要素があれば */
		QSort(x, j+1, right);			/* 右の配列を QSort */
}
Пример #5
0
void ByteUtil::QSort(byte *arr, int min, int max) {
	
	if(min >= max - 1)
        return;
    // Initially find a random pivot
    int pivotIndex = min + rand() % (max - min);
    int pivot = arr[pivotIndex];

	byte *begin = arr + min;
	byte *end = arr + (max - 1);

	// While begin != end 
    while(begin != end)
    {
        // Find the lowest bound number to swap
        while(*begin < pivot && begin < end)
            begin++;
        while(*end >= pivot && begin < end)
            end--;

            // Do the swap
        SwapBytes(begin, end);
    }

    // Here begin and end are equal and equal to point from where left side is lower and right side is greater or equal to pivot

    // Partition left
    QSort(arr, min, begin - arr);
    // Partiion right
    QSort(arr, end - arr, max);	
};
Пример #6
0
//void QSort
void QSort(DArray *L, pDArrayCmp cmp, int left, int right)
{
	void *key,*tmp;
	int i = left,j = right;
	key= L->data[left];

	while(i < j)
	{
		while((cmp(key,L->data[j]) <= 1) && (i<j))
		{
			--j;
		}
		L->data[i] = L->data[j];
		
		while((1 <= cmp(key, L->data[i])) && (i<j))
		{
			++i	;
		}

		L->data[j] = L->data[i];
	}
	L->data[i] = key;
	printf("***the middle index is %d\n",i);
	DArrayForeach(L,pDArrayVisit_PrintInt, NULL);
	
	if((i-1) > left)  QSort(L,cmp, left, i-1);
	if(right > (i+1)) QSort(L,cmp, i+1, right);
}
Пример #7
0
 void	MIDITrack::QSort( int left, int right )
 {
   int i,j;
   MIDITimedBigMessage *x, y;
   
   i=left; j=right;
   
   // search for a non NOP message for our median
   
   int pos=(left+right)/2;
   
   for( ;pos<=right;++pos )
   {
     x=GetEventAddress(pos);
     if( x && !x->IsNoOp() ) 
       break;
   }
   if( GetEventAddress( pos )->IsNoOp() )
   {
     for( pos=(left+right)/2; pos>=left; --pos )
     {
       x = GetEventAddress(pos);
       if( x && !x->IsNoOp() )
         break;
     }
   }
   
   if( x && x->IsNoOp() )
     return;
   
   do
   {
     while( MIDITimedMessage::CompareEvents( *GetEventAddress(i), *x ) == 2 &&
            i<right ) ++i;
     
     while( MIDITimedMessage::CompareEvents( *x, *GetEventAddress(j) ) == 2 &&
            j>left ) --j;
     
     if( i<=j )
     {
       y=*GetEventAddress( i );
       *GetEventAddress( i ) = *GetEventAddress( j );
       *GetEventAddress( j ) = y;
       ++i;
       --j;
     }
     
   } while( i<=j );
   
   if( left<j )
   {
     QSort(left,j);
   }
   if( i<right )
   {
     QSort(i,right);
   }
   
 }
Пример #8
0
void QSort(std::vector<T> &vec, int bg, int ed)
{
	if (bg < ed){
		int i = Partition(vec, bg, ed);
		QSort(vec, bg, i - 1);
		QSort(vec, i + 1, ed);
	}
}
Пример #9
0
void QSort(int a[], int low, int high)
{
	if ( low < high ) {
		int pivot = partition(a, low, high);
		QSort(a, low, pivot-1);
		QSort(a, pivot+1, high);
	}
}
Пример #10
0
void QSort(SqList &L, int low, int high) {  //算法10.7
  // 对顺序表L中的子序列L.r[low..high]进行快速排序
  int pivotloc;
  if (low < high) {                      // 长度大于1
    pivotloc = Partition(L, low, high);  // 将L.r[low..high]一分为二
    QSort(L, low, pivotloc-1); // 对低子表递归排序,pivotloc是枢轴位置
    QSort(L, pivotloc+1, high);          // 对高子表递归排序
  }
} // QSort
void QSort(SqList *L, int low, int high)
{
	// 对顺序表L中的子序列L->elem[low...high]做快速排序
	if(low < high){
		int pivotloc = Partition(L, low, high);	// 一分为二
		QSort(L, low, pivotloc-1);
		QSort(L, pivotloc+1, high);
	}
}
Пример #12
0
void QSort(int array[], int low, int high)
{
    if(low <high)
    {
        int pivot = partition(array, low, high); // 分割

        QSort(array, low, pivot-1);
        QSort(array, pivot+1, high);
    }
}
Пример #13
0
void HistoryHeuristic::QSort(ChessMove *source, int low, int high)
{
	int i;
	if (low < high)
	{
		i = Partition(source, low, high);
		QSort(source, low, i - 1);
		QSort(source, i+1, high);
	}
}
Пример #14
0
void QSort(int a[],int low,int high)
{
	int key;
	if(low<high)
	{
		key=partition(a,low,high);
		QSort(a,low,key-1);
		QSort(a,key+1,high);
	}
}
Пример #15
0
 /* bo10-2.c 快速排序的函数,包括算法10.7、10.8 */
 void QSort(SqList *L,int low,int high)
 { /* 对顺序表L中的子序列L.r[low..high]作快速排序。算法10.7 */
   int pivotloc;
   if(low<high)
   { /* 长度大于1 */
     pivotloc=Partition(L,low,high); /* 将L.r[low..high]一分为二 */
     QSort(L,low,pivotloc-1); /* 对低子表递归排序,pivotloc是枢轴位置 */
     QSort(L,pivotloc+1,high); /* 对高子表递归排序 */
   }
 }
Пример #16
0
void QSort(SqList&L,int low,int high){
//一趟对父序列分割为两个无序的子序列,对子序列递归调用自身,必须传进low和high
	//T(n)=O(nlogn),S(n)=O(logn),不稳定
	if(low<high){
		int mid=Partiotion(L,low,high);
		QSort(L,low,mid);
		QSort(L,mid,high);
	}

}
Пример #17
0
void QSort( SqList* l, int low, int high )
{
	if( low >= high )
	{
		return;
	}
	int pivotloc = 0;
	pivotloc = Partition( l, low, high );
	QSort( l, low, pivotloc-1 );
	QSort( l, pivotloc+1, high );
}
Пример #18
0
/* 对顺序表L中的子序列L->r[low..high]作快速排序 */
void QSort(sqList *L,int low,int high)
{
	int pivot;
	
	if(low < high)
	{		
		pivot = Partition(L,low,high);				/* 将L->r[low..high]一分为二 */
													/* 算出枢轴值pivot */
		QSort(L,low,pivot - 1);						/* 对低子表进行排序 */
		QSort(L,pivot + 1,high);					/* 对高子表进行排序 */
	}
}
Пример #19
0
void QSort(int *pint, int Low_Index, int High_Index)
{
	if (Low_Index < High_Index)
	{
		//选一个pv值,把数据分别放在pv值得左右两边,并把pivot位置返回出来。。
		int pivot = Partition(pint, Low_Index, High_Index);

		//对子序列1排序
		QSort(pint, Low_Index, pivot - 1);
		//对子序列2排序
		QSort(pint, pivot + 1, High_Index);
	}
}
Пример #20
0
void PC_TransparencySpec::QSort(ObjPtrList& a, int lo0, int hi0)
{
    int lo = lo0;
    int hi = hi0;

    // Bubble sort if the number of elements is less than 6
    if ((hi-lo) <= 6)
    {
        QBSort(a, lo, hi);
        return;
    }

    // Pick a pivot and move it out of the way /

    ObjectTransDesc* pivot = a[(lo + hi) / 2];
    a[(lo + hi) / 2] = a[hi];
    a[hi] = pivot;

    const double& pivotDist = pivot->objDist;
    while( lo < hi )
    {
        // Search forward from a[lo] until an element is found that * is greater than the pivot or lo >= hi
        while ((lo < hi) && (a[lo]->objDist <= pivotDist))
        {
            lo++;
        }

        // Search backward from a[hi] until element is found that * is less than the pivot, or hi <= lo
        while ((lo < hi) && (pivotDist <= a[hi]->objDist))
        {
            hi--;
        }
        // Swap elements a[lo] and a[hi]
        if( lo < hi )
        {
            ObjectTransDesc* T = a[lo];
            a[lo] = a[hi];
            a[hi] = T;
        }
    }

    // Put the median in the "center" of the list
    a[hi0] = a[hi];
    a[hi] = pivot;
    // Recursive calls, elements a[lo0] to a[lo-1] are less than or * equal to pivot, elements a[hi+1] to a[hi0] are greater than * pivot.
    QSort(a, lo0, lo-1);
    QSort(a, hi+1, hi0);
}
Пример #21
0
int main()
{
    int i,l,r,mid,ans;
    freopen("poj2456.txt","r",stdin);
    freopen("poj2456ans.txt","w",stdout);
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        for (i=1;i<=n;i++) scanf("%d",&pos[i]);
        QSort(1,n);
        l=0;
	r=pos[n]-pos[1]+1;   
        while (l<r)
        {
            mid=(l+r)/2;
            fprintf(stderr,"l=%d,r=%d,mid=%d\n",l,r,mid); 
            if (Check(mid)) 
            {
                ans=mid;
                l=mid+1;
                fprintf(stderr,"now ans : %d\n",ans); 
            }
            else r=mid;
        }
        printf("%d\n",ans);
    }
    return 0;
}
Пример #22
0
int main(void){
	/* ソートする配列 */
	FILE *fp ;
	float x[100] ;
	int i = 0;
	fp = fopen("inputint.txt", "r") ;
	while(fscanf(fp, "%f", &x[i]) != EOF) {
		i++ ;
	}
    int n = i ;
	int k ;

    QSort(x, 0, n - 1);
	printf("homework 3-2 no.1\r\n");
	ShowData(x, n);
	printf("\r\n homework 3-2 no.2\r\n");
	printf("%d", n);
	printf("input: k= ");
	scanf("%d", &k);
	if(0 < k && k <= n){
		ShowData(x, k);
	}
	else{
		printf("error");
	}
	return 0;
}
Пример #23
0
// Fill list of game titles made with selected engine
void UIStartupDialog::FillGameList()
{
	OverrideGameCombo->RemoveAllItems();
	const char* selectedEngine = OverrideEngineCombo->GetSelectionText();

	TArray<const char*> gameNames;
	int numEngineEntries = 0;
	int i;

	for (i = 0; /* empty */; i++)
	{
		const GameInfo &info = GListOfGames[i];
		if (!info.Name) break;
		const char* engine = GetEngineName(info.Enum);
		if (!strcmp(engine, selectedEngine))
		{
			gameNames.Add(info.Name);
			if (!strnicmp(info.Name, "Unreal engine ", 14))
				numEngineEntries++;
		}
	}
	if (gameNames.Num() > numEngineEntries + 1)
	{
		// sort items, keep 1st numEngineEntries items (engine name) in place
		QSort(&gameNames[numEngineEntries], gameNames.Num() - numEngineEntries, CompareStrings);
	}
	for (i = 0; i < gameNames.Num(); i++)
		OverrideGameCombo->AddItem(gameNames[i]);

	// select engine item
	OverrideGameCombo->SelectItem(0);
}
Пример #24
0
  void	MIDITrack::Sort()
  {
//
// A simple single buffer sorting algorithm.
//
// first, see if we need sorting by checking each element
// with the next. they should all be in order.
//
// if not, do qsort algorithm
    
    unsigned int i;
    unsigned int first_out_of_order_item=0;
    
    for( i=0; i<num_events-1; ++i )
    {
      first_out_of_order_item=i+1;
      if( MIDITimedMessage::CompareEvents(
            *GetEventAddress(i),
            *GetEventAddress(first_out_of_order_item)
            )==1 )
        break;
    }
    
    if( first_out_of_order_item>=num_events-1 )
    {
//		return;		// no need for sort
    }
    
    QSort(0,num_events-1);
  }
Пример #25
0
void QuickSort(std::vector<T> &vec)
{
	if (vec.empty()) return;
	srand(unsigned(time(NULL)));
	int idx = rand() % vec.size();
	swap(vec[idx], vec[vec.size() - 1]);
	QSort(vec, 0, vec.size() - 1);
}
Пример #26
0
int main()
{
    char word[20];
    int i,k,nowlen;
    freopen("poj1035.txt","r",stdin);
    freopen("poj1035ans.txt","w",stdout);

    InitHash();

    n=0;
    while (scanf("%s",word) && word[0]!='#')
    {
        strcpy(dict[n].word,word);
        dict[n].id=n;
        dict[n].len=strlen(word);
        dict[n].weight=dict[n].len*100000+n;
        n++;
        Find(word,INSERT);
    }

    QSort(0,n-1);

    nowlen=0;
    memset(startpos,-1,sizeof(startpos));
    for (i=0;i<n;i++)
    {
        if (dict[i].len==nowlen) continue;
        else
        {
            nowlen=dict[i].len;
            startpos[nowlen]=i;
        }
    }
    
    while (scanf("%s",word) && word[0]!='#')
    {
        nowlen=strlen(word);
        cnt=0;
        if (Find(word,QUERY)!=-1) printf("%s is correct\n",word);
        else
        {
            printf("%s:",word);
            CheckLen(word,nowlen);
            if (nowlen==1) CheckLen(word,2);
            else if (nowlen==15) CheckLen(word,14);
            else
            {
                CheckLen(word,nowlen-1);
                CheckLen(word,nowlen+1);
            }
            QSort2(0,cnt-1);
            for (i=0;i<cnt;i++) printf(" %s",ans[i].word);
            printf("\n");
        }
    }
    
    return 0;
}
Пример #27
0
 void QSort(T *sa,
            S *pa,
            int na,
            int (*comp)(const T&, const T&) = gpstk::Qsort_compare)
 {
    int i,j,nr;
    T stemp, spart;
    S ptemp, ppart;
    while(1) { 
       if(na < 8) {                     // use insert sort for small arrays
          insert(sa,pa,na,comp);
          return;
       }
       spart = sa[na/2];                // pick middle element as pivot
       ppart = pa[na/2];
       i = -1; 
       j = na;
       while(1) {
          do {                          // find first element to move right
             i = i + 1;
          } while(comp(sa[i],spart) < 0);
          do {                          // find first element to move left
             j = j - 1;
          } while(comp(sa[j], spart) > 0);
                                        // if the boundaries have met,
                                        // through paritioning,
          if(i >= j) break;
                                        // swap i and j elements
          stemp = sa[i]; ptemp = pa[i];
          sa[i] = sa[j]; pa[i] = pa[j];
          sa[j] = stemp; pa[j] = ptemp;
       }
       nr = na - i;
       if(i < (na/2)) {                 // now sort each partition
          QSort(sa, pa, i, comp);       // sort left side 
          sa = &sa[i];                  // sort right side here
          pa = &pa[i];
          na = nr;                      // memsort(&(sa[i]),nr,comp);
       }
       else { 
          QSort(&(sa[i]), &(pa[i]), nr, comp);    // sort right side
          na = i;
       }
    }
 }  // end QSort
Пример #28
0
int main()
 {
	int data[MaxN];
	int scale;
	InputData(data,&scale);
	QSort(data,0,scale-1);
	OutputData(data,scale);
	return 0;
}
Пример #29
0
void ICVector<T>::QSort(int l, int r){   
  int i = l;
  int j = r;
  T pivot = m_pArray[(l+r)/2]; 
  while (i<=j) {
    while(m_pArray[i]<pivot) i++;
    while(m_pArray[j]>pivot) j--;
    if (i<=j){
      T t = m_pArray[i];
      m_pArray[i] = m_pArray[j];
      m_pArray[j] = t;
      i++;
      j--;
    }
  }
  if (l < j) QSort(l, j);
  if (i < r) QSort(i, r);
}
Пример #30
0
EFI_STATUS
DevicePathConsistMappingSort (
  IN  EFI_DEVICE_PATH_PROTOCOL **DevicePathBuffer,
  IN  UINTN                    DevicePathNum
  )
{
  QSort (DevicePathBuffer, DevicePathNum, sizeof (EFI_DEVICE_PATH_PROTOCOL *), DevicePathConsistMappingCompare);
  return EFI_SUCCESS;
}