Advertisement
desislava_topuzakova

01.Selection Sort

Jun 14th, 2020
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Sortign
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] numbers = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  12.             selectionSort(numbers);
  13.             printArray(numbers);
  14.         }
  15.  
  16.         static void selectionSort(int[] arr)
  17.         {
  18.             int n = arr.Length;
  19.  
  20.             // One by one move boundary of unsorted subarray
  21.             for (int i = 0; i < n - 1; i++)
  22.             {
  23.                 // Find the minimum element in unsorted array
  24.                 int min_idx = i;
  25.                 for (int j = i + 1; j < n; j++)
  26.                     if (arr[j] < arr[min_idx])
  27.                         min_idx = j;
  28.  
  29.                 // Swap the found minimum element with the first
  30.                 // element
  31.                 int temp = arr[min_idx];
  32.                 arr[min_idx] = arr[i];
  33.                 arr[i] = temp;
  34.             }
  35.         }
  36.  
  37.  
  38.         // Prints the array
  39.         static void printArray(int[] arr)
  40.         {
  41.             int n = arr.Length;
  42.             for (int i = 0; i < n; ++i)
  43.                 Console.Write(arr[i] + " ");
  44.             Console.WriteLine();
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement