Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,621 questions

10,674 answers

573 users

How to implement selection sort in C

1 Answer

0 votes
#include <stdio.h>     
#include <time.h> 
#include <stdlib.h>        

void swap(int *x, int *y) {
	int tmp = *x;
	*x = *y;
	*y = tmp;
}

void selection_sort(int *array, int size) {
	int *next, *before, *last = array + size - 1;

	for (; array < last; array++)
	{
		before = array;
		for (next = array + 1; next <= last; next++)
			if (*before > *next)
				before = next;
		swap(array, before);
	}
}

int main(void)
{
	const int size = 20;
	int arr[size];

	srand((unsigned int)time(NULL));

	for (int i = 0; i < size; i++)
		arr[i] = (rand() % 1000);

	selection_sort(arr, size);

	for (int i = 0; i < size; i++)
		printf("%d\n", arr[i]);
	
	return 0;
}


/*
run:

32
143
186
225
241
400
426
441
610
653
670
752
769
770
800
812
825
867
913
998

*/

 




answered May 12 by avibootz
...