Exemple #1
0
void q_sort(int numbers[], int left, int right)
{
  int pivot, l_hold, r_hold;
 
  l_hold = left;
  r_hold = right;
  pivot = numbers[left];
  while (left < right)
  {
    while ((numbers[right] >= pivot) && (left < right))
      right--;
    if (left != right)
    {
      numbers[left] = numbers[right];
      left++;
    }
    while ((numbers[left] <= pivot) && (left < right))
      left++;
    if (left != right)
    {
      numbers[right] = numbers[left];
      right--;
    }
  }
  numbers[left] = pivot;
  pivot = left;
  left = l_hold;
  right = r_hold;
  if (left < pivot)
    q_sort(numbers, left, pivot-1);
  if (right > pivot)
    q_sort(numbers, pivot+1, right);
}
Exemple #2
0
int *q_sort(int *list, int count){
  int i = 1;
  int j = 0;
 
  if (count <= 1){
    return list;
  }
  else { 
    float crazy = rand()/(RAND_MAX+1.0);
    int rdm = (int)(count*crazy);
    int pivot = list[rdm];
    list[rdm] = list[0];
    list[0] = pivot;
 

    for(j=1;j<count;j++){

      if (pivot>list[j]){
        int temp =list[i];
        list[i]=list[j];
        list[j]=temp;
        i++;
      }
    }
 
  list[0]= list[i-1];
  list[i-1]=pivot;
 
  q_sort(list+i,count-i);
  q_sort(list,i);
  return list;
  }
}
Exemple #3
0
void q_sort(int *arr,int l,int r)
{
    int x,i,j,q,temp;
    if(l<r)
    {
        x=arr[r];
        i=l-1;
        for(j=l; j<r; j++)
        {
            if(arr[j]<=x)
            {
                i++;
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;

            }

        }

        temp=arr[i+1];
        arr[i+1]=arr[j];
        arr[j]=temp;
        q=i+1;
        q_sort(arr,l,q-1);
        q_sort(arr,q+1,r);

    }
}
Exemple #4
0
/* 快速排序法的递归处理 */
void q_sort(FILE *fp,long int left,long int right)
{
	char partition;			//分割元素
	int i,j;

	if(left < right)  //是否继续分割
	{
		i = left;		//分割的最左
		j = right + 1;	//分割的最右
		partition = get_char(fp,left);	//取第一个元素
		do
		{
			do  //从左往右找
			{
				i++;
			}while(get_char(fp,i) < partition);

			do
			{
				j--;
			}while(get_char(fp,j) > partition);

			if(i < j)
				swap_char(fp,i,j);	//交换数据
		
		}while(i < j);

		swap_char(fp,j,left);	//交换数据
		q_sort(fp,left,j-1);    //快速排序递归调用
		q_sort(fp,j+1,right);	//快速排序递归调用
	}

}
 void q_sort(int pivot, int left, int right){
     
     int splitpoint =  0;
     int length     =  right-pivot;
     
     if (left > right) {
         return;
     }
     while (left <= right) {
         while ((my_array[left] <= my_array[pivot]) && (left < (right+1)) ){
             left++;
         }
         while (my_array[right] > my_array[pivot]) {
             right--;
         }
         if(left < right){
             swap(left, right);
         }
         if ((left > right) && (pivot != (left-1))){
             splitpoint = right;
             swap(left-1, pivot);
         }
     }
     
     q_sort(pivot, (pivot+1), right-1);
     q_sort(left, left+1, ((left+(length - (right-pivot)))-1));
 }
Exemple #6
0
void inline q_sort(DATA_TYPE numbers[], int left, int right)
{
	int l_hold, r_hold;
	int temp;
	DATA_TYPE pivot;
	l_hold = left;
	r_hold = right;
	pivot = numbers[left];
	while (left < right)
	{
		while ((numbers[right] >= pivot) && (left < right))
			right--;
		if (left != right)
		{
			numbers[left] = numbers[right];
			left++;
		}
		while ((numbers[left] <= pivot) && (left < right))
			left++;
		if (left != right)
		{
			numbers[right] = numbers[left];
			right--;
		}
	}
	numbers[left] = pivot;
	temp = left;
	left = l_hold;
	right = r_hold;
	if (left < temp)
		q_sort(numbers, left, temp-1);
	if (right > temp)
		q_sort(numbers, temp+1, right);
}
Exemple #7
0
void q_sort(int head, int tail)
{//クイックソートの実装
  int middle;
  if (head > tail) return;
  middle = partiton(head, tail);
  q_sort(head, middle - 1);
  q_sort(middle + 1, tail);
}
void q_sort(int l, int r)
{
    int s;
    if (l < r) {
        s = partition(l, r);
        q_sort(l, s - 1);
        q_sort(s + 1, r);
    }
}
Exemple #9
0
// 算法10.7 P275
// 对顺序表L中的子序列L.r[low..high]作快速排序。
void q_sort(SqList *L,int low,int high,int show){
	int pivot_loc;
	if(low<high){
		//长度大于1
		if(show) pivot_loc = partition(L,low,high);//将L.r[low..high]一分为二
		else pivot_loc = partition_a(L,low,high);//将L.r[low..high]一分为二
		q_sort(L,low,pivot_loc-1,show);//对低子表递归排序,pivot_loc是枢轴位置
		q_sort(L,pivot_loc+1,high,show);//对高子表递归排序
	}
}
Exemple #10
0
void q_sort(LADSPA_Data array[], int left, int right) {
        float pivot = partition(array, left, right);

        if (left < pivot) {
                q_sort(array, left, pivot-1);
        }
        if (right > pivot) {
                q_sort(array, pivot+1, right);
        }
}
Exemple #11
0
void q_sort(section *Array, int left, int right)
{
	if(left >= right)
		return;
	if(right - left <= 8)
		insertion_sort(Array, left, right);

	int i = partition(Array, left, right);
	q_sort(Array, left, i - 1);
	q_sort(Array, i, right);
}
Exemple #12
0
/*快排主程序*/
void q_sort(int *a, int left, int right)
{
	int point;
	if(left < right) //base case
	{
		point = partition(a,left,right); 

		q_sort(a,left,point - 1);
		q_sort(a,point + 1,right);
	}
}
Exemple #13
0
void q_sort (struct alignment* aln, int left, int right)
{
    int pivot, l_hold, r_hold;
    int pivot2;
    l_hold = left;
    r_hold = right;
    pivot2 = aln->nsip[left];
    pivot = aln->sip[left][0];// numbers[left];

    while (left < right)
    {
        while ( (aln->sip[right][0] <= pivot) && (left < right) )
        {
            right--;
        }

        if (left != right)
        {
            aln->sip[left][0] = aln->sip[right][0];
            aln->nsip[left] = aln->nsip[right];
            left++;
        }

        while ( (aln->sip[left][0] >= pivot) && (left < right) )
        {
            left++;
        }

        if (left != right)
        {
            aln->sip[right][0] = aln->sip[left][0];
            aln->nsip[right] = aln->nsip[left];
            right--;
        }
    }

    aln->sip[left][0] = pivot;
    aln->nsip[left] = pivot2;
    pivot = left;
    left = l_hold;
    right = r_hold;

    if (left < pivot)
    {
        q_sort (aln, left, pivot - 1);
    }

    if (right > pivot)
    {
        q_sort (aln, pivot + 1, right);
    }
}
Exemple #14
0
void q_sort(int *sortedfreq, int *sortedocc, long *sortedindex, int *order, int left, int right)
{
  int pivot, l_hold, r_hold;
  int origfreq, origorder, origocc;
  long origindex;

  l_hold = left;
  r_hold = right;
  origfreq = sortedfreq[left]; 
  origorder = order[left]; 
  origocc = sortedocc[left]; 
  origindex = sortedindex[left];
  while (left < right)
  {
    while ((sortedfreq[right] <= origfreq) && (left < right))
      right--;
    if (left != right)
    {
      sortedfreq[left] = sortedfreq[right]; 
      order[left] = order[right];
      sortedocc[left] = sortedocc[right];
      sortedindex[left] = sortedindex[right];
      left++;
    }
    while ((sortedfreq[left] >= origfreq) && (left < right))
      left++;
    if (left != right)
    {
      sortedfreq[right] = sortedfreq[left]; 
      order[right] = order[left];
      sortedocc[right] = sortedocc[left];
      sortedindex[right] = sortedindex[left];
      right--;
    }
  }
  sortedfreq[left] = origfreq;      
  order[left] = origorder;
  sortedocc[left] = origocc;
  sortedindex[left] = origindex;

  pivot= left;
  left = l_hold;
  right = r_hold;
  if (left < pivot)
    q_sort(sortedfreq, sortedocc, sortedindex, order, left, pivot-1);
  if (right > pivot)
    q_sort(sortedfreq, sortedocc, sortedindex, order, pivot+1, right);
}
Exemple #15
0
int main()
{
    int l,i,j;
    long int ans;
    while(1)
    {
        scanf("%d",&l);
        if(l==0) break;
        scanf("%d",&n);
        c[0]=0;
        for(i=1; i<=n; i++) scanf("%d",&c[i]);
        c[n+1]=l;
        q_sort(c,1,n);
        j=0;
        for(i=0; i<=n+1; i++,j++) redir[c[i]]=j;
        for(i=0; i<=n+1; i++)
        {
            for(j=0; j<=n+1; j++) p[redir[c[i]]][j]=MINUS_INFINITY;
        }
        ans=optimal_cut(0,l);
        printf("The minimum cutting is %ld.\n",ans);

    }

    return 0;
}
int main()
{
    int i;
    float start, end, complexity;

    printf("Enter the value of n : ");
    scanf("%d", &n);
    a = calloc(n, sizeof(int));

    for (i = 0; i < n; i++) {
        a[i] = rand() % 20;
    }

    printf("The array is : \n");
    print_array();

    start = clock();
    q_sort(0, n - 1);
    end = clock();

    printf("The sorted array is : \n");
    print_array();

    complexity = (end - start) / CLOCKS_PER_SEC;

    printf("Start time = %f \n", start);
    printf("End time = %f \n", end);
    printf("Complexity = %f \n", complexity);

    return 0;
}
static void
q_sort(int s, int e)
{
	if (s >= e) return;
	int l, r, p, tmp;
	l = s - 1, r = e + 1, p = arr[(s + e) / 2];
	while (1)
	{
		while (arr[++l] < p);
		while (arr[--r] > p);
		if (l >= r) break;
		tmp = arr[l], arr[l] = arr[r], arr[r] = tmp;
	}
	q_sort(s, l - 1);
	q_sort(r + 1, e);
}
Exemple #18
0
int main(int argc, char *argv[])
{
	int nlines; /* number of input lines read */
	int numeric = 0; /* 1 if numeric sort */
	int i;

	for (i = 1; i < argc ; i++) {
		if (*argv[i] == '-') {
			switch (*(argv[i] + 1)) {
				case 'n':
					numeric = 1;
					break;
				case 'r':
					order = 1;
					break;
				case 'f':
					fold = 1;
				default:
					break;
			}
		}
	}

	if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
		q_sort((void**)lineptr, 0, nlines-1,(int(*)(void*, void*))(numeric ? numcmp : (fold ? fstrcmp : strcmp)), order);
		printf("\nResult:\n======\n");
        	writelines(lineptr, nlines);
        	return 0;
	} else {
		printf("error: input too big to sort\n");
		return 1;
	}
}
Exemple #19
0
int main()
{
	int i, time = 0 , max;
	section *a = getArrayFromRand(n);
	printArrayToCons(a, n);

	q_sort(a, 0, n - 1);

	printArrayToCons(a, n);

	i = 0;
	printf("*********************");
	while(i < n)
	{
		max = i;
		while(i + 1 < n && a[i + 1].s <= time)
		{
			if(a[i].f > a[max].f)
				max = i;

			i++;
		}
		
		time = a[max].f;
		printf("\n%i) [%i; %i];", max + 1, a[max].s, a[max].f);

		i++;
	}
	return 0;
}
Exemple #20
0
void sort_headptr(struct llist **headptr, int *sortedfreq, int *sortedocc, long *sortedindex, int ngoodlmers)
{
  int n, h;
  int *order;
  struct llist *tmp;

  void q_sort(int *sortedfreq, int *sortedocc, long *sortedindex, int *order, int left, int right);
  long compute_index (char *this);
  char num_to_char(char z);

  if((order = (int *) malloc(ngoodlmers*sizeof(*order))) == NULL)
  {
    fprintf(stderr,"Out of memory\n");  exit(1);
  } 

  n = 0;
  for(h=0; h<HASH_SIZE; h++)
  {
    tmp = headptr[h];
    while(tmp != NULL)
    {
      sortedocc[n] = tmp->lastplusocc;
      sortedfreq[n] = tmp->freq;
      sortedindex[n] = compute_index(sequence+tmp->lastplusocc);
      n++;
      tmp = tmp->next;
    }
  }

  for(n=0; n<ngoodlmers; n++) order[n] = n;
  if(SORT) {
	q_sort(sortedfreq, sortedocc, sortedindex, order, 0, ngoodlmers - 1);
  }
   
}
Exemple #21
0
/* qsort: sort v[left]...v[right] into increasing/decreasing order */
void q_sort(void *v[], int left, int right, int (*comp)( void *, void *), int order)
{
	int i, last;
	void swap(void *v[], int, int);

	if (left >= right) /* do nothing if array contains */
		return; /* fewer than two elements */
	swap(v, left, (left + right)/2);
	last = left;
	for (i = left+1; i <= right; i++){
		if(!order&&(*comp)(v[i], v[left])<0)
			 swap(v, ++last, i);
		if(order&&(*comp)(v[i], v[left])>0)
			swap(v, ++last, i);
	}
	swap(v, left, last);
	q_sort(v, left, last-1, comp, order);
	q_sort(v, last+1, right, comp, order);
}
Exemple #22
0
void q_sort(VBArrayList* _list, VBArrayListNode* left, VBArrayListNode* right, int li, int ri, VBArrayListSortFunc _func) {
	VBArrayListNode* pivot;
	VBArrayListNode* l_hold = left;
	VBArrayListNode* r_hold = right;
	int lih = li;
	int rih = ri;
	pivot = left;
	while (li < ri)
	{
		while(_func(right->data, pivot->data) && (li < ri)) {
			right = right->prev;
			ri--;
		}
		if (li != ri)
		{
			left->data = right->data;
			
			left = left->next;
			li++;
		}
		while (_func(pivot->data, left->data) && (li < ri)) {
			left = left->next;
			li++;
		}
		if (li != ri)
		{
			right->data = left->data;
			right = right->prev;
			ri--;
		}
	}
	left->data = pivot->data;
	pivot = left;
	
	left = l_hold;
	right = r_hold;
	
	if (lih < li && pivot->prev != NULL)
		q_sort(_list, left, pivot->prev, lih, li - 1, _func);
	if (rih > li && pivot->next != NULL)
		q_sort(_list, pivot->next, right, li + 1, rih, _func);
}
Exemple #23
0
void set<t_el>::q_sort( int l, int r ) {
	int l1, r1;
	if( l < r ) {
		l1 = l;
		r1 = r;
		do {
			while( l1 < r &&
					strcmpi( bs.st[e[l1-1]].name, bs.st[e[l-11]].name ) <= 0 )
				l1++;
			while( l < r1 &&
					strcmpi( bs.st[e[r1-1]].name, bs.st[e[l-1]].name ) >= 0 )
				r1--;
			if( l1 < r1 )
				swap( e[l1-1], e[r1-1] );
		} while( l1 < r1 );
		swap( e[l-1], e[r1-1] );
		q_sort( l, r1-1 );
		q_sort( r1+1, r );
	}
}
Exemple #24
0
void quick_sort(int arr[], int p, int len)
{
    int q;
    
    while (p < len) {
        q = q_sort(arr, p, len);
        quick_sort(arr, p, q-1);
        p = q+1;
    }
        
}
void q_sort(int arr[], int high, int low)
{
    if (high <= low)
        return;
    int p = high;
    int prevhigh = low;
    int idx = low;
    while (idx < high)
    {
        if (arr[idx] < arr[p])
        {
            swap(&arr[prevhigh], &arr[idx]);
            prevhigh++;
        }
        idx++;
    }
    swap(&arr[p], &arr[prevhigh]);
    q_sort(arr, prevhigh - 1, low);
    q_sort(arr, high, prevhigh + 1);

}    
//sort the unsorted nodes and print out
void q_sort(NodeG **unsort,int left,int right) //an array that point to an array-array
{
    if(left>=right)
        return;
    NodeG *temp;
    temp=unsort[left];
    int l=left;
    int r=right;
    while(l<r)
    {
        while(l<r&&strcmp(unsort[r]->name,temp->name)>=0)
            r--;
        unsort[l]=unsort[r];
        while(l<r&&strcmp(unsort[l]->name,temp->name)<=0)
            l++;
        unsort[r]=unsort[l];
    }
    unsort[l]=temp;
    q_sort(unsort,left,l-1);
    q_sort(unsort,l+1,right);
}
void q_sort(int arr[], int left, int right)
{
	int l = left, r = right, temp;
	int pivot = arr[(left + right) / 2];

	do{
		while (pivot > arr[l]) l++;
		while (pivot < arr[r]) r--;

		if (l <= r)
		{
			temp = a[l];
			a[l] = a[r];
			a[r] = temp;
			l++; r--;
		}
	} while (l <= r);

	if(left<r) q_sort(arr, left, r);
	if(l<right) q_sort(arr, l, right);
}
Exemple #28
0
// sort using QuickSort
void Vector::sort()
{
   if(Sorted)
     return;
   if(VectorSize < 2)
     return;

   q_sort(Array, 0, VectorSize - 1);

   ReCount();
   Sorted = true;
}
static
void test_err_q_sort_same_val()
{
    size_t i;
    int32_t ary[] = {0,0,0,0,0};
    int32_t ref[] = {0,0,0,0,0};
    q_sort(ary,ARRAY_SIZE(ary));

    for(i = 0;i < ARRAY_SIZE(ary);i++)
    {
        CU_ASSERT_EQUAL(ref[i],ary[i]);
    }
}
Exemple #30
0
inline void mcodes::q_sort(long *mkeys, char *mvals, int left, int right) {
  long pivot, l_hold, r_hold;
  char pivot_v;
 
  l_hold = left;
  r_hold = right;
  pivot = mkeys[left];
  pivot_v = mvals[left];
    
  while (left < right)
  {    
    while ((mkeys[right] >= pivot) && (left < right))
      right--;
    if (left != right)
    {
      mkeys[left] = mkeys[right];
      mvals[left] = mvals[right];
      left++;
    }
    while ((mkeys[left] <= pivot) && (left < right))
      left++;
    if (left != right)
    {
      mkeys[right] = mkeys[left];
      mvals[right] = mvals[left];
      right--;
    }
  }
  mkeys[left] = pivot;
  mvals[left] = pivot_v;
  pivot = left;
  pivot_v = left;
  left = l_hold;
  right = r_hold;
  if (left < pivot)
    q_sort(mkeys, mvals, left, pivot-1);
  if (right > pivot)
    q_sort(mkeys, mvals, pivot+1, right);
}