Advertisement
m1okgoodyes

SelectionSort(сортировка выбором)

Apr 8th, 2022
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5. namespace sortSelection//сортировка выбором
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Введите неотсортированный массив");
  12.             int[] arr = new int[10];
  13.             for (int i = 0; i < arr.Length; i++)
  14.             {
  15.                 arr[i] = Convert.ToInt32(Console.ReadLine());
  16.             }
  17.  
  18.             Console.WriteLine("\n\n");
  19.             arr = SelectionSort(arr);
  20.             foreach(int i in arr)
  21.             {
  22.                 Console.WriteLine(i);
  23.             }
  24.         }
  25.         public static int[] SelectionSort(int[] arr)
  26.         {
  27.             for(int i = 0; i < arr.Length - 1; i++)
  28.             {
  29.                 int min = i;
  30.                 for(int j = i + 1; j < arr.Length; j++)
  31.                 {
  32.                     if(arr[j] < arr[min])
  33.                     {
  34.                         min = j;
  35.                     }
  36.                 }
  37.                 int temp = arr[i];
  38.                 arr[i] = arr[min];
  39.                 arr[min] = temp;
  40.                 min = i;
  41.             }
  42.             return arr;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement