Venciity

[C#] BOOK Arrays - 8 Задача [ втори метод ] нарастващ ред

Jan 6th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. /*Сортиране на масив означава да подредим елементите му в нарастващ (намаляващ) ред.
  2.  * Напишете програма, която сортира масив. Да се използва алгоритъма "Selection sort". */
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _8_Exercise___2_method__
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             Console.Write("How much elements do you want to have the array: ");
  17.             int numberOfElements = int.Parse(Console.ReadLine());
  18.             int[] arrayOfNumbers = new int[numberOfElements];
  19.  
  20.             //I get the numbers into an array
  21.  
  22.             for (int i = 0; i < numberOfElements; i++)
  23.             {
  24.                 Console.Write("number {0} = ",i+1);
  25.                 arrayOfNumbers[i] = int.Parse(Console.ReadLine());
  26.             }
  27.  
  28.             int helpfulVariable = 0;   //I will use this variable to exchange values
  29.  
  30.  
  31.             //I get the first number and compare it with the other.
  32.             //If there is a smaller number I exchange the values and the smallest number goes to the first place.
  33.             //I repeat this with the rest of the elements
  34.  
  35.             for (int i = 0; i < numberOfElements; i++)
  36.             {
  37.                 for (int j = i + 1; j < numberOfElements; j++)
  38.                 {
  39.                     if (arrayOfNumbers[j] < arrayOfNumbers[i])
  40.                     {
  41.                         helpfulVariable = arrayOfNumbers[j];
  42.                         arrayOfNumbers[j] = arrayOfNumbers[i];
  43.                         arrayOfNumbers[i] = helpfulVariable;
  44.                     }
  45.                 }
  46.             }
  47.             Console.WriteLine();
  48.             Console.WriteLine("The sorted array is: ");
  49.  
  50.             for (int i = 0; i < numberOfElements; i++)
  51.             {
  52.                 Console.Write("Number {0} = ",i+1);
  53.                 Console.WriteLine(arrayOfNumbers[i]);
  54.             }
  55.  
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment