Exemplo n.º 1
0
void InsertionSortTest()
{
    std::vector<int> ivec {5, 2, 4, 6, 1, 3};
    InsertionSort(ivec.begin(), ivec.end(), std::less<int>());

    int best_worst[] {0, 1, 2, 3, 4, 5};
    InsertionSort(std::begin(best_worst), std::end(best_worst), std::less<int>());
    InsertionSort(std::begin(best_worst), std::end(best_worst), std::greater<int>());
}
Exemplo n.º 2
0
// Compare hands if their values are the same.
// Returns true if hand1 > hand2
bool CompareHands(struct card hand1[HAND_SIZE], struct card hand2[HAND_SIZE], int points)
{
	InsertionSort(hand1);
	InsertionSort(hand2);

	if (points == 1 || points == 2) {									// One pair, two pairs
		return hand2[HighestCard(hand2)] < hand1[HighestCard(hand1)];
	}
	else if (points == 3 || points == 6 || points == 7) {                // 3 of a kind, full house, 4 of a kind
		return hand2[2] < hand1[2];					                   
	}	
	else {                                                               // Straight flush, straight, flush, high card, royal
		return (hand2[HAND_SIZE-1] < hand1[HAND_SIZE-1]);
	}
}
Exemplo n.º 3
0
int main(void)
{
	int *array, size;
	
	printf("Enter the size of array: ");
	scanf("%d", &size);
	
	array = (int *)malloc(size * sizeof(int));
	
	printf("Input\n");
	
	for(int i = 0; i < size; i++)
	{
		printf("%d: ", i + 1);
		scanf("%d", &array[i]);
	}
	
	InsertionSort(array, size);
	
	printf("Ouput\n");
	
	for(int i = 0; i < size; i++)
		printf("%d\n", array[i]);
	
	free(array);
		
	return 0;
}
Exemplo n.º 4
0
/*快速选择,选择完成之后,要求的第K小的数在数组下标为k-1上*/
void QSelect(ElementType A[], int k, 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]);

		/*k=i+1的时候,即是i=k-1的时候,代表枢纽元就已经是要求的结果了,
		正好处在k-1上,这里不使用k-1进行判断是为了避免k=0的情况*/
		if(k <=i)
			QSelect(A, k, Left, i-1);
		else if(k>i+1)
			QSelect(A, k, i+1, Right);
	}
	else/*少于3个数据就直接使用插入排序更快*/
		InsertionSort(A+Left, Right-Left+1);
}
Exemplo n.º 5
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);
}
Exemplo n.º 6
0
void ArraySorter::QuickSort2(int *arr, int left, int right)
{
	if((left + 10) <= right)
	{
		int pivot = Median3(arr, left, right);
		int i = left, j = right-1;
		for(;;)
		{
			while(arr[++i] < pivot) 
			{
				// Nada mucho
			}
			while(pivot < arr[--j]) 
			{
				// Empty
			}
			
			if( i < j )
			{
				swap(arr, i, j);
			}
			else
			{
				break;
			}
		}
		swap(arr, i, right-1);
		QuickSort2(arr, left, i-1);
		QuickSort2(arr, i+1, right);
	}
	else //Run insertion sort
	{
		InsertionSort(arr, right+1, left, 0);
	}
}
Exemplo n.º 7
0
void main()
{
	TELNO ContactArray[10];
	int iIndex =0;
	int ichoice =1;
	while(ichoice !=0)
	{
		system("cls");
		printf("1.Input\n");
		printf("2.Output\n");
		//printf("3.Save File\n");
		//printf("4.Read File\n");
		scanf("%d", &ichoice);
		fflush(stdin);
		switch(ichoice)
		{
		case 1:
			InputContact(ContactArray,&iIndex,sizeof(ContactArray)/sizeof(ContactArray[0]));
			break;
		case 2:
			OutputContact(ContactArray,iIndex);
			break;
		case  3:
			//FileSave(ContactArray,iIndex);
			InsertionSort(ContactArray, iIndex);
			OutputContact(ContactArray,iIndex);
			break;
		case  4: 
			//ReadFile(ContactArray,iIndex);
			break;
		getch();

		}
	}
}
int main(int argc, char* agrgv)
{
    int A[100];
    int N;
    int i, j;

    printf("How Many Number: ");
    scanf("%d", &N);

    for(i=1; i<=N; i++)
    {
        scanf("%d", &A[i]);
    }


    InsertionSort(A,N);

    printf("The Sorted Data:\n\n");

    for(i=1; i<=N; i++)
    {
        printf("%d   ", A[i]);
    }

    return 0;
}
Exemplo n.º 9
0
//快速排序  
void QuickSort(int A[],int Left,int Right)  
{  
	int i,j;  
	int Pivot;  
	const int Cutoff = 3;  
	if (Left + Cutoff <= Right)  
	{  
		Pivot = Median3(A,Left,Right);  
		i = Left;  
		j = Right - 1;  
		while (1)  
		{  
			while(A[++i] < Pivot){;}  
			while(A[--j] > Pivot){;}  
			if (i < j)  
				swap_int(&A[i],&A[j]);  
			else  
				break;  
		}  
		swap_int(&A[i],&A[Right - 1]);   

		QuickSort(A,Left,i - 1);  
		QuickSort(A,i + 1,Right);  
	}  
	else  
	{  
		InsertionSort(A+Left,Right - Left + 1);  
	}  
}  
int main(){
	//variable declaration
	int *arr, i, arrSize;
	printf("Enter Array Size: ");
	scanf("%d",&arrSize);
	arr=(int *)malloc(arrSize*sizeof(int));
	if(arr)
	{
		printf("Enter the %d elements of the array:\n",arrSize);
		//input
		for(i = 0; i < arrSize; i++)
		{
			scanf("%d", &arr[i]);
		}
		//sort
		InsertionSort(arr, arrSize);	//passing arr address and no. of elements
		printf("Elements After sorting: ");
		//output
		for(i = 0; i < arrSize; i++)
		{
			printf("%d  ", arr[i]);
		}
	}
	return 0;
}
Exemplo n.º 11
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 );
        }
Exemplo n.º 12
0
/* 
 * SortArgumentList: Sort the given list in place, and return it.
 *   args must be a list of arg_type; that is, a list of arguments to a 
 *   function call.
 */
list_type SortArgumentList(list_type args)
{
   list_type ptr, prev;
   arg_type arg;

   /* Only sort the settings--leave other arguments as they are */
   prev = NULL;
   for (ptr = args; ptr != NULL; prev = ptr, ptr = ptr->next)
   {
      arg = (arg_type) ptr->data;
      if (arg->type == ARG_SETTING)
	 break;
   }

   /* If no settings, don't sort */
   if (ptr == NULL)
      return args;

   /* Sort settings and reattach to end of list */
   ptr = InsertionSort(ptr, CompareArguments);

   if (prev != NULL)
      prev->next = ptr;
   else args = ptr;

   /* Fix up "last" pointer */
   args->last = ptr->last;
      
   return args;
}
Exemplo n.º 13
0
void ShellSort(int *A, int N) {
    int k, h = 1;
    while(h < N)
        h = 3 * h + 1;
    h /= 3;
    #pragma omp parallel firstprivate(h)
    {
        while(h != 1) {
            #pragma omp for
            for(k = 0; k < h; k++) {
                int i, j, v;
                for(i = k; i < N; i += h) {
                    v = A[i];
                    j = i;
                    while(A[j-h] > v) {
                        A[j] = A[j-h];
                        j -= h;
                        if(j <= h) break;
                    }
                    A[j] = v;
                }
            }
            h /= 3;
        }
    }
    InsertionSort(A, N);
}
Exemplo n.º 14
0
  // draws all stored jsprites then clears it.
  void JSpriteRenderer::DrawBatch( bool sort )
  {
    shader.BindProgram();
    glActiveTexture( GL_TEXTURE0 );

    //std::sort( objects.begin(), objects.end() );
    if ( sort )
      InsertionSort(jsprites);

    int size = jsprites.size();
    for ( int i = 0; i < size; ++i )
    {
      glBindTexture( GL_TEXTURE_2D, jsprites[i]->GetTexture() );
      shader.uniform( "Texture", 0 );

      shader.uniform( "alpha", jsprites[i]->GetAlpha() );
      float tc[3] = { jsprites[i]->GetColor().x, jsprites[i]->GetColor().y, jsprites[i]->GetColor().z };
      shader.uniform<3>( "Color", tc );

      //draw
      jsprites[i]->Draw();
    }

    jsprites.clear();


    glBindTexture( GL_TEXTURE_2D, 0 );
    shader.UnBindProgram();
  }
Exemplo n.º 15
0
int main()
{
    long int n,i,position,median;
    indx=0;
    while(scanf("%ld",&n)==1)
    {
        if(n==0)
            break;
        median=0;
        indx=indx+1;
        a[indx]=n;
        InsertionSort();
        if(indx%2==0)
        {
            position=indx/2;
            median=(a[position]+a[position+1])/2;
        }
        else
        {
            position=(indx+1)/2;
            median=a[position];
        }
        printf("%ld\n",median);
    }
    return 0;
}
Exemplo n.º 16
0
void main()
{
	int arr[SIZE],i;

	srand(time(NULL));

	for(i=0;i<SIZE;i++)
	{
		arr[i] = rand()/1000 + 100;
	}

	printf("排序前:\n");
	for(i=0;i<SIZE;i++)
	{
		printf("%d ",arr[i]);
	}
	printf("\n");

	InsertionSort(arr,SIZE);

	printf("排序后:\n");
	for(i=0;i<SIZE;i++)
	{
		printf("%d ",arr[i]);
	}
	printf("\n");
}
Exemplo n.º 17
0
// Project d-dimensional vector x onto the set
// { u in R^d | u_i <= u_d, for all i < d }.
// Argument x is overwritten with the result of the projection.
// The algorithm has complexity O(d log d) and requires a sort. For more
// information, see Algorithm 10, p. 120, in:
//
// The Geometry of Constrained Structured Prediction: Applications to
// Inference and Learning of Natural Language Syntax.
// Andre Martins.
// PhD Thesis.
//
// This is the cached version of that algorithm.
// y is a cached version of projection computed in the previous call, sorted,
// so that we have a hint for the ordering.
int project_onto_cone_cached(double* x, int d,
                             vector<pair<double, int> >& y) {
  int j;
  double s = 0.0;
  double yav = 0.0;

  if (y.size() != d) {
    y.resize(d);
    for (j = 0; j < d; j++) {
      y[j].first = x[j];
      y[j].second = j;
    }
  } else {
    for (j = 0; j < d; j++) {
      if (y[j].second == d - 1 && j != d - 1) {
        y[j].second = y[d - 1].second;
        y[d - 1].second = d - 1;
      }
      y[j].first = x[y[j].second];
    }
  }
  InsertionSort(&y[0], d - 1);

  for (j = d - 1; j >= 0; j--) {
    s += y[j].first;
    yav = s / ((double)(d - j));
    if (j == 0 || yav >= y[j - 1].first) break;
  }

  for (; j < d; j++) {
    x[y[j].second] = yav;
  }

  return 0;
}
Exemplo n.º 18
0
int main()
{
    // input file, with ios flag set to in
    std::fstream file("rosalind_ins.txt", std::ios::in);
    // strings to hold the size and array
    std::string size, arr;

    // if the file can be opened
    if (file.good())
    {
        // get the size and array from the file
        std::getline(file, size);
        std::getline(file, arr);
    }

    // convert string to int using stoi()
    int s = std::stoi(size);
    // create a dynamically allocated array with the s
    int *a = new int[s];
    // call Split on the string and convert to int array
    Split(arr, a);

    std::cout << "============\n";
    std::cout << InsertionSort(a, s) << std::endl;
    std::cout << "============\n";

    return 0;
}
Exemplo n.º 19
0
Arquivo: sort.c Projeto: 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);
	}
} 
Exemplo n.º 20
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);
}
Exemplo n.º 21
0
void QuickSort(int a[],int Left,int Right)
{
	int i;
	int j;
    int k;
	int Center;
	const int Cutoff = 3;
	if (Left + Cutoff <= Right)
	{
		Center = Median3(a,Left,Right);
		i = Left;
		j = Right-1;
		while (1)//for()
		{
			while(a[++i] < Center){;}//下标右移
			while(a[--j] > Center){;}//下标左移
			if (i < j)
                swap_int(&a[i],&a[j]);
			else
				break;
		}
		swap_int(&a[i],&a[Right-1]);
		QuickSort(a,Left,i-1);
		QuickSort(a,i+1,Right);
	}
	else
	{
		InsertionSort(a+Left,Right-Left+1);
	}
}
Exemplo n.º 22
0
int main(int argc, char* argv[])
{
	int A[] = { 12, 19, 1, 10, 14, 16, 4, 7, 9, 3, 2, 8, 5 };
	print(A, sizeof(A) / sizeof(int));
	InsertionSort(A, sizeof(A) / sizeof(int));
	print(A, sizeof(A) / sizeof(int));
	return 0;
}
Exemplo n.º 23
0
int main() {
  int seq[10] = {4, 6, 9, 1, 3, 5, 2, 8, 7, 0};
  InsertionSort(seq, 10);
  for(int i=0; i<10; i++) {
    printf("%d\n", seq[i]);
  }
  return 0;
}
Exemplo n.º 24
0
int main()
{
	char str[] = "17452198";
	InsertionSort(str);
	printf("%s\n", str);
	system("pause");
	return 0;
}
Exemplo n.º 25
0
// Returns true if hand contains a straight
bool IsSequence(struct card hand[HAND_SIZE]) 
{
	InsertionSort(hand);
	int i;
	for (i = 1; i < HAND_SIZE; i++) {
		if ((hand[i] - hand[i-1]) != 1) { return false; }
	}
	return true;
}
Exemplo n.º 26
0
// main - entry point
int main(int argc, const char * argv[]) {
    int numbers[10] = { 12, 445, 55, 67, 2, 7, 909, 45, 4454, 1 };
    printf("Before Insertion Sort: ");
    PrintArray(numbers, 10);
    InsertionSort(numbers, 10);
    printf("After Insertion Sort: ");
    PrintArray(numbers, 10);
    return 0;
}
Exemplo n.º 27
0
int main( void ){
	int datas[20]={	1,12,3,4,15,6,7,18,9,10,
					11,2,13,14,5,16,17,8,19,20};
	set_random( datas, 20 );
	print(datas,20);
	InsertionSort(datas, 20);
	print(datas,20);
	return 0;
}
Exemplo n.º 28
0
int main(int argc, char *argv[])
{
    int arr[5] = {4,2,5,1,9}; 
    InsertionSort(arr, 5);
    for(auto &i : arr)
    std::cout << i << " ";
    std::cout << std::endl;
    return 0;
}
Exemplo n.º 29
0
// Returns the index in the hand of the highest card ranking for pairs.
// Assumes hand contains a pair or two.
int HighestCard(struct card hand[HAND_SIZE]) 
{
	InsertionSort(hand);
	int i;
	for (i = HAND_SIZE-1; i > 0; i--) {
		if (hand[i].rank == hand[i-1].rank) { return i; }
	}
	return 0;			// should not have to reach this
}
Exemplo n.º 30
0
// This is implemented for you, but you need to implement the InsertionSort 
// function that it calls.
void ArraySorter::ShellSort(int* arr, int n, int* gapVals, int gapValsCount)
{
	// Do an insertion sort pass for each of the gap values
	int i;
	for (i = 0; i < gapValsCount; i++)
	{
		for (int j = 0; j < gapVals[i]; j++)
		{
			InsertionSort(arr, n, j, gapVals[i]);
		}
	}

	// We always need to finish with a pass using gap=1
	if (1 != gapVals[i - 1])
	{
		InsertionSort(arr, n);
	}
}