Write a bubble sort in Rust
- User
Write a bubble sort in Rust
- Assistant
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. This is repeated until the list is sorted.
Here’s an example of bubble sort in Rust:
fn bubble_sort(mut list: Vec<i32>) -> Vec<i32> { let n = list.len(); for i in 0..n { for j in 0..n-i-1 { if list[j] > list[j+1] { list.swap(j, j+1); } } } }This function takes a vector of integers as an input argument and returns a sorted vector using the bubble sort algorithm.
- User
Could you format it in a code block with Markdown and add indentation to make the code easier to read ?
- Assistant
Sure! Here's the code block with Markdown formatting and indentation:
fn bubble_sort(mut list: Vec<i32>) -> Vec<i32> { let n = list.len(); for i in 0..n { for j in 0..n-i-1 { if list[j] > list[j+1] { list.swap(j, j+1); } } } list }This function takes a vector of integers as an input argument and returns a sorted vector using the bubble sort algorithm.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)