Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Сортиране на масив означава да подредим елементите му в нарастващ (намаляващ) ред.
- * Напишете програма, която сортира масив. Да се използва алгоритъма "Selection sort". */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _8_Exercise___2_method__
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("How much elements do you want to have the array: ");
- int numberOfElements = int.Parse(Console.ReadLine());
- int[] arrayOfNumbers = new int[numberOfElements];
- //I get the numbers into an array
- for (int i = 0; i < numberOfElements; i++)
- {
- Console.Write("number {0} = ",i+1);
- arrayOfNumbers[i] = int.Parse(Console.ReadLine());
- }
- int helpfulVariable = 0; //I will use this variable to exchange values
- //I get the first number and compare it with the other.
- //If there is a smaller number I exchange the values and the smallest number goes to the first place.
- //I repeat this with the rest of the elements
- for (int i = 0; i < numberOfElements; i++)
- {
- for (int j = i + 1; j < numberOfElements; j++)
- {
- if (arrayOfNumbers[j] < arrayOfNumbers[i])
- {
- helpfulVariable = arrayOfNumbers[j];
- arrayOfNumbers[j] = arrayOfNumbers[i];
- arrayOfNumbers[i] = helpfulVariable;
- }
- }
- }
- Console.WriteLine();
- Console.WriteLine("The sorted array is: ");
- for (int i = 0; i < numberOfElements; i++)
- {
- Console.Write("Number {0} = ",i+1);
- Console.WriteLine(arrayOfNumbers[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment