encho253

Untitled

Nov 1st, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System;
  2.  
  3. /// <summary>
  4. /// Sorting an array means to arrange its elements in increasing order.
  5. /// Write a program to sort an array. Use the Selection sort algorithm:
  6. /// Find the smallest element, move it at the first position, find the smallest from the rest,
  7. /// move it at the second position, etc.
  8. /// </summary>
  9. class SelectionSort
  10. {
  11.     /// <summary>
  12.     /// Defines the entry point of the application.
  13.     /// </summary>
  14.     static void Main()
  15.     {
  16.         int n = int.Parse(Console.ReadLine());
  17.         int[] array = new int[n];
  18.         int index = 0;
  19.         int value = 0;
  20.         bool inside = false;
  21.  
  22.         for (int i = 0; i < array.Length; i++)
  23.         {
  24.             array[i] = int.Parse(Console.ReadLine());
  25.         }
  26.  
  27.         for (int i = 0; i < array.Length; i++)
  28.         {
  29.             for (int j = i; j < array.Length; j++)
  30.             {
  31.                 if (array[index] > array[j])
  32.                 {
  33.                     index = j;
  34.                     value = array[index];
  35.                     inside = true;
  36.                 }          
  37.             }
  38.             if (inside)
  39.             {
  40.                 array[index] = array[i];
  41.                 array[i] = value;              
  42.                 inside = false;
  43.             }
  44.             index = i + 1;
  45.         }
  46.         for (int i = 0; i < array.Length; i++)
  47.         {
  48.             Console.WriteLine(array[i]);
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment