File tree Expand file tree Collapse file tree 3 files changed +31
-1
lines changed
chapters/sorting_searching/bubble Expand file tree Collapse file tree 3 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 5
5
Jeremie Gillet (- Jie -)
6
6
Salim Khatib
7
7
Hitesh C
8
- Jess 3Jane
8
+ Pen Pal
9
+ Jess 3Jane
Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
20
20
[ import, lang:"haskell"] ( code/haskell/bubbleSort.hs )
21
21
{% sample lang="cpp" %}
22
22
[ import, lang:"c_cpp"] ( code/c++/bubblesort.cpp )
23
+ {% sample lang="rs" %}
24
+ [ import:6-19, lang:"rust"] ( code/rust/bubble_sort.rs )
23
25
{% sample lang="d" %}
24
26
[ import:3-18, lang:"d"] ( code/d/bubble_sort.d )
25
27
{% endmethod %}
Original file line number Diff line number Diff line change
1
+ extern crate rand; // External crate that provides random number generation tools
2
+
3
+ use rand:: { thread_rng, Rng } ; // Used for random number generation
4
+ use rand:: distributions:: Uniform ; // Used for a uniform distribution
5
+
6
+ fn bubble_sort ( a : & mut [ u32 ] ) {
7
+ let n = a. len ( ) ;
8
+
9
+ for _ in 0 ..n {
10
+ for j in 1 ..n {
11
+ if a[ j - 1 ] > a[ j] {
12
+ a. swap ( j, j - 1 ) ;
13
+ }
14
+ }
15
+ }
16
+ }
17
+
18
+ fn main ( ) {
19
+ let mut rng = thread_rng ( ) ; // Create random number generator
20
+ let num_range = Uniform :: new_inclusive ( 0 , 10000 ) ; // Obtain uniform distribution of range [0, 10000]
21
+ let mut rand_vec: Vec < u32 > = rng. sample_iter ( & num_range) . take ( 10 ) . collect ( ) ;
22
+ // Generates random values over that range, take 10 values from it and collect in vector
23
+
24
+ println ! ( "Before sorting: {:?}" , rand_vec) ;
25
+ bubble_sort ( & mut rand_vec) ;
26
+ println ! ( "After sorting: {:?}" , rand_vec) ;
27
+ }
You can’t perform that action at this time.
0 commit comments