Created
January 20, 2015 12:41
-
-
Save mgadzhi/d7c9f923d8a54ab15007 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn swap(arr: &mut Vec<int>, i: uint, j: uint) -> &Vec<int> { | |
let buf = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = buf; | |
arr | |
} | |
fn insertion_sort(arr: &mut Vec<int>) -> &Vec<int> { | |
for i in range(1, arr.len()) { | |
let key = arr[i]; | |
let mut j = i - 1; | |
while j > 0 && arr[j] > key { | |
arr[j + 1] = arr[j]; | |
j = j - 1; | |
} | |
arr[j + 1] = key; | |
if arr[0] > arr[1] { | |
swap(arr, 0, 1); | |
} | |
} | |
arr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment