• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ selection_sort函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: merge_sort

/*
** Divide the array into N/M blocks of size of M, sort every block
** using selection sort, and run the array merging the first block
** with the second, then the second block with the third, and so on.
*/
void merge_sort(item_t *array,int left,int right)
{
	int n=right-left+1;
	int n_blocks=n/M;
	int *nodes;
	int i;

	if(right-left+1<M){
		selection_sort(array,left,right);
		return;
	}

	nodes=malloc(n_blocks*sizeof(*nodes));
	for(i=0;i<n_blocks;i++){
		if(i==n_blocks-1){
			nodes[i]=right;
		}else{
			nodes[i]=left+(i+1)*M-1;
		}
	}

	for(i=0;i<n_blocks;i++){
		selection_sort(array,left+i*M,nodes[i]);
	}

	for(i=0;i<n_blocks-1;i++){
		merge(array,left,nodes[i],nodes[i+1]);
	}

	free(nodes);
}
开发者ID:algking,项目名称:Algorithms-in-C-Sedgewick,代码行数:36,代码来源:E8.21_mergesort_block.c


示例2: main

int main()
{
	int a[] = {24,54,32,-56,12,-6,543,23,-85,32,6,65};
	const int sa = sizeof(a) / sizeof(int);
	char b[] = { 'd', 'c', 'r', 's', 'j', 't' };
	const int sb = sizeof(b) / sizeof(char);
	float c[] = { 4.32f, 5.4f, 0.01f, -56.9f, 54.34f, -34.54f };
	const int sc = sizeof(c) / sizeof(float);
	long d[] = { 1 };
	selection_sort(a, sa);
	selection_sort(b, sb);
	selection_sort(c, sc);
	selection_sort(d, 1);
	for (int i = 0; i < sa; i++)
		std::cout << a[i] << ' ';
	std::cout << std::endl;
	for (int i = 0; i < sb; i++)
		std::cout << b[i] << ' ';
	std::cout << std::endl;
	for (int i = 0; i < sc; i++)
		std::cout << c[i] << ' ';
	std::cout << std::endl;
	std::cout << d[0] << std::endl;
	return 0;
}
开发者ID:lianera,项目名称:UniversalAlgorithm,代码行数:25,代码来源:selection_sort.cpp


示例3: main

int main(void)
{
    int nums0[] = {31, 2, 5, 15, 21, 22, 11, 8, 1};
    int sz0 = 9;
    print_array(nums0, sz0);
    selection_sort(nums0, sz0);
    print_array(nums0, sz0);

    int nums1[] = {1};
    int sz1 = 1;
    print_array(nums1, sz1);
    selection_sort(nums1, sz1);
    print_array(nums1, sz1);

    int nums2[] = {};
    int sz2 = 0;
    print_array(nums2, sz2);
    selection_sort(nums2, sz2);
    print_array(nums2, sz2);

    int nums3[] = {18, 23, 12, 13};
    int sz3 = 4;
    print_array(nums3, sz3);
    selection_sort(nums3, sz3);
    print_array(nums3, sz3);
}
开发者ID:andymos66,项目名称:cs50-section-examples,代码行数:26,代码来源:selection-sort0-pseudocode.c


示例4: main

void main()
{	clrscr();
	int a[20],n,flag=0;
	cout<<"\n Enter the no of terms:";
	cin>>n;
	cout<<"\n Enter elements:";
	for(int i=0;i<n;i++)
	{ cin>>a[i];
	}
	cout<<"\nArray is:"<<endl;
	for(i=0;i<n;i++)
   {    cout<<a[i]<<" ";
   }
	for(i=0;i<n-1;i++)
   {    if(a[i]<a[i+1])
	{  flag=0;
	}
	else
	{       flag=1;
		break;
	}
    }
	if(flag==0)
	cout<<"\nArray is already sorted";
	else
	selection_sort(a,n);
	getch();
}
开发者ID:SachinBhandari,项目名称:MyCodes,代码行数:28,代码来源:SELEC_SO+(2).CPP


示例5: main

int main()
{
  int i, array_size;


  printf("Enter number of elements: ");
  scanf("%d",&array_size);

  int a[array_size];

  printf("Enter array: ");

  for(i=0; i<array_size; i++)
    scanf("%d",&a[i]);
  //reads a list of numbers from user and puts them in an array

  selection_sort(array_size, a);
  //calls selection sort () to sort array

  printf("Sorted array: ");

  for(i=0; i<array_size; i++)
    printf("%d ",a[i]);
  printf("\n");
  //prints sorted array
  return 0;
}
开发者ID:SheepGotoHeaven,项目名称:C-language-homework-assignments,代码行数:27,代码来源:sort.c


示例6: selection_sort

void selection_sort(int n, int array[])
{
  int big, i;

  if(n==0)
    return;
  //if the first parameter is 0, then the size of the array is 0 and the 
  //function terminates    

   big=array[0];
   //initializes 'big' value to the first array entry

   for(i=1; i<n; i++)
     {
       if(array[i]>big)
	 big=array[i];
     }
   //finds largest number in array and makes big = largest value

   for(i=0; i<n; i++)
     {
       if(array[i]==big)
       {
	 array[i]=array[n-1];
	 array[n-1]=big;
	 break;
       }
     }
   //swaps position of largest value and last value in array
   selection_sort(n-1, array);
   //function calls self recursively to sort the remaining
   //numbers of the array.  By decrementing n by 1, we ensure that the largest
   //value at the end will go untouched, while the remaining
   // values will be sorted.  When n reaches 0, the function terminates.
}
开发者ID:SheepGotoHeaven,项目名称:C-language-homework-assignments,代码行数:35,代码来源:sort.c


示例7: main

main() {

    clock_t tempo1;

	//Contagem de tempo do BubbleSort
	cria_vetor();
	tempo1 = clock();
  bubble_sort();
	tempo1 = clock() - tempo1;
  printf("\nBubbleSorte: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n", verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do InsertionSort
  cria_vetor();
  tempo1 = clock();
  insertion_sort();
	tempo1 = clock() - tempo1;
  printf("InsertionSort: %.4f seg\n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do SelectionSort
  cria_vetor();
  tempo1 = clock();
  selection_sort();
	tempo1 = clock() - tempo1;
  printf("SelectionSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do Heapsort
  cria_vetor();
  tempo1 = clock();
  heapsort();
	tempo1 = clock() - tempo1;
  printf("HeapSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do MergeSort
  cria_vetor();
	tempo1 = clock();
  mergesort(0, TAM - 1);
	tempo1 = clock() - tempo1;
  printf("mergeSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

    //Contagem de tempo do QuickSort
  cria_vetor();
	tempo1 = clock();
  quicksort(0, TAM - 1);
	tempo1 = clock() - tempo1;
  printf("QuickSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC);
  printf("Vetor ordenado? %s\n\n",  verifica_ordem() ? "Sim" : "Nao"); //Operador ternario
	//--------------------------------------------------------------------------

	getch();
}
开发者ID:Renannr,项目名称:estrutura-de-dados,代码行数:60,代码来源:Trabalho_3bi_1_Maysa_e_Renann.cpp


示例8: main

int main(void)
{
	int v[21];
	int n=20;
	count c;

	setlocale (LC_ALL, "Portuguese");

	resetVetor(v, n);
	printf("Vetor usado \n");
	imprimeVetor(v, n);

	printf("selection Sort \n");
	c = selection_sort(v, n);
	automato( c, v, n);

	printf("Selecion Sort v2 \n");
	c = selection_sort2(v, n);
	automato( c, v, n);

	printf("Insertion Sort \n");
	c = insertion_sort(v, n);
	automato( c, v, n);

	printf("Bubble Sort \n");
	c = bubble_sort(v, n);
	automato( c, v, n);
	
	printf("Merge Sort \n");
	c = merge_sort(v, 0, n);
	automato( c, v, n);
	return 0;

}
开发者ID:RicardoIreno,项目名称:Algoritmos,代码行数:34,代码来源:ordenacoes.c


示例9: selection_sort

void selection_sort(RandomAccessIterator begin, RandomAccessIterator end) {
  // typedef
  typedef std::iterator_traits<RandomAccessIterator> traits;
  typedef typename traits::value_type value_type;

  selection_sort(begin, end, std::less<value_type>());
}
开发者ID:dieumort,项目名称:sort,代码行数:7,代码来源:selection_sort.hpp


示例10: main

int main(){
    int A[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
    int B[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
    size_t len = sizeof(A) / sizeof(A[0]);

    selection_sort(A, len);
    selection_sort(B, len);

    printf("A: ");
    printl(A, len);

    printf("B: ");
    printl(B, len);

    return 0;
}
开发者ID:ephexeve,项目名称:algorithms,代码行数:16,代码来源:2.2-2.c


示例11: timeSelectionSort

long timeSelectionSort(
    std::size_t Size,
    std::uniform_int_distribution<IntType> &UniformIntDistribution_,
    std::default_random_engine DefaultRandomEngine_) {
  auto L = getRandomList(Size, UniformIntDistribution_, DefaultRandomEngine_);
  return timeAlgorithm([&]() -> void { L.selection_sort(); });
}
开发者ID:m5w,项目名称:2015,代码行数:7,代码来源:complexity.cpp


示例12: main

main()
{
  // Function declaration
  void selection_sort(int *, int);

  int *array = NULL; // Pointer to int, initialize to nothing.
  int n, i;

  printf("Enter number of elements: ");
  scanf("%d", &n);

  // Dynamically allocate memory for n elements
  array = (int *)calloc(n, sizeof(int));

  // Reading the elements
  printf("\nEnter the elements:");

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

  // Invoke selection_sort
  selection_sort(array, n);

  // Display sorted array
  printf("sorted array:");

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

  printf("\n");
}
开发者ID:codeblocks,项目名称:codeblocks,代码行数:31,代码来源:selection_sort.c


示例13: sort_array

void sort_array(int *a, int n) {
	char c;
	int success;

	do {
		printf("\n\nSorting algorithms: ");
		printf("\n[1].-Bubble\n[2].-Selection\n[3].-Insertion\n[4].-No-Sorting");
		printf("\nChoose:\t");
		if(!(success = is_single_char(&c)))
			fflushstdin();
		if(c < 49 || c > 52)
			printf("Please give a valid option\n");
	} while(!success || c < 49 || c > 52);

	switch (c)
	{
		case '1':
			bubble_sort (a, n);
			break;
		case '2':
			selection_sort(a, n);
			break;
		case '3':
			insertion_sort (a, 0, n);
			break;
		case '4':
			printf("\nDATA STAYS THE SAME\n");
			break;
		default:
			break;
	}
}
开发者ID:intfrr,项目名称:c-functions,代码行数:32,代码来源:sorting.c


示例14: START_TEST

END_TEST

START_TEST (test_selection_rand)
{
    selection_sort(randarray, N, compare_ints, exch_ints);
    ck_assert(is_sorted(randarray, N));
}
开发者ID:Revil,项目名称:sorting,代码行数:7,代码来源:check_sort.c


示例15: par_mergesort

void par_mergesort(void * arg) {
  struct array * A = (struct array*)arg;

  if(A->len <= seq_threshold) {
    selection_sort(A);
  }

  else {
    struct array left_half, right_half;

    if (A->len % 2 == 0) {
      left_half.len  = right_half.len = A->len/2;
    } else {
      left_half.len  = A->len/2;
      right_half.len = A->len/2 + 1;
    }

    left_half.arr  = A->arr;
    right_half.arr = A->arr + left_half.len;

    struct thread * left_t  = thread_fork(par_mergesort, &left_half);
    struct thread * right_t = thread_fork(par_mergesort, &right_half);

    thread_join(left_t);
    thread_join(right_t);

    merge(&left_half, &right_half);
  }
}
开发者ID:wuhaooooo,项目名称:OS-homework,代码行数:29,代码来源:main.c


示例16: main

int main(void)
{
  	int *i;
  	int a[N];

  	printf("Enter %d numbers to be sorted: \n", N);
	
	for (i = a; i < a + N; i++) /* fill sorted array with pointer arithmetic */
	{
		scanf("%d\n", &*i);	
	}
	
  	selection_sort(a, N); /* perform selection sort recursivly with pointer arithmetic */

  	printf("In sorted order:");
	
	for (i = a; i < a + N; i++) /* print sorted array */
	{
		printf(" %d", *i);
	}

	printf("\n");

  	return 0;
}
开发者ID:Darksole,项目名称:class-projects,代码行数:25,代码来源:selection_sort.c


示例17: main

int main(void){
  Item a[10];
  
  rand_array(a);
  printf("before: ");
  print_array(a);
  selection_sort(a,0,9);
  printf("after: ");
  print_array(a);

  rand_array(a);
  printf("before: ");
  print_array(a);
  insertion_sort(a,0,9);
  printf("after: ");
  print_array(a);

  rand_array(a);
  printf("before: ");
  print_array(a);
  quicksort(a,0,9);
  printf("after: ");
  print_array(a);

}
开发者ID:AhmedSamara,项目名称:ECE-209,代码行数:25,代码来源:SortingAlgorithms.c


示例18: main

int main()
{
	int x[10] = {10,8,6,4,2,1,3,5,7,9};

	//printArray(x,10);
	selection_sort(x, 10);
}
开发者ID:juner417,项目名称:data_structure,代码行数:7,代码来源:1_sort2.cpp


示例19: main

int main(void) {
    /*
    int my_array[ARRAY_MAX];
    int i, count = 0;
    while (count < ARRAY_MAX && 1 == scanf("%d", &my_array[count])) {
        count++;
    }
    selection_sort(my_array, count);
    for (i = 0; i < count; i++) {
        printf("%d\n", my_array[i]);
    }
    return EXIT_SUCCESS;
    */
    int my_array[ARRAY_MAX];
    clock_t start, end;
    int i, count = 0;
    while (count < ARRAY_MAX && 1 == scanf("%d", &my_array[count])) {
        count++;
    }
    start = clock();
    selection_sort(my_array, count);
    end = clock();
    for (i = 0; i < count; i++) {
        printf("%d\n", my_array[i]);
    }
    fprintf(stderr, "%d %f\n", count, (end - start) / (double)CLOCKS_PER_SEC);
    return EXIT_SUCCESS;
}
开发者ID:Mr-Binary,项目名称:C_Playground,代码行数:28,代码来源:selectionSort.c


示例20: main

int main() {
    
  u_i len = 12;
  int arr[12] = {2, 13, 12, 16, 15, 4, 17, 8, 1, 18, 14, 9};
  int *temp1 = copy(arr, len);
  insertion_sort(temp1, len);
  int *temp2 = copy(arr, len);
  quick_sort(temp2, len);
  int *temp3 = copy(arr, len);
  merge_sort(temp3, len);
  int *temp4 = copy(arr, len);
  selection_sort(temp4, len);
  int *temp5 = copy(arr, len);
  counting_sort(temp5, len);
  int *temp6 = copy(arr, len);
  heap_sort(temp6, len);
  int *temp7 = copy(arr, len);
  radix_sort(temp7, len);
  
  bool eq = equal(temp1, temp2, len) &
            equal(temp2, temp3, len) &
            equal(temp3, temp4, len) &
            equal(temp4, temp5, len) &
            equal(temp5, temp6, len) &
            equal(temp6, temp7, len);
            
  printf("\x1B[32m""Equal arrays :%s\n""\033[0m", eq ? "true" : "false");
  
  // Free all arrays allocated.
}
开发者ID:Maithem,项目名称:algorithms-for-fun,代码行数:30,代码来源:driver.c



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ selector函数代码示例发布时间:2022-05-30
下一篇:
C++ selectionState函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap