Clever Geek Handbook
📜 ⬆️ ⬇️

Shell Sort

Shell sort ( Shell sort ) - sorting algorithm , which is an improved option for sorting inserts . The idea of ​​the Shell method is to compare elements that are not only nearby, but also at a certain distance from each other. In other words, this is sorting by inserts with preliminary “rough” passes. A similar method for improving bubble sorting is called comb sorting .

Shell Sort
Shell sorting step-by-step visualization
Sort in steps 23, 10, 4, 1.
Author
DestinationSorting algorithm
Data structureArray
Worst timeO ( n 2 )
Best timeO ( n log 2 n )
Average timedepends on the steps chosen
Memory overheadO (n) total, O (1) optional
Shell sorting by example

Content

Description

When sorting Shell, values ​​are first compared and sorted among themselves, at a certain distance from each otherd {\ displaystyle d}   (about choosing a valued {\ displaystyle d}   see below ). After this, the procedure is repeated for some lower values.d {\ displaystyle d}   , and Shell sorting is completed by ordering the elements whend=one {\ displaystyle d = 1}   (i.e. normal sorting by inserts ). The efficiency of Shell sorting in certain cases is ensured by the fact that the elements “fall faster” fall into place (in simple sorting methods, for example, bubble sorting, each rearrangement of two elements reduces the number of inversions in the list by a maximum of 1, and when sorting Shell, this number may be greater )

Despite the fact that Shell sorting is in many cases slower than fast sorting , it has several advantages:

  • lack of memory requirements for the stack;
  • no degradation due to unsuccessful data sets - quick sorting easily degrades to O (n²), which is worse than the worst guaranteed time for Shell sorting.

History

Shell sorting was named after its inventor, Donald Shell , who published this algorithm in 1959 .

Example

 


Let the list be givenA=(32,95,sixteen,82,24,66,35,nineteen,75,54,40,43,93,68) {\ displaystyle A = (32.95,16.82,24,66,35,19,75,54,40,43,93,68)}   and it is sorted by the Shell method, and as valuesd {\ displaystyle d}   selectedfive,3,one {\ displaystyle 5,3,1}   .

The first step is sorting the sublists.A {\ displaystyle A}   made up of all elementsA {\ displaystyle A}   , differing by 5 positions, i.e. sublistsAfive,one=(32,66,40) {\ displaystyle A_ {5,1} = (32,66,40)}   ,Afive,2=(95,35,43) {\ displaystyle A_ {5,2} = (95,35,43)}   ,Afive,3=(sixteen,nineteen,93) {\ displaystyle A_ {5,3} = (16,19,93)}   ,Afive,four=(82,75,68) {\ displaystyle A_ {5,4} = (82,75,68)}   ,Afive,five=(24,54) {\ displaystyle A_ {5.5} = (24.54)}   .

In the resulting list, in the second step, the sublists are again sorted from the elements spaced by 3 positions.

The process ends with the usual sorting by inserts of the resulting list.

Gap Length Selection

The average runtime of the algorithm depends on the lengths of the gaps -d {\ displaystyle d}   on which there will be sortable elements of the original array with capacityN {\ displaystyle N}   at every step of the algorithm. There are several approaches to choosing these values:

  • The sequence of spacing lengths originally used by Shell:done=N/2,di=di-one/2,dk=one {\ displaystyle d_ {1} = N / 2, d_ {i} = d_ {i-1} / 2, d_ {k} = 1}   in the worst case, the complexity of the algorithm isO(N2) {\ displaystyle O (N ^ {2})}   ;
  • Hibbard-proposed sequence: all values2i-one≤N,i∈N {\ displaystyle 2 ^ {i} -1 \ leq N, i \ in \ mathbb {N}}   ; such a sequence of steps leads to an algorithm of complexityO(N3/2) {\ displaystyle O (N ^ {3/2})}   ;
  • Sedgwick proposed sequence:di=9⋅2i-9⋅2i/2+one {\ displaystyle d_ {i} = 9 \ cdot 2 ^ {i} -9 \ cdot 2 ^ {i / 2} +1}   if i is even anddi=eight⋅2i-6⋅2(i+one)/2+one {\ displaystyle d_ {i} = 8 \ cdot 2 ^ {i} -6 \ cdot 2 ^ {(i + 1) / 2} +1}   if i is odd. When using such increments, the average complexity of the algorithm is:O(n7/6) {\ displaystyle O (n ^ {7/6})}   , and in the worst case orderO(nfour/3) {\ displaystyle O (n ^ {4/3})}   . When using the Sedgwick formula, we should focus on the value inc [s-1] if 3 * inc [s]> size. [1] ;
  • Pratt's proposed sequence: all values2i⋅3j≤N/2,i,j∈N {\ displaystyle 2 ^ {i} \ cdot 3 ^ {j} \ leq N / 2, i, j \ in \ mathbb {N}}   ; in this case, the complexity of the algorithm isO(N(logN)2) {\ displaystyle O (N (logN) ^ {2})}   ;
  • the empirical sequence of Marcin Tsiur (sequence A102549 in OEIS ):d∈{one,four,ten,23,57,132,301,701,1750} {\ displaystyle d \ in \ left \ {1,4,10,23,57,132,301,701,1750 \ right \}}   ; is one of the best for sorting an array with a capacity of up to about 4000 elements. [2] ;
  • empirical sequence based on Fibonacci numbers :d∈{Fn} {\ displaystyle d \ in \ left \ {F_ {n} \ right \}}   ;
  • all values(3j-one)≤N {\ displaystyle (3 ^ {j} -1) \ leq N}   ,j∈N {\ displaystyle j \ in \ mathbb {N}}   ; such a sequence of steps leads to an algorithm of complexityO(N3/2) {\ displaystyle O (N ^ {3/2})}   .

C ++ Implementation

  template < typename RandomAccessIterator , typename Compare >
 void shell_sort ( RandomAccessIterator first , RandomAccessIterator last , Compare comp )
 {
     for ( typename std :: iterator_traits < RandomAccessIterator > :: difference_type d = ( last - first ) / 2 ; d ! = 0 ; d / = 2 )
 // need a loop for first = a [0..d-1]
         for ( RandomAccessIterator i = first + d ; i ! = last ; ++ i )
             for ( RandomAccessIterator j = i ; j - first > = d && comp ( * j , * ( j - d ) ); j - = d )
                 std :: swap ( * j , * ( j - d ) );
 }

C implementation

  void ShellSort ( int array [], int size ) // * Δk = (bΔk − 1) / 2 Δ0 = N
 {
	 int step , i , j , tmp ;
	
	 // Select step
	 for ( step = size / 2 ; step > 0 ; step / = 2 )
	     // Listing items that are sorted at a specific step
		 for ( i = step ; i < size ; i ++ ) 
		     // Rearrange the elements inside the sublist until the i-th is sorted
			 for ( j = i - step ; j > = 0 && array [ j ] > array [ j + step ]; j - = step )
			 {
				 tmp = array [ j ];
				 array [ j ] = array [ j + step ];
				 array [ j + step ] = tmp ;
			 }
 }

Java Implementation

  public class ShellSort
 {
     public void sort ( int [] arr )
     {
         for ( int inc = arr . length / 2 ; inc > = 1 ; inc = inc / 2 ;)
             for ( int step = 0 ; step < i ; step ++)
                 insertionSort ( arr , step , inc );
     }

     private void insertionSort ( int [] arr , int start , int inc )
     {
         int tmp ;
         for ( int i = start ; i < arr . length - 1 ; i + = inc )
             for ( int j = Math . min ( i + inc , arr . length - 1 ); j - inc > = 0 ; j = j - inc )
                 if ( arr [ j - inc ] > arr [ j ])
                 {
                     tmp = arr [ j ];
                     arr [ j ] = arr [ j - inc ];
                     arr [ j - inc ] = tmp ;
                 }
                 else break ;
     }
 }

Python implementation

  def shellSort ( array ):
	 increment = len ( array ) // 2
	 while increment > 0 :

		 for startPosition in range ( increment ):
			 gapInsertionSort ( array , startPosition , increment )

		 print ( "After incrementing the size by" , increment , "array:" , array )

		 increment // = 2

 def gapInsertionSort ( array , low , gap ):
    
	 for i in range ( low + gap , len ( array ), gap ):
		 currentvalue = array [ i ]
		 position = i

		 while position > = gap and array [ position - gap ] > currentvalue :
			 array [ position ] = array [ position - gap ]
			 position = position - gap

		 array [ position ] = currentvalue



Notes

  1. ↑ J. Incerpi, R. Sedgewick , “Improved Upper Bounds for Shellsort,” J. Computer and System Sciences 31, 2, 1985.
  2. ↑ Marcin Ciura Best Increments for the Average Case of Shellsort

Links

  • D. Knut . The art of programming. Volume 3. Sort and search, 2nd ed. Ch. 5.2.1. ISBN 5-8459-0082-4
  • Animated Shell Sort Algorithm
  • Presentation of Shell's sorting algorithm as a dance (video)
Source - https://ru.wikipedia.org/w/index.php?title=Shell_Sorting&oldid=101466171


More articles:

  • Punk Fashion
  • Armenian Boxing Championship 2015
  • Kariaeva, Tamara Kharitonovna
  • Jesus Christ (South Park)
  • Sexual practices between women
  • Abdominal Migraine
  • Gianniotas, Giannis
  • Barmin, Igor Vladimirovich
  • Berenbaum, May
  • Solovyov, Petr Nikolayevich

All articles

Clever Geek | 2019