Advertisement
Guest User

[C#] Arrays - 7 задача

a guest
Dec 15th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. //Sorting an array means to arrange its elements in increasing order. Write a program to sort an array. Use the "selection sort" algorithm: Find the smallest element, move it at the first position, find the smallest from the rest, move it at the second position, etc.
  2.  
  3. using System;
  4.  
  5. class SelectionSortAlgorithm
  6. {
  7.     static void Main()
  8.     {
  9.         int n = int.Parse(Console.ReadLine());
  10.         int [] array=new int [n];
  11.         int[] newArray = new int[n];
  12.         for (int i = 0; i < n; i++)
  13.         {
  14.             array[i] = int.Parse(Console.ReadLine());
  15.         }
  16.         for (int i = 0; i < n; i++)
  17.         {
  18.             int minValue = int.MaxValue;
  19.             int maxValue = int.MinValue;
  20.             for (int j = i+1; j < n; j++)
  21.             {
  22.                 minValue = Math.Min(array[i], array[j]);
  23.                 maxValue = Math.Max(array[i], array[j]);
  24.                 array[i] = minValue;
  25.                 array[j] = maxValue;
  26.             }          
  27.         }
  28.         Console.WriteLine(string.Join(" ",array));
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement