fueanta

Selection Sorting

Oct 12th, 2016
143
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Problem : Introduction to selection sorting.
  2. // AUTHOR : fueanta
  3. // Written : 12-Oct-16
  4.  
  5. #include "cstdio"
  6. #include "algorithm"
  7. using namespace std;
  8.  
  9. void sel_sort(int *, int);
  10.  
  11. void main() {
  12.     int arr[] = { 5, 3, 7, 2, 4, 5, 1 }; /* just an example :p */
  13.     printf("\n"); sel_sort(arr, (sizeof(arr) / 4));
  14.     for (int i = 0; i < (sizeof(arr) / 4); i++) {
  15.         printf("|%d|", arr[i]);
  16.     }
  17.     printf("\n\n");
  18. }
  19.  
  20. void sel_sort(int arr[], int n) {
  21.     for (int SELECT = 0; SELECT < n - 1; SELECT++) {
  22.         int LOC = SELECT;
  23.         for (int I = SELECT + 1; I < n; I++) {
  24.             if (arr[I] < arr[LOC])
  25.                 LOC = I;
  26.         }
  27.         swap(arr[SELECT], arr[LOC]); // swap came from algorithm lib.
  28.     }
  29. }
RAW Paste Data