A22.0002 - ASSIGNMENT 7


You have to write a program that sorts an array of integers using Selection Sort. The algorithm is quite simple:

  1. You examine all the entries in the array and you find the smallest entry.
  2. You swap the contents of the first entry in the array with the entry in the array that you have found in step 1.
  3. You repeat the process above, with the difference that you take into the consideration only the array starting from the second entry, then from the third, etc. until you have reached the end of the array.
Ex:
   Location    1   2   3   4   5   6   7 
   Value      35  12  90  -1   7  10  27

   Smallest number is -1 in location 4: swap the contents of location 1 with the contents of location 4.

   Location    1   2   3   4   5   6   7 
   Value      -1  12  90  35   7  10  27

   Taking in consideration only locations 2 through 7, the smallest number is 7 in location 5: swap the 
   contents of location 2 with the contents of location 5.

   Location    1   2   3   4   5   6   7 
   Value      -1   7  90  35  12  10  27

   Taking in consideration only locations 3 through 7, the smallest number is 10 in location 6: swap the 
   contents of location 3 with the contents of location 6.

   Location    1   2   3   4   5   6   7 
   Value      -1   7  10  35  12  90  27

   Taking in consideration only locations 4 through 7, the smallest number is 12 in location 5: swap the 
   contents of location 4 with the contents of location 5.

   Location    1   2   3   4   5   6   7 
   Value      -1   7  10  12  35  90  27

   Taking in consideration only locations 5 through 7, the smallest number is 27 in location 7: swap the 
   contents of location 5 with the contents of location 7.

   Location    1   2   3   4   5   6   7 
   Value      -1   7  10  12  27  90  35

   Taking in consideration only locations 6 through 7, the smallest number is 35 in location 7: swap the 
   contents of location 6 with the contents of location 7.

   Location    1   2   3   4   5   6   7 
   Value      -1   7  10  12  27  35  90

   It makes no sense to compare location 7 with itself, so the algorithm stops.
Your program will ask the user to input up to 20 integers (both positive and negative). The program will read the integers from the keyboard and store them into an array until the user presses <CR> (or until the whole array is filled). It will then display the integers in sorted order.

Ex.:

   Please enter up to 20 integers: 35 12 90 -1 7 10 27
   Integers in sorted order      : -1   7  10  12  27  35  90
Notes: