Shell Sort in data structure and algorithm

What is Shell Sort?

Shell Sort is a highly efficient sorting algorithm based on insertion sorting algorithm (Insertion Sort) . This algorithm avoids the case of swapping positions of two distant elements in the selection algorithm (if the smaller element is in the right position quite far from the larger element on the left).

First, this algorithm uses a sorting algorithm on elements that are far apart, then arranging elements with a narrower distance. This distance is also known as interval - the number of positions from one element to another. This range is calculated based on the following Knuth formula:

 h = h * 3 + 1 trong đó: h l à Kho ả ng ( interval ) v ớ i gi á tr ị ban đâ u l à 1 

This algorithm is quite effective for medium sized data sets when the worst case complexity and the average case are O (n), where n is the number of elements.

How Shell Sort works

To make it easier to learn, below I provide illustrations for how Shell Sort works. We use an array of values ​​as shown below. Suppose the initial value of interval is 4. For example, for element 35, with a range of 4, the remaining element will be 14. Therefore we will have pairs of values ​​{35, 14}, { 33, 19}, {42, 27}, and {10, 14}.

Shell Sort in data structure and algorithm Picture 1

Compare these values ​​together in sub-lists and swap them (if needed) in the original array. After this step, the new array will be empty as follows:

Shell Sort in data structure and algorithm Picture 2

Then, take the value of 2 interval and with this distance will give two sub-lists: {14, 27, 35, 42}, {19, 10, 33, 44}.

Shell Sort in data structure and algorithm Picture 3

Continue to compare and swap values ​​(if needed) in the original array. After this step, the array will look like this:

Shell Sort in data structure and algorithm Picture 4

Finally, we arrange this remaining array with interval equal to 1. Shell Sort uses an insertion sort algorithm to sort the array. Below is an illustration of each step.

Shell Sort in data structure and algorithm Picture 5

As shown above, you see that we only need 4 swaps to sort out this remaining array.

Algorithm for Shell Sort

Now we will monitor the algorithm for Shell Sort:

 Bước 1 : Khởi tạo giá trị h Bước 2 : Chia list thành các sublist nhỏ hơn tương ứng với h Bước 3 : Sắp xếp các sublist này bởi sử dụng sắp xếp chèn (Insertion Sort) Bước 4 : Lặp lại cho tới khi list đã được sắp xếp

Sample algorithm for Shell Sort

From the above steps we can design a sample algorithm for Shell Sort as follows:

 B ắ t đầ u h à m shellSort () A : m ả ng c á c ph ầ n t ử /* Tính toán giá trị Khoảng (interval) */ while interval < A . length / 3 th ự c hi ệ n : interval = interval * 3 + 1 k ế t th ú c while while interval > 0 th ự c hi ệ n : for outer = interval ; outer < A . length ; outer ++ th ự c hi ệ n : /* chọn giá trị để chèn */ valueToInsert = A [ outer ] inner = outer ; /*dịch chuyển phần tử sang phải*/ while inner > interval - 1 && A [ inner - interval ] >= valueToInsert do : A [ inner ] = A [ inner - interval ] inner = inner - interval k ế t th ú c while /* chèn giá trị vào vị trí trên */ A [ inner ] = valueToInsert k ế t th ú c for /* Tính toán giá trị Khoảng (interval) */ interval = ( interval - 1 ) / 3 ; k ế t th ú c while K ế t th ú c h à m 

According to Tutorialspoint

Previous lesson: Mixing algorithm (Merge Sort)

Next lesson: Quick Sort (Quick Sort)

4 ★ | 2 Vote

May be interested

  • AVL tree in data structure and algorithmAVL tree in data structure and algorithm
    what happens if the data entered into the binary search tree (bst) is in ordered form (ascending or descending). it will look like the following.
  • How to Sort Cells Alphabetically in ExcelHow to Sort Cells Alphabetically in Excel
    excel is a powerful spreadsheet tool used to store and manage text or figures. alphabetical sorting is one of excel's useful features with the ability to help you sort, access and reference data quickly. to sort cells in excel alphabetically, simply double-click by highlighting the range of cells to sort, then click the 'az sort' or 'za sort' icon in the bar. standard tool. to sort cells alphabetically in excel using the advanced sort option, highlight the entire worksheet, click the 'sort' option from the 'data' menu. , then select the column and order you want to sort in the dialog box that appears.
  • Sort the database in ExcelSort the database in Excel
    instructions on how to organize a database in excel. sorting data is indispensable when working in excel. 1. sort data simply. step 1: select the data range to be sorted - data - sort: step 2: sort dialog box appears field selection
  • Sort data in ExcelSort data in Excel
    sort data in excel - excel supports you with data sorting tools to give you the sorting options. you can use the sort tool to sort the data in your excel document.
  • Stack data structure (Stack)Stack data structure (Stack)
    a stack is an abstract data structure (abstract data type - adt for short), mostly used in almost every programming language. name the stack because it acts as a stack in real life, such as a deck of cards or a stack of disks ...
  • Queue data structure (Queue)Queue data structure (Queue)
    queue (queue) is an abstract data structure, is something similar to queues in everyday life (queuing).
  • MS Excel 2007 - Lesson 8: Sort and FilterMS Excel 2007 - Lesson 8: Sort and Filter
    sort and filter are features that allow you to manipulate data in a spreadsheet based on standards. to sort data and filter data in excel 2007 you will use the sort & filter feature. below tipsmake.com will guide you how to filter data, arrange data with this feature in excel 2007.
  • How to sort data in Excel using Sort is extremely simpleHow to sort data in Excel using Sort is extremely simple
    with just a few operations to arrange data in excel, you can transform your spreadsheet to become neat and easy to see. let's find out with free download now!
  • Structure (Struct) in C #Structure (Struct) in C #
    in c #, a structure is a data type. it helps you create a single variable that keeps relevant data of diverse data types. the keyword struct in c # is used to create a structure.
  • Environment settings in Data structuresEnvironment settings in Data structures
    because c and c ++ languages ​​are the languages ​​that almost every university uses to teach, in this chapter i will guide you to install c and c ++ to run the examples in the configuration series. structure and algorithm.