Advertisement
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
- {
- 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[] array = new int[numberOfElements];
- //I get the numbers into an array
- for (int i = 0; i < numberOfElements; i++)
- {
- Console.Write("number {0} = ", i + 1);
- array[i] = int.Parse(Console.ReadLine());
- }
- int startingPosition = 0;
- while (startingPosition <= array.Length - 1)
- {
- int currentMinIndex = 0;
- int currentMin = int.MaxValue;
- for (int i = startingPosition; i < array.Length; i++)
- {
- if (array[i] < currentMin)
- {
- currentMin = array[i];
- currentMinIndex = i;
- }
- }
- int temp = array[startingPosition];
- array[startingPosition] = currentMin;
- array[currentMinIndex] = temp;
- startingPosition++;
- }
- Console.WriteLine();
- Console.WriteLine("The sorted elements of massif are :");
- for (int i = 0; i < array.Length; i++)
- {
- Console.WriteLine("{0}: {1}", i+1, array[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement