Back to 课程

Computer-Science-A-level-Ocr

0% Complete
0/0 Steps
  1. 3-3-networks
    8 主题
  2. 3-2-databases
    7 主题
  3. 3-1-compression-encryption-and-hashing
    4 主题
  4. 2-5-object-oriented-languages
    7 主题
  5. 2-4-types-of-programming-language
    4 主题
  6. 2-3-software-development
    5 主题
  7. 2-2-applications-generation
    6 主题
  8. 2-1-systems-software
    8 主题
  9. 1-3-input-output-and-storage
    2 主题
  10. 1-2-types-of-processor
    3 主题
  11. 1-1-structure-and-function-of-the-processor
    1 主题
  12. structuring-your-responses
    3 主题
  13. the-exam-papers
    2 主题
  14. 8-2-algorithms-for-the-main-data-structures
    4 主题
  15. 8-1-algorithms
    10 主题
  16. 7-2-computational-methods
    11 主题
  17. 7-1-programming-techniques
    14 主题
  18. 6-5-thinking-concurrently
    2 主题
  19. 6-4-thinking-logically
    2 主题
  20. 6-3-thinking-procedurally
    3 主题
  21. 6-2-thinking-ahead
    1 主题
  22. 6-1-thinking-abstractly
    3 主题
  23. 5-2-moral-and-ethical-issues
    9 主题
  24. 5-1-computing-related-legislation
    4 主题
  25. 4-3-boolean-algebra
    5 主题
  26. 4-2-data-structures
    10 主题
  27. 4-1-data-types
    9 主题
  28. 3-4-web-technologies
    16 主题
课 Progress
0% Complete

Insertion sort

What is an insertion sort?

  • The insertion sort sorts one item at a time by placing it in the correct position of an unsorted list

  • This process repeats until all items are in the correct position

  • Specifically, the current item being sorted is compared to each previous item

  • If it is smaller than the previous item then the previous item is moved to the right and the current item takes its place

  • If the current item is larger than the previous item then it is in the correct position and the next current item is then sorted as illustrated below

Performing the insertion sort

bS-Ng1JW_insertion-sort-revision-notes-computer-science

Figure 2: Performing the Insertion sort

Time complexity of an insertion sort

  • In the above algorithm, one statement is present, a for loop with three statements and a nested while loop that contains two statements

  • The time complexity for the insertion sort would be 3n*2n + 1, giving 6n2 + 1. 3n comes from the for loop having three statements inside it, not including the while. 2n comes from the while loop having two statements inside it

  • Removing coefficients and dominated factors leaves O(n2) as the worst case scenario

  • The best case scenario would be an already sorted list which means each item is checked one at a time giving a time complexity of O(n)

The average case scenario would be a half sorted list giving O(n2/2), which when coefficients are removed still gives O(n2)

Space complexity of an insertion sort

  • As the insertion sort requires no additional space, it is space complexity O(1)

Tracing an insertion sort

  • Tracing through the insertion sort involves starting at element 1 (not 0)

  • i increments through each element one at a time and each item is compared to the one previous. If the previous item is bigger or equal it is set to list[position]

  • If position equals 0 then the iteration is at the start of the list and the next iteration begins

Trace Table of the Insertion sort

<td class=”border border-dark ContentB

list

i

item

position

list[position-1]

list[position]

[5, 9, 4, 2, 7, 1, 2, 4, 3]

1

9

1

5

9

 

2

4

2

9

9

 

 

 

1

5

5

 

 

 

0

 

 

[4, 5, 9, 2, 7, 1, 2, 4, 3]

3

2

3

9

9

 

 

 

2

5

5

 

 

 

1

4

4

 

 

 

0

 

 

[2, 4, 5, 9, 7, 1, 2, 4, 3]

4

7

4

9

9

 

 

 

3

5

7

[2, 4, 5, 7, 9, 1, 2, 4, 3]

5

1

5

9

9

 

 

 

4

7

7

 

 

 

3

5

5

 

 

 

2

4

4

 

 

 

1

2

2

 

 

 

0

 

1

[1, 2, 4, 5, 7, 9, 2, 4, 3]

6

2

6

9

9

 

 

 

5

7

7

 

 

 

4

5

5

 

 

 

3

4

4

 

 

 

2

2

2

[1, 2, 2, 4, 5, 7, 9, 4, 3]

7

4

7

9

9

 

 

 

6

7

7

 

 

 

5

5

5

 

 

 

4

4

4

[1, 2, 2, 4, 4, 5, 7, 9, 3]

8

3

8

9

9

 

 

 

7

7

7

 

 

 

6

5

5

 

 

 

5

4

4

 

 

 

4

4

4

 

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注