C++ examples for Data Structure:Algorithm
Selection Sort on array
#include <ctime> #include <iostream> void selectionSort(int[], int); static const int LIMIT = 10; int main(int argc, const char *argv[]) { int n[LIMIT]; srand(time(0));//w ww .j a v a 2 s . c o m for (int i = 0; i < LIMIT; ++i) { n[i] = rand() % 100 + 1; } // print unsorted array std::cout << "Before selectionSort: " << std::endl; for (int i = 0; i < LIMIT; ++i) { std::cout << n[i] << std::endl; } selectionSort(n, LIMIT); // print sorted array std::cout << "\nAfter selectionSort: " << std::endl; for (int i = 0; i < LIMIT; ++i) { std::cout << n[i] << std::endl; } return 0; } void selectionSort(int n[], int LIMIT) { static int startIndex = 0; int minIndex = startIndex; // exit condition if (startIndex >= LIMIT - 1) return; // get the index of the smallest value for (int i = startIndex + 1; i < LIMIT; ++i) { if (n[i] < n[minIndex]) minIndex = i; } // swap with n[startIndex] int tmp = n[startIndex]; n[startIndex] = n[minIndex]; n[minIndex] = tmp; // increment startIndex ++startIndex; selectionSort(n, LIMIT); }