Task

Find the top   N   salaries in each department,   where   N   is provided as a parameter.

Use this data as a formatted internal data structure (adapt it to your language-native idioms, rather than parse at runtime), or identify your external data source:

Employee Name,Employee ID,Salary,Department
Tyler Bennett,E10297,32000,D101
John Rappl,E21437,47000,D050
George Woltman,E00127,53500,D101
Adam Smith,E63535,18000,D202
Claire Buckman,E39876,27800,D202
David McClellan,E04242,41500,D101
Rich Holcomb,E01234,49500,D202
Nathan Adams,E41298,21900,D050
Richard Potter,E43128,15900,D101
David Motsinger,E27002,19250,D202
Tim Sampair,E03033,27000,D101
Kim Arlich,E10001,57000,D190
Timothy Grove,E16398,29900,D190



(subrg, stringrange, stringsize):
rank: procedure options (main);   /* 10 November 2013 */
 
   declare 1 employee (13),
             2 name char (15) varying,
             2 ID   char (6),
             2 salary fixed (5),
             2 department char (4);
   declare done(hbound(employee)) bit (1);
   declare ptr(hbound(employee)) fixed binary;
   declare true bit(1) value ('1'b), false bit(1) value ('0'b);
   declare dept character (4);
   declare text character (80) varying;
   declare (i, j, l, k, m, n, p, t) fixed binary;
   declare in file input;
 
   open file (in) title ('/TOP-RANK.DAT, RECSIZE(80), TYPE(TEXT)' );
 
   on endfile (in) go to completed_input;
   j = 0;
   do forever;
      get file (in) edit (text) (L);
      j = j + 1;
      i = index(text, ',');
      name(j) = substr(text, 1, i-1);
      k = index(text, ',', i+1);
      ID(j) = substr(text, i+1, k-(i+1));
      i = k; k = index(text, ',', i+1);
      salary(j) = substr(text, i+1, k-(i+1));
      department(j) = substr(text, k+1);
   end;
 
completed_input:
   m = hbound(employee);
   put skip list ('How many highest-paid employees do you want?');
   get (n);
   put skip edit ('Looking for the ', trim(n),
                  ' highest-paid employees in each department') (a);
   done = false;
   do i = 1 to m;
      do j = 1 to m;
         if done(j) then iterate;
         dept = department(j);
          /* done(j) = true; */
         leave;
      end;
      /* Locate all the employees of this department. */
      k = 0;
      do j = 1 to m;
         if ^done(j) & (department(j) = dept) then
            do;
               k = k + 1;
               ptr(k) = j;
               done(j) = true;
            end;
      end;
      if k = 0 then leave; /* (No more departments.) */
 
      put skip list ('Employees in department ' || dept || ' are:-' );
      do j = 1 to k;
         put skip list (employee(ptr(j)));
      end;
      /* We now have k employees in "dept".  Now find the maximum n salaries. */
      /* ptr points to all of them. */
      /* Use a bubble sort to move n values to one end. */
      do p = 1 to min(n, k);
         do j = 1 to k-1;
            if salary(ptr(j)) > salary(ptr(j+1)) then
               do;
                  t = ptr(j+1); ptr(j+1) = ptr(j); ptr(j) = t;
               end;
         end;
      end;
 
      /* Having moved the largest n values to the end of our list, */
      /* we print them. */
      put skip list ('Highest-paid employees in department ' || dept || ':-');
      do j = k to k-min(k,n)+1 by -1;
         put skip list (employee(ptr(j)) );
      end;
   end;
   put skip list ('FINISHED');
end rank;