Advertisement
m1okgoodyes

SortShell

Apr 9th, 2022
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace ShellSort
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Console.WriteLine("Введите неотсортированный массив");
  12.             int[] arr = new int[6];
  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 = ShellSort(arr);
  20.             foreach (int i in arr)
  21.             {
  22.                 Console.WriteLine(i);
  23.             }
  24.         }
  25.         public static int[] ShellSort(int[] arr)
  26.         {
  27.             int j;
  28.             int step = arr.Length / 2;
  29.             while(step > 0)
  30.             {
  31.                 for(int i = 0; i < (arr.Length - step); i++)
  32.                 {
  33.                     j = i;
  34.                     while((j >= 0) && (arr[j] > arr[j + step]))
  35.                     {
  36.                         int temp = arr[j];
  37.                         arr[j] = arr[j + step];
  38.                         arr[j + step] = temp;
  39.                         j -= step;
  40.                     }
  41.                 }
  42.                 step = step / 2;
  43.             }
  44.             return arr;
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement