Skip to content

Latest commit

 

History

History

Insertion Sort

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Insertion Sort

A sorting algorithm that builds the final sorted array one at a time. When people sort playing cards, most use a method similar to insertion sort.

Time Complexity

Best Case: Ω(n)

Average Case: θ(n²)

Worst Case: O(n²)

where n represents the size of the array.

Space Complexity

Worst Case: O(1)

Function Description

The function insertion_sort has the following parameter(s):

  • a: the vector of integers to be sorted

Return value: None.

It sorts the vector inplace in ascending order. The original vector gets modified as it is passed by reference.

Input Format

The first line contains an integer n, the size of array.

The next line contains n space-separated integers a[i] where 0 ≤ i < n.

Output Format

The array a in sorted order.

Sample Input

5
5 3 1 2 4

Sample Output

1 2 3 4 5