Advertisement
m1okgoodyes

InsertedSort(сортировка вставками)

Apr 8th, 2022
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5. namespace SortVstavki
  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.             int[] InsertionSort(int[] arr)
  20.             {
  21.                 int[] result = new int[arr.Length];
  22.                 for (int i = 0; i < arr.Length; i++)
  23.                 {
  24.                     int j = i;
  25.                     while (j > 0 && result[j - 1] > arr[i])
  26.                     {
  27.                         result[j] = result[j - 1];
  28.                         j--;
  29.                     }
  30.                     result[j] = arr[i];
  31.                 }
  32.                 return result;
  33.             }
  34.             int[] res = InsertionSort(arr);
  35.             Console.WriteLine("отсортированный массив: ");
  36.             foreach (int i in res)
  37.             {
  38.                 Console.WriteLine(i);
  39.             }
  40.         }
  41.  
  42.      
  43.     }
  44.            
  45.    
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement