当前位置: 首页>>代码示例>>C++>>正文


C++ bubblesort函数代码示例

本文整理汇总了C++中bubblesort函数的典型用法代码示例。如果您正苦于以下问题:C++ bubblesort函数的具体用法?C++ bubblesort怎么用?C++ bubblesort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了bubblesort函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: cmp_pts

TEAM *input_dat(FILE *fp, int *num_team)
{
	char line[SIZE_STR], **token;
	int i, num_token;
	int cmp_pts();
	TEAM team_home, team_away, *team_ary;
	struct list *team_list, *p;

	*num_team = 0;
	team_list = init_list();
	while (fgets(line, SIZE_STR, fp) != NULL) {
		if (line[0] != '#') {
			token = tokenize_str(line, "-\t\n", &num_token);
			if (num_token >= 4) {
				set_home_team(&team_home, token);
				set_away_team(&team_away, token);

				if ((p = search_team(team_list, team_home.name)) == NULL) {
					push_queue(team_list, &team_home, sizeof(team_home));
					(*num_team)++;
				}
				else {
					add_team_result(p, team_home);
				}

				if ((p = search_team(team_list, team_away.name)) == NULL) {
					push_queue(team_list, &team_away, sizeof(team_away));
					(*num_team)++;
				}
				else {
					add_team_result(p, team_away);
				}
			}
			free_token_ary(token, num_token);
		}
	}

	team_ary = (TEAM *) malloc(sizeof(TEAM) * (*num_team));
	if (team_ary == NULL) {
		fprintf(stderr, "ERROR: Unable to allocate memory.\n");
		exit(EXIT_FAILURE);
	}
	for (i = 0; i < *num_team; i++) {
		pop_queue(team_list, &team_ary[i], sizeof(TEAM));
	}
	delete_list(team_list);

	/* 安定ソートで被ゴール数→ゴール数→得失点差→勝ち点の順で整列すると順位で並ぶ */
	/* リーグ戦の規程によってはこのやり方ではダメ */
	bubblesort(team_ary, *num_team, sizeof(TEAM), cmp_gag);
	bubblesort(team_ary, *num_team, sizeof(TEAM), cmp_gfo);
	bubblesort(team_ary, *num_team, sizeof(TEAM), cmp_gdi);
	bubblesort(team_ary, *num_team, sizeof(TEAM), cmp_pts);

	return team_ary;
}
开发者ID:jfujihub,项目名称:league,代码行数:56,代码来源:filein.c

示例2: bubblesort_two_strings

/**
 * Test that bubblesort() correctly sorts an array of two
 * strings.
 */
static void bubblesort_two_strings(void)
{
	char *a[2];

	/* The array is already sorted - no designation */
	a[0] = test;
	a[1] = tset;
	bubblesort(a, 2);
	CU_ASSERT_EQUAL(a[0], test);
	CU_ASSERT_EQUAL(a[1], tset);

	/* The array is in revers order - no designation */
	a[0] = tset;
	a[1] = test;
	bubblesort(a, 2);
	CU_ASSERT_EQUAL(a[0], test);
	CU_ASSERT_EQUAL(a[1], tset);

	/* The array is already sorted - with designations */
	a[0] = test_1;
	a[1] = tset_1;
	bubblesort(a, 2);
	CU_ASSERT_EQUAL(a[0], test_1);
	CU_ASSERT_EQUAL(a[1], tset_1);

	/* The array is in reverse order - with designation */
	a[0] = tset_1;
	a[1] = test_1;
	bubblesort(a, 2);
	CU_ASSERT_EQUAL(a[0], test_1);
	CU_ASSERT_EQUAL(a[1], tset_1);

	/* The array is already sorted - mixed designations */
	a[0] = test_1;
	a[1] = tset;
	bubblesort(a, 2);
	CU_ASSERT_EQUAL(a[0], test_1);
	CU_ASSERT_EQUAL(a[1], tset);

	/* The array is in reverse order - mixed designations */
	a[0] = tset;
	a[1] = test_1;
	bubblesort(a, 2);
	CU_ASSERT_EQUAL(a[0], test_1);
	CU_ASSERT_EQUAL(a[1], tset);

	/* The array is already sorted - differing designations */
	a[0] = test_1;
	a[1] = test_99;
	bubblesort(a, 2);
	CU_ASSERT_EQUAL(a[0], test_1);
	CU_ASSERT_EQUAL(a[1], test_99);

	/* The array is in reverse order - differing designations */
	a[0] = test_99;
	a[1] = test_1;
	bubblesort(a, 2);
	CU_ASSERT_EQUAL(a[0], test_1);
	CU_ASSERT_EQUAL(a[1], test_99);
}
开发者ID:thentenaar,项目名称:mmm,代码行数:64,代码来源:utils.c

示例3: main

int main()
{
   int number_array[20] = {1,2,200,10,34,65,23,78,79,11,23,55,123,1001,91,11221,1,7,203020,12};
   int i,r;
   int  size0 = 80; int size1 = 80; 
       *((volatile unsigned int *)0xf000f008) = (unsigned int)(number_array);
    *((volatile unsigned int *)0xf000f00C) = 100;  
   
   printf("------Original Array-------\n");
   for(i=0; i<20; i++)
   {
	 printf("%d \n", number_array[i]);
	}

   
	 

   
 
    r = bubblesort(number_array, size0, number_array, size1);

   printf("------Sorted Array-------\n");
   for(i=0; i<20; i++)
   {
    printf("%d \n", number_array[i]);
   }
  return 0;
}
开发者ID:RCSL-HKUST,项目名称:heterosim,代码行数:28,代码来源:tb_bsort100.c

示例4: main

int main(void)
{
    
    printf("Enter number of entries: ");
    scanf("%d", &testArrayTotal);
    
    for (int i = 0; i < testArrayTotal; i++) {
        printf("Enter a number: ");
        scanf("%d", &testArray[i]);
    }
    
    *testArray = *bubblesort(testArray, testArrayTotal);
    
    printf("\nThe sorted list is: ");
    
    for (int i = 0; i < testArrayTotal; i++) {
        
        printf("%i ", testArray[i]);
        
    }
    
    printf("\n\n");
    
    return 0;
    
}
开发者ID:fossildog117,项目名称:main,代码行数:26,代码来源:main.c

示例5: main

/******************************************************************************************
 * 起泡排序测试程序
 ******************************************************************************************/
void main ( int argc, char* argv[] ) {
   int n = 0; //array length
   if ( 1 < argc ) n = atoi ( argv[1] ); if ( n < 0 ) n = 0; //make sure length is non-negative
   int* A = ( int* ) malloc ( n * sizeof ( int ) ); //allocate an array of size n
   unsigned int seed = ( unsigned int ) time ( NULL ); //A same seed is used here for comparison between different algorithms
   printf ( "\n== Bubblesort algorithm #0 ========\n" );
   randomArray ( A, n, seed ); //create a randomized array using the same seed
   printf ( "-->  " ); print ( A, n );
   bubblesort ( A, n ); //sort the array using algorithm#0
   printf ( "==>  " ); print ( A, n );
   printf ( "\n== Bubblesort algorithm #1A ========\n" );
   randomArray ( A, n, seed ); //create a randomized array using the same seed
   printf ( "==>  " ); print ( A, n );
   bubblesort1A ( A, n ); //sort the array using algorithm#1A
   printf ( "==>  " ); print ( A, n );
   printf ( "\n== Bubblesort algorithm #1B ========\n" );
   randomArray ( A, n, seed ); //create a randomized array using the same seed
   printf ( "==>  " ); print ( A, n );
   bubblesort1B ( A, n ); //sort the array using algorithm#1B
   printf ( "==>  " ); print ( A, n );
   printf ( "\n== Bubblesort algorithm #2 ========\n" );
   randomArray ( A, n, seed ); //create a randomized array using the same seed
   printf ( "==>  " ); print ( A, n );
   bubblesort2 ( A, n ); //sort the array using algorithm#2
   printf ( "==>  " ); print ( A, n );
   free ( A ); //release the array
}
开发者ID:HillBamboo,项目名称:MOOCs,代码行数:30,代码来源:main.cpp

示例6: main

int main(){
    int a[] = {9,8,7,6,5,4,3,2,1,0};
    bubblesort(a, 10);
    int b;
    double c;
    return 0;
}
开发者ID:RainWarrior,项目名称:c-compare,代码行数:7,代码来源:main2.c

示例7: main

int main( int argc, char *argv[])
{
#ifndef FACTOR
	if ( argc == 1 ) {
		printf("The prime factors of 13195 are 5, 7, 13 and 29.\n");
		printf("What is the largest prime factor of the number 600851475143 ?\n");
		int primes[100];
		int p = 0;
		prime_factor(600851475143L, &p, primes);
		int max = 0;
		for ( int i = 0; i < p; i++)
			if(primes[i] > max) max = primes[i];
		printf("Answer: %d\n", max);
	}
#endif
#ifdef FACTOR
	if( argc == 2 ) {
		unsigned long factor = atol(argv[1]);
		int primes[100];
		int p = 0;
		prime_factor(factor, &p, primes);
		printf("%lu:", factor);
		bubblesort(primes, p);
		for( int i = 0; i < p; i++)
			printf(" %d", primes[i]);
		printf("\n");
	}
#endif
}
开发者ID:Dutchy-,项目名称:ceuler,代码行数:29,代码来源:euler003.c

示例8: main

int main()
{

    //Inicializa a biblioteca gráfica com a estrutura a ser apresentada na tela
    init($VETOR,MAX,1);

    setDataType(float);

    int i = 0;

    setSleepTime(1);

    for(i = 0; i < MAX; i++)
    {
        vetor[i] = rand()%100;
    }

    setSleepTime(2);
    show(&vetor,0);



    setSleepTime(1);
    bubblesort();
    setSleepTime(10);
    show(&vetor,0);

    terminateDSGraph();

    return 0;
}
开发者ID:ufjf-dcc,项目名称:dsgraph,代码行数:31,代码来源:main.c

示例9: main

int main() {
   int * items, * sorted, i;

   items = (int *) malloc(ITEMS_LENGTH * sizeof(int));

   for(i = 0; i < ITEMS_LENGTH; i++)
      scanf("%d", &items[i]);

   sorted = (int *) malloc(ITEMS_LENGTH * sizeof(int));

   for(i = 0; i < ITEMS_LENGTH; i++)
      sorted[i] = items[i];

   sorted = bubblesort(sorted);

   for(i = 0; i < ITEMS_LENGTH; i++)
      printf("%d\n", sorted[i]);

   printf("\n");

   for(i = 0; i < ITEMS_LENGTH; i++)
      printf("%d\n", items[i]);

   return 0;
}
开发者ID:Zarphenus,项目名称:URI-Challenges,代码行数:25,代码来源:1042.c

示例10: main

int main(int argc, char const *argv[])
{
	int array[100], i;
	int numofelement;

	printf("Enter number of element in array:\n");
	scanf("%d",&numofelement);

	printf("Enter element of array:\n");
	for (i = 0; i < numofelement; i++)
	{
		scanf("%d",&array[i]);
	}

	printf("elements of array: before sorting\n");
	for (i = 0; i < numofelement; i++)
	{
		printf("%d\n",array[i]);
	}

	printf("elements of array: after sorting\n");
	bubblesort(array, numofelement);
	for (i = 0; i < numofelement; i++)
	{
		printf("%d\n",array[i]);
	}

	return 0;
}
开发者ID:skant1681,项目名称:code,代码行数:29,代码来源:bubble.c

示例11: scan_root

void scan_root(void){
	if (artist_list != NULL) {rprintf("already scanned.\n");return;}
	char long_pathname[40] = "0:/MUSIC";
	rprintf("scanning 0:/MUSIC\n");
	scan_files(long_pathname);

	rprintf("found artists: ");

	num_of_artists = 0;
	unsigned int num_of_tracks = 0;
	Artist * list_cpy = artist_list;
	while(list_cpy != NULL) {
		num_of_artists++;

		list_cpy->tracks = bubblesort(list_cpy->tracks);
		unsigned int arts_trks = 0;
		for (Track *tmp=list_cpy->tracks; tmp->next != NULL; tmp=tmp->next) { arts_trks++; } //counts how many tracks this artist has
		rprintf("%s [%i], ",list_cpy->name, arts_trks );
		num_of_tracks += arts_trks; //add artist total to the overall total.

		list_cpy = list_cpy->next;
	}

	rprintf("\nnumber of: artists=%i, tracks=%i \n",num_of_artists,num_of_tracks);
}	
开发者ID:t413,项目名称:mp3_player_v2,代码行数:25,代码来源:mp3.c

示例12: main

int main(void){
  int i;

  printf("N? ");
  scanf("%d", &N);

  /*set random numbers Bubble[], Quick[], Merge[]*/
  srand((unsigned int)time(0));
 
  for(i = 0; i < N; i++){
    Bubble[i] = Quick[i] = Merge[i] = (rand() % 10000) + 1;
  }

  time(&tm1);  /*tm1(秒)*/
  bubblesort();
  time(&tm2);  /*tm2(秒)*/
  printf("Bubble Sort %ld sec \n", tm2 - tm1);

  time(&tm1);  /*tm1(秒)*/
  quicksort(0, N - 1);
  time(&tm2);  /*tm2(秒)*/
  printf("Quick Sort %ld sec \n", tm2 - tm1);

  time(&tm1);  /*tm1(秒)*/
  m_sort(0, N - 1);
  time(&tm2);  /*tm2(秒)*/
  printf("Merge Sort %ld sec \n", tm2 - tm1);

  return 0;
}
开发者ID:camberbridge,项目名称:Algorithm_DataStructure,代码行数:30,代码来源:BQMsort.c

示例13: main

int main(int argc, char **argv)
{
    srand(time(NULL));
    
    int numbers[SIZE];
    
    int i;
    for (i = 0; i < SIZE; ++i)
    {
        numbers[i] = rand() % (SIZE + 1); 
    }
    
    for (i = 0; i < SIZE; ++i)
    {
        printf("%d: %d\n", i, numbers[i]);
    }
    
    bubblesort (numbers, SIZE);
    
    assert( sorted(numbers, SIZE));
    
    return(0);
    

}
开发者ID:lquan,项目名称:OVS,代码行数:25,代码来源:bubble.c

示例14: while

// sort thread shall behave the same as hw thread:
// - get pointer to data buffer
// - if valid address: sort data and post answer
// - if exit command: issue thread exit os call
void *sort_thread(void* data)
{
    unsigned int ret;
    unsigned int dummy = 23;
    struct reconos_resource *res  = (struct reconos_resource*) data;
    struct mbox *mb_start = res[0].ptr;
    struct mbox *mb_stop  = res[1].ptr;
    //pthread_t self = pthread_self();
    //printf("SW Thread %lu: Started with mailbox addresses %p and %p ...\n", self,  mb_start, mb_stop);
    while ( 1 ) {
        ret = mbox_get(mb_start);
        //printf("SW Thread %lu: Got address %p from mailbox %p.\n", self, (void*)ret, mb_start);
        if (ret == UINT_MAX)
        {
            //  printf("SW Thread %lu: Got exit command from mailbox %p.\n", self, mb_start);
            pthread_exit((void*)0);
        }
        else
        {
            bubblesort( (unsigned int*) ret, N);
        }

        mbox_put(mb_stop, dummy);
    }

    return (void*)0;
}
开发者ID:rihuber,项目名称:reconos_v3,代码行数:31,代码来源:sort_demo.c

示例15: main

int main(void){
  int i;
  double t, x, y, z;  /*t: ソート処理前のclock, x: バブルソート実行時間, 
                       *y: クイックソート実行時間, z: マージソート実行時間
                       */

  printf("N? ");
  scanf("%d", &N);

  /*set random numbers Bubble[], Quick[], Merge[]*/
  srand((unsigned int)time(0));
 
  for(i = 0; i < N; i++){
    Bubble[i] = Quick[i] = Merge[i] = (rand() % 10000) + 1;
  }

  t = clock();
  bubblesort();
  x = (clock() - t) / CLOCKS_PER_SEC;
  printf("Bubble Sort %g sec \n", x);

  t = clock();
  quicksort(0, N - 1);
  y = (clock() - t) / CLOCKS_PER_SEC;
  printf("Quick Sort %g sec \n", y);

  t = clock();
  m_sort(0, N - 1);
  z = (clock() - t) / CLOCKS_PER_SEC;
  printf("Merge Sort %g sec \n", z);

  return 0;
}
开发者ID:camberbridge,项目名称:Algorithm_DataStructure,代码行数:33,代码来源:BQMsort_2.c


注:本文中的bubblesort函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。