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
// a simple bubble sort algorithm implemented in Rust | |
fn main(){ | |
let data = &mut [3, 4, 1, 5, 2]; | |
loop { | |
let mut has_swapped = false; | |
for i in 0..data.len() - 1 { | |
let a = i; | |
let b = (i+1) as usize; | |
if data[a] > data[b] { |
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 main(){ | |
use std::io::stdin; | |
use std::io::stdout; | |
use std::io::Write; | |
// prompt the user | |
print!("Enter numbers: "); | |
let _=stdout().flush(); | |
// get user input |
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 quicksort(data: &mut Vec<i32>, begin: i32, end: i32){ | |
if begin < end { | |
// sort the data left and right | |
let mid = partition(data, begin, end); | |
// break the data in half, and then | |
// repeat the process recursively | |
// on each side | |
quicksort(data, begin, mid-1); | |
quicksort(data, mid+1, end); | |
} |
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
function quicksort(data, begin, end){ | |
if(begin < end){ | |
// sort the data left and right | |
const mid = partition(data, begin, end); | |
// break the data in half, and then | |
// repeat the process recursively | |
// on each side | |
quicksort(data, begin, mid-1); | |
quicksort(data, mid+1, end); |
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
function partition(data, begin, end){ | |
let pivot = data[Math.floor((begin+end)/2)]; | |
let i = begin - 1; | |
let j = end + 1; | |
while(true) { | |
while(true) { | |
i = i + 1; | |
if(data[i] >= pivot){ | |
break; | |
} |
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
function heapsort(arr){ | |
heapify(arr); | |
var last = arr.length-1; | |
// reverse loop - max swap position | |
for(let i=last; i>0; i--){ | |
swap(0, i, arr); | |
siftDown(0, i-1, arr); |
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
function swap(data, a, b){ | |
let temp = data[a]; | |
data[a] = data[b]; | |
data[b] = temp; | |
} | |
function insertionSort(data){ | |
for(let i=0; i < data.length; i++){ | |
for(let j=i; j > 0; j--){ | |
if(data[j] < data[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
function copyOver(data1, data2, begin, end){ | |
for(let i=begin; i<=end; i++){ | |
data2[i] = data1[i]; | |
} | |
} | |
function merge(data, begin, mid, end){ | |
let left = begin; | |
let right = mid+1; | |
let temp = []; |
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
function swap(data, a, b){ | |
let temp = data[a]; | |
data[a] = data[b]; | |
data[b] = temp; | |
} | |
function selectionSort(data){ | |
for(let i=0; i < data.length-1; i++){ | |
let smallest = 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
function swap(data, a, b){ | |
let temp = data[a]; | |
data[a] = data[b]; | |
data[b] = temp; | |
} | |
function bubbleSort(data){ | |
let totalCount = data.length; | |
let progress = 0; | |
for(let i=1; i < totalCount; i++){ |
OlderNewer