-
Notifications
You must be signed in to change notification settings - Fork 213
/
insertion_sort.go
71 lines (53 loc) · 3.07 KB
/
insertion_sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time.
It starts with the second element of the array, compares it with the first element, and swaps them
if necessary. It then continues to the third element, compares it with the first and second elements,
and swaps it into the correct position. This process continues until the last element is compared and
sorted into its correct position in the sorted array.
At each iteration, the algorithm assumes that the subarray to the left of the current element is already
sorted, and it searches for the correct position of the current element within that subarray by comparing
it to each element from right to left until it finds the correct position. Once it finds the correct
position, it shifts all the larger elements to the right to make space for the current element and
inserts it in its correct position.
Insertion sort has an average-case time complexity of O(n^2) but can perform better than other O(n^2)
algorithms, such as bubble sort or selection sort, in certain cases. It is also an efficient algorithm
for small data sets or partially sorted data sets.
In this implementation, we define a function called InsertionSort that takes an array of integers and sorts
it in ascending order using the Insertion Sort algorithm.
The algorithm works by iterating over the array from the second element to the end.
For each element, it compares it with the previous elements in the array and inserts it in the correct position.
The current variable holds the value of the current element being compared.
The j variable holds the index of the previous element being compared.
The loop compares the current value with the previous values in the array and shifts the values to the right to make space for the current value.
Once the correct position is found, the current value is inserted into the array.
Finally, the sorted array is returned. In the main function, we define an array of integers, sort it using the InsertionSort function, and print the sorted array.
Sample input: [0, 2, 1,-1, 10, 3, 4]
Output: [-1 0 1 2 3 4 10]
*/
package main
import "fmt"
// InsertionSort is a function that takes an array of integers and sorts it in
// ascending order using the Insertion Sort algorithm.
func InsertionSort(arr []int) []int {
// Iterate over the array from the second element to the end
for i := 1; i < len(arr); i++ {
// Set the current value and the previous index
current := arr[i]
j := i - 1
// Compare the current value with the previous values in the array
for j >= 0 && arr[j] > current {
// Shift the values to the right to make space for the current value
arr[j+1] = arr[j]
j--
}
// Insert the current value in the correct position
arr[j+1] = current
}
// Return the sorted array
return arr
}
func main() {
arr := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}
sortedArr := InsertionSort(arr)
fmt.Println(sortedArr)
}