YouTip LogoYouTip

Python Shellsort

# Python3.x Python Shell Sort [![Image 4: Document Object Reference](#) Python3 Examples](#) Shell sort, also known as the diminishing increment sort algorithm, is a more efficient improved version of insertion sort. However, Shell sort is not a stable sorting algorithm. The basic idea of Shell sort is to first divide the entire sequence of records to be sorted into several subsequences and perform direct insertion sort on each subsequence separately. When the records in the entire sequence are "basically ordered," a final direct insertion sort is performed on all records. !(#) ## Example def shellSort(arr): n = len(arr)gap = int(n/2)while gap>0: for i in range(gap,n): temp = arrj = i while j>= gap and arr>temp: arr = arrj -= gap arr = temp gap = int(gap/2)arr = [12, 34, 54, 2, 3]n = len(arr)print("Before sorting:")for i in range(n): print(arr), shellSort(arr)print("n After sorting:")for i in range(n): print(arr), Executing the above code produces the following output: Before sorting:12345423After sorting:23123454 [![Image 6: Document Object Reference](#) Python3 Examples](#)
← Method ContinuePython Heap Sort β†’