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


C++ selection_sort函数代码示例

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


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


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