[Data Structure and Algorithm] Internal Sorting Exercises

topic

There are a total of two after-school exercises about internal sorting below, all of which are ideas drawing questions and not algorithm design questions, so I will list them together here~


1. Take the key code sequence (503,087,512,061,908,170,897,275,653,426) as an example, manually execute the following sorting algorithm, and write out the key code status at the end of each sorting:

  1. direct insertion sort
  2. Hill sort (increment d[1]=5)
  3. quick sort
  4. heap sort

answer

1. Direct insertion sort

Explanation:
[] The elements inside are the selected key codes, and {}the elements inside are the key codes to be selected. Each time, {}the first element selected from the inside will be directly inserted into []it

STEP1:
[503], {087, 512, 061, 908, 170, 897, 275, 653, 426}
STEP2:
[087, 503], {512, 061, 908, 170, 897, 275, 653, 426}
STEP3:
[087, 503, 512], {061, 908, 170, 897, 275, 653, 426}
STEP4:
[061, 087, 503, 512], {908, 170, 897, 275, 653, 426}
STEP5:
[061, 087, 503, 512, 908], {170, 897, 275, 653, 426}
STEP6:
[061, 087, 170, 503, 512, 908], {897, 275, 653, 426}
STEP7:
[061, 087, 170, 503, 512, 897, 908], {275, 653, 426}
STEP8:
[061, 087, 170, 275, 503, 512, 897, 908], {653, 426}
STEP9:
[061, 087, 170, 275, 503, 512, 653, 897, 908], {426}
STEP10:
[061, 087, 170, 275, 426, 503, 512, 653, 897, 908]


2. Hill sort

Explanation:
A total of four Hill sorts have been performed, and the DK values ​​of each time are 5, 3, 2, 1,
starting from the first element before sorting and going backwards, and compared with the DK element behind it, the smaller one is in The bigger one in the front is followed until the DKth element in the back does not exist.

STEP1(DK = 5):
before sorting: 503 087 512 061 908 170 897 275 653 426
after sorting: 170 087 275 061 426 503 897 512 653 908
STEP2(DK = 3)
before sorting: 170 087 275 061 426 503 897 512 653 908
After sorting: 061 087 275 170 426 503 897 512 653 908
STEP3 (DK = 2)
Before sorting: 061 087 275 170 426 503 897 512 653 908 After sorting: 061 087 275 170 426 503 653 512 897 908
STEP4
(DK = 1)
Before sorting: 061 087 275 170 426 503 653 512 897 908
After sorting: 061 087 170 275 426 503 653 512 897 908


3. Quick Sort

Take the first key code 503 as the key

First step
insert image description here
Second
insert image description here
step
insert image description here
Third step Fourth step
insert image description here
Fifth step
insert image description here
Sixth step
insert image description here

Step 7,
insert image description here
Step 8, Step
insert image description here
9
insert image description here
So far, the first quick sorting has been completed. In the quick sorting using 503 as a keyword, those less than 503 are on the left, and those greater than 503 are on the right. But the left and right sides are unordered, so it can be seen that the left and right subsequences need to be processed recursively

  • Sort its left subsequence 426 087 275 061 170 (obtainable by referring to the above steps)
    061 087 170 275 426
  • In the same way, the right subsequence 897 908 653 512 can be recursively sorted to get
    512 653 897 908

From this the whole sort is done
061 087 170 275 426 512 061 087 170 275 426


4. Heap sort

The initial big top heap
insert image description here
starts heap sorting
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here


2. Determine whether the following sequence is a minimum heap? If not, tune it to a min-heap.

  1. {100,86,48,73,35,39,42,57,66,21}
  2. {12,70,33,65,24,56,48,92,86,33}

answer

The solution is divided into two parts, namely the solution of sequence one and the solution of sequence two.

number one

The original sequence heap image
insert image description here
is adjusted to the minimum heap
insert image description here

Rank two

The original sequence heap image
insert image description here
is adjusted to the minimum heap
insert image description here


conclusion

  Because it is an algorithm side dish, the methods and ideas provided may not be very good, please bear with me~ If you have any questions, please leave a message to discuss. If you think this article is helpful to you, can you give me a free like? The communication between us is my biggest motivation!

Guess you like

Origin blog.csdn.net/Zchengjisihan/article/details/131581593