Skip to content

Commit

Permalink
insertion sort of an array
Browse files Browse the repository at this point in the history
  • Loading branch information
suparna-khamaru authored Jun 16, 2020
1 parent 3cd468b commit 7c3281e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sort_InsertionSort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

var numbers = [12,66,34,99,9,2,1,5,6]

for i in 0..<numbers.count {

let key = numbers[i]
var prev = i - 1

while prev >= 0 && numbers[prev] > key {

numbers[prev + 1] = numbers[prev]
prev = prev - 1
}
numbers[prev + 1] = key
}

print(numbers) // [1, 2, 5, 6, 9, 12, 34, 66, 99]

0 comments on commit 7c3281e

Please sign in to comment.