This file contains hidden or 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
| use core::ptr; | |
| fn shell_sort<T: PartialOrd>(v: &mut [T]) { | |
| let len = v.len(); | |
| let mut gap = len / 2; | |
| while gap > 0 { | |
| for i in gap..len { | |
| unsafe { | |
| let tmp = ptr::read(v.get_unchecked(i)); | |
| let mut j = i; |
This file contains hidden or 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 comb_sort<T: PartialOrd>(v: &mut [T]) { | |
| let len = v.len(); | |
| let mut h = len; | |
| let mut swapped = true; | |
| while h != 1 || swapped { | |
| h = h * 10 / 13; | |
| if h < 1 { | |
| h = 1 | |
| } | |
| swapped = false; |
This file contains hidden or 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 partition<T: PartialOrd>(v: &mut [T]) -> (usize, usize) { | |
| let len = v.len(); | |
| let end = len - 1; | |
| if v[0] > v[end] { | |
| v.swap(0, end) | |
| } | |
| let mut j = 1; | |
| let mut g = end - 1; | |
| let mut k = 1; | |
| while k <= g { |
This file contains hidden or 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 stooge_sort<T: PartialOrd>(v: &mut [T]) { | |
| let len = v.len(); | |
| let end = len - 1; | |
| if v[end] < v[0] { | |
| v.swap(0, end); | |
| } | |
| if len >= 3 { | |
| let t = len / 3; | |
| stooge_sort(&mut v[0..len - t]); | |
| stooge_sort(&mut v[t..len]); |
This file contains hidden or 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 odd_even_sort<T: PartialOrd>(v: &mut [T]) { | |
| let len = v.len(); | |
| loop { | |
| let mut changed = false; | |
| for i in (0..len - 1).step_by(2) { | |
| if v[i] > v[i + 1] { | |
| v.swap(i, i + 1); | |
| changed = true; | |
| } | |
| } |
This file contains hidden or 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 shaker_sort<T: PartialOrd>(v: &mut [T]) { | |
| let len = v.len(); | |
| let mut swapped = true; | |
| let mut start = 0; | |
| let mut end = len - 1; | |
| while swapped { | |
| swapped = false; | |
| for i in start..end { | |
| if v[i] > v[i + 1] { | |
| v.swap(i, i + 1); |
This file contains hidden or 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 heapify<T: PartialOrd>(v: &mut [T], i: usize) { | |
| let len = v.len(); | |
| let mut j = i; | |
| let mut max = i; | |
| loop { | |
| let l = j * 2 + 1; | |
| let r = j * 2 + 2; | |
| if l < len && v[l] > v[max] { | |
| max = l; | |
| } |
This file contains hidden or 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 gnome_sort<T: PartialOrd>(v: &mut [T]) { | |
| let len = v.len(); | |
| let mut i = 1; | |
| while i < len { | |
| if v[i - 1] <= v[i] { | |
| i += 1; | |
| } else { | |
| v.swap(i - 1, i); | |
| i -= 1; | |
| if i == 0 { |
This file contains hidden or 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 bubble_sort<T: PartialOrd>(v: &mut [T]) { | |
| let len = v.len(); | |
| for i in 0..len - 1 { | |
| for j in 0..len - i - 1 { | |
| if v[j] > v[j + 1] { | |
| v.swap(j, j + 1) | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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 partition<T: PartialOrd>(v: &mut [T]) -> usize { | |
| let mut mid = 0; | |
| let len = v.len(); | |
| let end = len - 1; | |
| for i in 0..end { | |
| if v[i] < v[end] { | |
| v.swap(i, mid); | |
| mid += 1; | |
| } | |
| } |