Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

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


8,090 questions

11,268 answers

573 users

How to implement selection sort in C++

1 Answer

0 votes
#include <iostream>
#include <cstdlib>        
#include <ctime> 

using std::cout;
using std::endl;

inline 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()
{
	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++)
		cout << arr[i] << endl;
	
	return 0;
}


/*
run:

13
26
69
144
191
324
330
344
382
393
600
743
786
835
891
908
950
951
955
998

*/

 




answered May 12 by avibootz
...