TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALL
from: DAVID NOON
date: 1998-04-09 21:38:00
subject: Selection Sort

As promised in C_Echo, here is Selection sort. Not a speed burner, but there
are some worse ones around.
=============================== selectn.hpp ================================
// Templates for Selection sort algorithm for ascending & descending sort.
// Copyright (C) 1998, David W. Noon
// All rights reserved.
// You may use this code freely. You may also distribute it, provided
// no charge is levied beyond that for its distribution medium.
// The author makes no warranty of the code's usefulness for any purpose,
// and accepts no responsibility for any damages caused by its misuse.
template void ascending(T array[], int n)
{
   int i, j, m, const nm1 = n - 1;
   T Hold_it_here;
   for (i = 0; i < nm1; ++i)
   {
      m = i; // Assume left-most is minimum
      // Scan for true minimum
      for (j = i + 1; j < n; ++j)
         if (array[j] < array[m])
            m = j;
      // Exchange minimum with left-most
      Hold_it_here = array[m];
      array[m] = array[i];
      array[i] = Hold_it_here;
   }
}
template void descending(T array[], int n)
{
   int i, j, m, const nm1 = n - 1;
   T Hold_it_here;
   for (i = 0; i < nm1; ++i)
   {
      m = i; // Assume left-most is maximum
      // Scan for true maximum
      for (j = i + 1; j < n; ++j)
         if (array[j] > array[m])
            m = j;
      // Exchange maximum with left-most
      Hold_it_here = array[m];
      array[m] = array[i];
      array[i] = Hold_it_here;
   }
}
============================================================================
Regards
Dave

___
 * MR/2 2.25 #353 * SYNTAX?  Why not--they tax everything else!
--- Maximus/2 3.01
---------------
* Origin: DoNoR/2,Woking UK (44-1483-717905) (2:440/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.