**directory**

Inserting sort based on the linked list

### Inserting sort based on the linked list

https://blog.csdn.net/Doutd_y/article/details/81557320

```
ListNode * InsertionSort(ListNode *head)
{
if (head == NULL)
return 0;
ListNode *sortedList = new ListNode(0);
while (head != NULL)
{
ListNode *temp = head->next;
ListNode *inser = sortedList;
while (inser->next != NULL && inser->next->val < head->val)
{
inser = inser->next;
}
head->next = inser->next;
inser->next = head;
head = temp;
}
}
```

### Inserting sort based on array

1) First of all, the first two data of the array are sorted from small to large

2) Then compare the 3rd data with the two data of the order, insert the third data to the appropriate position

3) Then, insert the fourth data into the first three data that has been discharged

4) Repeat the above processes until the last element is inserted into the right position.

```
void Insertionsort (int *a, int len)
{{
int i, j, temp;
for (i = 1; I <len; I ++)
{{
j = i -1; // j is the latter of the last element of the sorted sequence in the current large sorting sequence
temp = a [i]; // Temp is the number to be inserted currently to be inserted
While (j> = 0 && test <a [j]) // j> = 0 guarantees that the number of inserts to be inserted is smaller than the current TEMP, and continues to look for the position where the TEMP is inserted J.
{{
a [j + 1] = a [j]; // test value is relatively small, then A [j] moves backwards and updates the position j
j ---;
}
a [j+1] = temp; // When the exit is exit the while, J has made a self-reduction (J--), so the exit of the while represents the small TEMP small element (the bid is j), then the TEMP is placed in the TEMP in the TEMP in the TEMP in the TEMP on the TEMP. Behind j.
}
}
```

### SHELL Sorting based on array

Thoughts based on inserting sorting: The complexity of the algorithm inserted sort is, but if the sequence is raised as a positive order, it can be improved to, and it is relatively simple to insert the sorting algorithm. Hill’s sorting uses these two points to get a improved insertion sort.

1) The number of n elements is divided into n/2 digital sequences. The first data and n/2+1 data are paired, …

2) One cycle makes each sequence in a good order.

3) Then, then become n/4 sequences, sort again.

4) Repeat the above process until the sequence is reduced to 1.

```
void shellsort (int *a, int len)
{{
int i, j, gap;
int Temp;
for (gap = len / 2; gap> = 1; gap / = 2) // Control the number of sorting times
{{
For (i = gap; i <len; i ++) // Taking the Gap as the stepgrade, compare the size of A [i] and a [i-gap], that is, the size of TEMP and a [J]
{{
temp = a [i];
j = i -gap;
While (j> = 0 && test <a [j]) // Find the appropriate insertion position, but the step is GAP
{{
a [j + gap] = a [j];
j = j -gap;
}
a [j + gap] = test;
}
}
}
```