Advertisement
ScorpS

Sort Array Algorithm

Jan 6th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1.  
  2. //  Sorting an array means to arrange its elements in increasing order.
  3. //  Write a program to sort an array. Use the "selection sort" algorithm:
  4. //  Find the smallest element, move it at the first position, find the
  5. //  smallest from the rest, move it at the second position, etc.
  6.  
  7. using System;
  8.  
  9. class SortArray
  10. {
  11.     static void Main()
  12.     {
  13.         int[] array = new int[10];
  14.         int minValue;
  15.         int changer;
  16.  
  17.         for (int index = 0; index < 10; index++)
  18.         {
  19.             array[index] = int.Parse(Console.ReadLine());
  20.         }
  21.  
  22.         for (int i = 0; i < 9; i++)
  23.         {
  24.             minValue = i;
  25.             for (int k = i + 1; k < 10; k++)
  26.             {
  27.                 if (array[minValue] > array[k])
  28.                 {
  29.                     minValue = k;
  30.                 }
  31.             }
  32.  
  33.             changer = array[minValue];
  34.             array[minValue] = array[i];
  35.             array[i] = changer;          
  36.         }
  37.  
  38.         for (int i = 0; i < 10; i++)
  39.         {
  40.             Console.WriteLine(array[i]);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement