forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBubble_Sort.rs
48 lines (41 loc) · 1 KB
/
Bubble_Sort.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::io;
use std::str::FromStr;
fn main() {
println!("Enter the numbers to be sorted (separated by space) : ");
let i = read_values::<i32>().unwrap();
let a = bubble_sort(i);
println!("Sorted array: {:?}", a)
}
fn bubble_sort(mut a: Vec<i32>) -> Vec<i32> {
for _ in 0..a.len() {
let mut flag = false;
for j in 0..a.len() - 1 {
if a[j] > a[j + 1] {
let temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = true
}
}
if !flag {
break;
}
}
a
}
fn read_values<T: FromStr>() -> Result<Vec<T>, T::Err> {
let mut s = String::new();
io::stdin()
.read_line(&mut s)
.expect("could not read from stdin");
s.trim()
.split_whitespace()
.map(|word| word.parse())
.collect()
}
/*
Output :
Enter the numbers to be sorted (separated by space) :
5 2 8 6 89
Sorted array: [2, 5, 6, 8, 89]
*/