Example #1
0
File: sort.c Project: ChaoMu/myDS
void Qsort(ElementType A[],int Left,int Right)
{
	if(Left+Cutoff<=Right)
	{
		Pivot=Median3(A,Left,Right);
		i = Left;j = Right - 1;
		for(;;)
		{
			while(A[++i]<Pivot)
			{
			}
			while(A[--j]>Pirot) 
			{
			}
			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);
	}
} 
Example #2
0
int Qsort(int *array, int left, int right) {
    int middle_value;
    int i, j;

    if(right <= left)
        return 0;

    middle_value = array[left + rand() % (right - left  + 1)];
    i = left, j = right;
    while(i < j) {
        while(array[i] < middle_value)
            ++i;
        while(array[j] > middle_value)
            --j;
        if(i < j) {
            int temp = array[i];
            array[i] = array[j], array[j] = temp;
            ++i, --j;
        }
    }
    if(array[i] > middle_value)
        --i;
    if(Qsort(array, left, i) != 0)
		return 1;
	if(Qsort(array, i + 1, right) != 0)
		return 1;
    return 0;
}
//快速排序
Qsort(int a[], int low, int high) {
	if (low < high) {
		int pk = Partition(a, low, high);
		Qsort(a, low, pk-1);
		Qsort(a, pk+1, high);
	}
}
Example #4
0
void
Qsort(int A[], int left, int right){
    int i,j;
    int pivot;

    if(left + 10 <= right){
        pivot = Median3(A,left,right);
        i = left;
        j = right - 1;
        for(;;){
            while(A[++i] < pivot){}
            while(A[--j] > pivot){}
            if(i < j){
                int t;
                t = A[i];
                A[i] = A[j];
                A[j] = t;
            }
            else break;
        }
        int t = A[i];
        A[i] = A[right - 1];
        A[right - 1] = t;
        
        Qsort(A,left,i - 1);
        Qsort(A,i + 1,right);
    }
    else
        select_sort(A + left, right - left + 1);
}
Example #5
0
void DynSuffixArray::Qsort(int* array, int begin, int end)
{
  if(end > begin) {
    int index;
    {
      index = begin + (rand() % (end - begin + 1));
      int pivot = array[index];
      {
        int tmp = array[index];
        array[index] = array[end];
        array[end] = tmp;
      }
      for(int i=index=begin; i < end; ++i) {
        if (Compare(array[i], pivot, 20) <= 0) {
          {
            int tmp = array[index];
            array[index] = array[i];
            array[i] = tmp;
            index++;
          }
        }
      }
      {
        int tmp = array[index];
        array[index] = array[end];
        array[end] = tmp;
      }
    }
    Qsort(array, begin, index - 1);
    Qsort(array, index + 1,  end);
  }
}
Example #6
0
void Qsort(ElementType ori[], int left, int right)
{
	int i, j;
	ElementType pivot;
	
	//对于较小的数组,采用插入排序会比快速排序更优,
	//但此处为何是小于等于,而不是大于?
	if(left + CUTOFF <= right){
		pivot = median3(ori, left, right);
		
		i = left;
		j = right - 1;
		for(;;){
			while(ori[++i] < pivot);
			while(ori[--j] > pivot);
			if(i < j){
				swap(&ori[i], &ori[j]);
			}else{
				break;
			}
		}
		swap(&ori[i], &ori[right-1]);
		
		Qsort(ori, left, i-1);
		Qsort(ori, i+1, right);
	}else{
		Insertsort(ori + left, right - left + 1);
	}
}
Example #7
0
        void
        Qsort( ElementType A[ ], int Left, int Right )
        {
            int i, j;
            ElementType Pivot;

/* 1*/      if( Left + Cutoff <= Right )
            {
/* 2*/          Pivot = Median3( A, Left, Right );
/* 3*/          i = Left; j = Right - 1;
/* 4*/          for( ; ; )
                {
/* 5*/              while( A[ ++i ] < Pivot ){ }
/* 6*/              while( A[ --j ] > Pivot ){ }
/* 7*/              if( i < j )
/* job*/                  Swap( &A[ i ], &A[ j ] );
                    else
/* 9*/                  break;
                }
/*10*/          Swap( &A[ i ], &A[ Right - 1 ] );  /* Restore pivot */

/*11*/          Qsort( A, Left, i - 1 );
/*12*/          Qsort( A, i + 1, Right );
            }
            else  /* Do an insertion sort on the subarray */
/*13*/          InsertionSort( A + Left, Right - Left + 1 );
        }
Example #8
0
void Qsort(ElementType A[], int Left, int Right)
{
	int i,j;
	ElementType Pivot;
	
	Pivot = Median3(A, Left, Right);

	if(Left + Cutoff <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
		/*少于3个数据就直接使用插入排序更快*/
		InsertionSort(A+Left, Right-Left+1);
}
Example #9
0
//初级版本
void Qsort(int A[], int left, int right)
{
	if (left < right)
	{
		int i = left;
		int j = right;
		int tmp = A[i];

		while (i < j)
		{
			while (i<j && A[j]>tmp)
				j--;
			if (i < j)
				A[i++] = A[j];

			while (i < j && A[i] < tmp)
				i++;
			if (i < j)
				A[j--] = A[i];
		}
		A[i] = tmp;
		Qsort(A, left, i - 1);
		Qsort(A, i + 1, right);
	}
}
Example #10
0
File: 4.c Project: zyxstar/md_note
void Qsort(SqList *L,int low,int high)    /*Qsort() sub-function */
{  int pivotloc;
   if(low<high)                    /*长度大于1*/
   {  pivotloc=Partition(L,low,high);
      Qsort(L,low,pivotloc-1);
      Qsort(L,pivotloc+1,high);
   }
}
Example #11
0
void Qsort(int Array[], int Low, int High) {
	int Mid = 0;
	if (Low < High) {
		Mid = Partition(Array, Low, High);
		Qsort(Array, Low, Mid - 1);
		Qsort(Array, Mid + 1, High);
	}
}
void Qsort(SqList *L,int low,int high)//递归形式的快速排序算法
{
	int pivotloc;
    if(low<high)
	{
        pivotloc=Partition(L,low,high);
        Qsort(L,low,pivotloc-1);
        Qsort(L,pivotloc+1,high);
	}
}
Example #13
0
/*********************************************
函数名:Qsort()
输入:A[]——需要排序的数组
	  Left——数组左边界下标
	  Right——数组右边界下标
输出:无
功能:快速排序
*********************************************/
void Qsort(int A[],int Left,int Right)
{
	int i;
	if(Left<Right)
	{
		i = Partition(A,Left,Right);
		Qsort(A,Left,i-1);
		Qsort(A,i+1,Right);
	}
}
Example #14
0
File: b.c Project: lucyking/.code
/*-------fx_Qsort-------------*/
int Qsort(int a[],int low,int high){
    if(low<high){
    int axis = partition(a,low,high);   
            //xxxx0,9 is arry's Beginning&End 
    Qsort(a,low,axis-1);
    Qsort(a,axis+1,high);

    }


}
Example #15
0
void Qsort(long *Array, int lb, int ub){
	int pivot=0;
	if (lb>=ub){
		return;
	}
//	if((ub-lb) >10){
//		return;
//	}
	
	pivot = partition(Array, lb, ub);
	Qsort(Array, lb, pivot-1);
	Qsort(Array, pivot+1, ub);
}
Example #16
0
void *ARRAY::GetSortIndex(PGLOBAL g)
  {
  // Prepare non conservative sort with offet values
  Index.Size = Nval * sizeof(int);

  if (!PlgDBalloc(g, NULL, Index))
    goto error;

  Offset.Size = (Nval + 1) * sizeof(int);

  if (!PlgDBalloc(g, NULL, Offset))
    goto error;

  // Call the sort program, it returns the number of distinct values
  Ndif = Qsort(g, Nval);

  if (Ndif < 0)
    goto error;

  if (Ndif < Nval)
    goto error;

  PlgDBfree(Offset);
  return Index.Memp;

 error:
  Nval = Ndif = 0;
  Valblk->Free();
  PlgDBfree(Index);
  PlgDBfree(Offset);
  return NULL;
  } // end of GetSortIndex
Example #17
0
int main()
{
	
	int n=0;
	srand(time(NULL));
	int* mas;
	scanf ("%d",&n);
	printf("\n");
	
	mas = (int*) malloc (n*sizeof(int));
	for (int i = 0; i<n; i++)
	{
		mas[i] = rand()%100;
		printf("%d\n", mas[i]);
	}
	printf ("\n\n\n");
	Qsort( mas, 0, n-1);

	for ( int i = 0; i< n; i++)
	{ 
		printf("%d\n", mas[i]);
	}
	getch();
	return 0;
}
Example #18
0
void QSort(int* ary, int left, int right){
    int l, r;
    if(left >= right){ return; }
    l = left;
    r = right + 1;
    while(1){
        while(l + 1 < right && ary[++l] < ary[left]);
        while(r - 1 >= left && ary[--r] > ary[left]);
        if(l >= r){ break; }
        swap(&ary[l], &ary[r]);
        ++l; --r;
    }
    swap(&ary[r], &ary[left]);

    Qsort(ary, left, r-1);
    Qsort(ary, r+1, right);
}
void
Qsort(int * const a, int left, int right)
{
  int l = left, h = right, m = left;
  int pivot = a[l + rand() % (h-l+1)];
  while( m<=h )
    {
      int x = a[m] - pivot;
      switch( (x>0) - (x<0) )
        {
          case -1: swap(a, l++, m++); break;
          case  0: m++; break;
          case  1: swap(a, m, h--); break;
        }
      Qsort(a, left, l-1);
      Qsort(a, m, right);
    }
}
Example #20
0
void Qsort(char *v[], int left, int right) {

    int i, last;

    if(left >= right)
       return;

    swap(v, left, (left+right)/2);
    last = left;

    for(i = left + 1; i <= right; i++)
      if(strcmp(v[i], v[left]) < 0)
         swap(v, ++last, i);

    swap(v, left, last);
    Qsort(v, left, last-1);
    Qsort(v, last+1, right);
}
Example #21
0
void qs ( msg t[], int start, int end )		//递归实现快排
{
	int mid;
	if ( start < end )
	{
		mid = Qsort(t,start,end);
		qs ( t,start,mid-1);
		qs ( t,mid+1,end);
	}
}
Example #22
0
void Qsort(ElementType A[], int Left, int Right) {
	const int SPACE = sizeof(ElementType) + 2 * sizeof(int);
	Resource_logSpace(SPACE);

	int i, j; //2
	ElementType Pivot; //1

	if (Left + Cutoff <= Right) { //2
		Pivot = Median3(A, Left, Right); //1

		i = Left; //1
		j = Right - 1; //2

		for (;;) {
			while (A[++i] < Pivot) { //4
				Resource_logTime(4);
			}

			while (A[--j] > Pivot) {
				Resource_logTime(4);
			}
			if (i < j) { //1
				Swap(&A[i], &A[j]); //4
				Resource_logTime(5);
			} else {
				Resource_logTime(1);
				break;
			}
		}
		Swap(&A[i], &A[Right - 1]); /* Restore pivot *///5

		Qsort(A, Left, i - 1); //1
		Qsort(A, i + 1, Right); //1
		Resource_logTime(11);
	} else {
		/* Do an insertion sort on the subarray */
		insertionSort(A + Left, Right - Left + 1); // 3
		Resource_logTime(3);
	}
	Resource_logTime(7);
	Resource_logSpace(-SPACE);
}
void QuickSort(SqList *L)
{
    int i,compare,change;
    Qsort(L,1,L->len);
    printf("\n-----快速排序后序列-----\n");
    for(i=1;i<=L->len;i++)
    printf("%d  ",L->r[i].key);
    printf("\n");
    printf("快速排序后比较次数为:%d\n",compare);
    printf("快速排序后移动次数为:%d\n",change*3);
}
void Qsort(vector<int> &list, int low, int high)
{
	int pivot = list[low]; // 设置pivot
	// 进行一次排序
	int low_tmp = low, high_tmp = high;
	while (low_tmp < high_tmp)
	{
		while (low_tmp < high_tmp && list[high_tmp] >= pivot)
			--high_tmp;
		// 小于pivot的数,换到左侧
		list[low_tmp] = list[high_tmp];
		while (low_tmp < high_tmp && list[low_tmp] <= pivot)
			++low_tmp;
		list[high_tmp] = list[low_tmp];
	}
	list[low_tmp] = pivot;
	// 完成一次排序
	Qsort(list, low, low_tmp-1); // 递归处理左子序列
	Qsort(list, low_tmp+1, high); // 递归处理左子序列
}
Example #25
0
int main() {
	int Low, High;
	int Array[] = {6, 7, 3, 8, 10, 9, 1, 4, 5, 2};
	Low = 0;
	High= 9;
	Qsort(Array, Low, High);
	for (int i = 0; i < 10; ++i) {
		printf("%d ", Array[i]);
	}
	return 0;
}
Example #26
0
int main(void) {
    int i;
    int array[] = {7, 3, 4, 1, 2, 9, 10, 4};

    if(Qsort(array, 0, 7) != 0)
			exit(EXIT_FAILURE);

    for(i = 0; i < 8; ++i)
        printf("%d ", array[i]);
	 
	exit(EXIT_SUCCESS);
}
Example #27
0
void Qsort ( int* mas, int low , int high )
{
	int m, i = low, j = high;
	m=mas[(low+high)/2];
	do
	{
		while ( mas[i] < m )
			i++;
		while ( mas[j] > m )
			j--;
		if ( i <= j ) 
		{
			int b = mas[i];
			mas [i] = mas [j];
			mas[j] = b;
			i++;
			j--;
		}
	} while ( i < j );
	if ( j > low ) 
		Qsort ( mas, low ,j );
	if ( i < high ) Qsort ( mas, i , high);
}
Example #28
0
int main()
{
	int i,len;
	int A[] = {2,6,3,8,1,9,4,211,57,489,34,25,13,89,55,32,41,98,40,12,41,21,78,999,56,443,267,23};
	//int A[5] = {3,5,1,4,2};
	len = sizeof(A)/sizeof(int);
	Qsort(A,0,len-1);
	for(i=0; i<len; i++)
	{
		printf("%d ",A[i]);
	}
	printf("\n");
	return 0;
}
Example #29
0
int main(int argc, char *argv[])
{
     int nlines;

     if((nlines = readlines(lineptr, MAXLINES)) >= 0) {
          Qsort(lineptr, 0, nlines-1);
          writelines(lineptr, nlines);
          return 0;
     }
     else {
          printf("error: input too big to sort\n");
          return 1;
     }
}
Example #30
0
int main()
{
	int numbers[]={6,3,4,11,2,14,20,31,21};
	int n = (sizeof(numbers) / sizeof(numbers[0]));
	print(numbers, n);
	Qsort(numbers, n);
	print(numbers, n);

	int idx = Bsearch(numbers, n, 3);
	printf("idx of %d is %d\n", 3, idx);
	idx = Bsearch(numbers, n, 2);
	printf("idx of %d is %d\n", 2, idx);
	idx = Bsearch(numbers, n, 4);
	printf("idx of %d is %d\n", 4, idx);
}