[csharp] kakolimp84

Viewer

copydownloadembedprintName: kakolimp84
  1. using System;
  2.  
  3. class Program
  4. {
  5.     // Сортировка выбором
  6.     static void SelectionSort(int[] arr)
  7.     {
  8.         int n = arr.Length;
  9.         for (int i = 0; i < n - 1; i++)
  10.         {
  11.             int minIndex = i;
  12.             for (int j = i + 1; j < n; j++)
  13.             {
  14.                 if (arr[j] < arr[minIndex])
  15.                 {
  16.                     minIndex = j;
  17.                 }
  18.             }
  19.             // Обмен элементов
  20.             int temp = arr[minIndex];
  21.             arr[minIndex] = arr[i];
  22.             arr[i] = temp;
  23.  
  24.             // Вывод массива после каждого шага
  25.             Console.Write($"Шаг {i + 1}: ");
  26.             PrintArray(arr);
  27.         }
  28.     }
  29.  
  30.     // Бинарный поиск
  31.     static int BinarySearch(int[] arr, int target)
  32.     {
  33.         int left = 0;
  34.         int right = arr.Length - 1;
  35.  
  36.         while (left <= right)
  37.         {
  38.             int mid = left + (right - left) / 2;
  39.  
  40.             if (arr[mid] == target)
  41.             {
  42.                 return mid;
  43.             }
  44.             else if (arr[mid] < target)
  45.             {
  46.                 left = mid + 1;
  47.             }
  48.             else
  49.             {
  50.                 right = mid - 1;
  51.             }
  52.         }
  53.  
  54.         // Если элемент не найден
  55.         return -1;
  56.     }
  57.  
  58.     // Представление односвязного списка и вставка элемента после первого элемента
  59.     class ListNode
  60.     {
  61.         public int Value { get; set; }
  62.         public ListNode Next { get; set; }
  63.  
  64.         public ListNode(int value)
  65.         {
  66.             Value = value;
  67.             Next = null;
  68.         }
  69.     }
  70.  
  71.     static ListNode InsertAfterFirst(ListNode head, int value)
  72.     {
  73.         ListNode newNode = new ListNode(value);
  74.         if (head == null)
  75.         {
  76.             return newNode;
  77.         }
  78.  
  79.         newNode.Next = head.Next;
  80.         head.Next = newNode;
  81.         return head;
  82.     }
  83.  
  84.     static void PrintList(ListNode head)
  85.     {
  86.         ListNode current = head;
  87.         while (current != null)
  88.         {
  89.             Console.Write($"{current.Value} ");
  90.             current = current.Next;
  91.         }
  92.         Console.WriteLine();
  93.     }
  94.  
  95.     static void PrintArray(int[] arr)
  96.     {
  97.         foreach (var item in arr)
  98.         {
  99.             Console.Write($"{item} ");
  100.         }
  101.         Console.WriteLine();
  102.     }
  103.  
  104.     static void Main(string[] args)
  105.     {
  106.         int[] arr = { 231057641220911222425 };
  107.  
  108.         Console.WriteLine("Исходный массив:");
  109.         PrintArray(arr);
  110.  
  111.         Console.WriteLine("\nТрассировка первых 10 шагов (обменов) сортировки выбором:");
  112.         SelectionSort(arr);
  113.         
  114.         Console.WriteLine("\nРезультат сортировки:");
  115.         PrintArray(arr);
  116.  
  117.         int target = 20;
  118.         Console.WriteLine($"\nПоиск числа {target} методом бинарного поиска:");
  119.         int index = BinarySearch(arr, target);
  120.         if (index != -1)
  121.         {
  122.             Console.WriteLine($"Число {target} найдено в позиции {index}.");
  123.         }
  124.         else
  125.         {
  126.             Console.WriteLine($"Число {target} не найдено в массиве.");
  127.         }
  128.  
  129.         // Создание односвязного списка и вставка элемента после первого элемента
  130.         ListNode head = new ListNode(1);
  131.         head = InsertAfterFirst(head, 2);
  132.         head = InsertAfterFirst(head, 3);
  133.  
  134.         Console.WriteLine("\nОдносвязный список после вставки элемента:");
  135.         PrintList(head);
  136.     }
  137. }
  138.  

Editor

You can edit this paste and save as new:


File Description
  • kakolimp84
  • Paste Code
  • 29 Mar-2024
  • 3.8 Kb
You can Share it: