Advertisement
Venciity

[C#] BOOK Arrays - 8 Задача [ първи метод ]

Jan 6th, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. /*Сортиране на масив означава да подредим елементите му в нарастващ (намаляващ) ред.
  2.  * Напишете програма, която сортира масив. Да се използва алгоритъма "Selection sort". */
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace _8_Exercise
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Console.Write("How much elements do you want to have the array: ");
  16.             int numberOfElements = int.Parse(Console.ReadLine());
  17.             int[] array = new int[numberOfElements];
  18.  
  19.             //I get the numbers into an array
  20.  
  21.             for (int i = 0; i < numberOfElements; i++)
  22.             {
  23.                 Console.Write("number {0} = ", i + 1);
  24.                 array[i] = int.Parse(Console.ReadLine());
  25.             }
  26.  
  27.             int startingPosition = 0;
  28.             while (startingPosition <= array.Length - 1)
  29.             {
  30.                 int currentMinIndex = 0;
  31.                 int currentMin = int.MaxValue;
  32.                 for (int i = startingPosition; i < array.Length; i++)
  33.                 {
  34.                     if (array[i] < currentMin)
  35.                     {
  36.                         currentMin = array[i];
  37.                         currentMinIndex = i;
  38.                     }
  39.                 }
  40.                 int temp = array[startingPosition];
  41.  
  42.                 array[startingPosition] = currentMin;
  43.                 array[currentMinIndex] = temp;
  44.                 startingPosition++;
  45.             }
  46.             Console.WriteLine();
  47.             Console.WriteLine("The sorted elements of massif are :");
  48.             for (int i = 0; i < array.Length; i++)
  49.             {
  50.                 Console.WriteLine("{0}: {1}", i+1, array[i]);
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement