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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*!
Double sort works by separating the elements into pairs of two called nodes, this allows
to order the two elements within the node extremely fast since it's a 1:1 memory swap.

Grouping elements by nodes allows a simple loop statement to order the whole vector in n/2 - 1 reads maximum by utilizing BinaryHeaps

# Usage

```toml
[dependencies]
double_sort = "1.2.0"
```
*/

#[derive(PartialEq,PartialOrd,Eq,Debug,Clone, Copy,Ord)]
struct Node<T>(T,Option<T>); 

impl<T: PartialOrd + Copy> Node<T> {
    //Rearranges the node in the correct order
    fn order(&mut self) {
        if let Some(_number) = self.1 {
            switch(&mut self.0,self.1.as_mut().unwrap());
        }
    }

    //Informs if there is None present in the structure
    fn none_present(&self) -> bool {
        if self.1 == None {
            true
        } else {
            false
        }
    }

    fn contains(&self,element:T) -> bool {
        if element == self.0 {
            true
        } else {
            false
        }
    }

    fn change_vector(&self, array: &mut Vec<T>, index: usize) {
        array[index] = self.0;
        if let Some(number) = self.1 {
            array[index+1] = number;
        }
    }
}

//Checks if the left element is the smallest and if not then swaps with the right element so they are ordered from left to right.
fn switch<T: PartialOrd>(left: &mut T,right: &mut T) -> bool {
    if left > right {
        std::mem::swap(left,right);
        return true;
    } else {
    false
    }
}

///Sorts a vector of elements by pairing them and ordering them by the lowest number, then exchanging elements with neighbours until sorted.
/// # Example 

///```rust
///use double_sort::double_sort;

///fn main() {
///    let mut vector = vec![48,23,78,67,89,22,33,44];
///  
///    double_sort(&mut vector);
/// 
///    assert_eq!(vector,[22,23,33,44,48,67,78,89]);
///}
///```
pub fn double_sort<T: Copy + Ord>(array: &mut Vec<T>) {


    if array.len() <= 2 {

        if array.len() == 1 {
            return;
        }

        let mut node = Node(array[0],Some(array[1]));
        node.order();

        node.change_vector(array, 0);
        
        return;
    }

    //Mutable values used to control the while loop
    let mut _counter = 0; //Amount of times the loop ran for


    let mut vector = Vec::new();

    let iter = array.chunks_exact(2);
    let mut node: Node<T>;
    let temp_node: Option<Node<T>>;

    if !iter.remainder().is_empty() {
        temp_node = Some(Node(iter.remainder()[0],None));

        temp_node.unwrap().order();
    } else {
        temp_node = None;
    }

    for chunk in iter {
        node = Node(chunk[0],Some(chunk[1]));

        node.order();

        vector.push(node);
    }

    if let Some(_element) = temp_node {
        vector.push(temp_node.unwrap());
    }
    

    let mut reference_vec = Vec::new();
    let mut temp_vec = Vec::new();

    for node in &vector {
        temp_vec.push(node.0);
    }

    double_heap_sort(&mut temp_vec);

    for reference in &temp_vec {
        let left_node = *vector.iter().find(|x| x.contains(*reference) == true).unwrap();

        reference_vec.push(left_node);
    }

    vector = reference_vec;


    let mut index = 0;

    //Final sort of the values by comparing left and right values of neighbouring nodes
    loop {
        let mut left = vector[_counter];

        if _counter == vector.len() - 1 {
            left.change_vector(array, index);
            break;
        }

        let mut right = vector[_counter+1];

        let switched: bool; //Checks whether anything was changed

        if let Some(_number) = left.1 {
            switched = switch(left.1.as_mut().unwrap(),&mut right.0);
        } else {
            switched = switch(&mut left.0,&mut right.0);
        }

        let mut end_left = vector[vector.len() - 2];
        let mut end_right = vector[vector.len() - 1];

        if let Some(_number) = end_left.1 {
            switch(end_left.1.as_mut().unwrap() ,&mut end_right.0);
        } else {
            switch(&mut end_left.0,&mut end_right.0);
        }

        if !switched {
            left.change_vector(array, index);
            vector[_counter] = left;

            _counter += 1;

            if left.none_present() {
                index += 1;
            } else {
                index += 2;
            }

            if right.none_present() {
                right.change_vector(array, index);
                vector[_counter] = right;

                if _counter == vector.len() - 1 {
                    break;
                }

                index += 1;
                _counter += 1;

                if _counter == vector.len() - 2 {
                    let left = vector[_counter];

                    left.change_vector(array, index);

                    break;
                }
                
            }
            continue;
        }

        left.order();
        right.order();

        vector[_counter] = left;
        vector[_counter+1] = right;

        if _counter == vector.len() - 1 {
            left.change_vector(array, index);

            if left.none_present() {
                index += 1;
            } else {
                index += 2;
            }

            right.change_vector(array, index); //Index out of bounds on random_sort
            break;
        }

        //Increment _counter
        _counter += 1;


        //Everything is pushed back into the heap so nothing is lost.
        left.change_vector(array, index);

        if left.none_present() {
            index += 1;
        } else {
            index += 2;
        }

        let mut temporary_vec = Vec::new();
        let mut reference_vec = Vec::new();

        for node in &vector {
            temporary_vec.push(node.0);
        }
    
        double_heap_sort(&mut temporary_vec);

        if temporary_vec != temp_vec {
            for reference in &temporary_vec {
                let left_node = *vector.iter().find(|x| x.contains(*reference) == true).unwrap();
        
                reference_vec.push(left_node);
            }
        
            vector = reference_vec;
        }

    }

}



///Works in the same way that [double_sort] does but utilizes [BinaryHeaps](std::collections::BinaryHeap). This provides a faster but less logarithmic sort since
/// it only shaves half [heap.pop()](https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#method.pop) runs.
/// # Note
/// This was the previous [double_sort] in 1.0.0
/// 
///# Example 
/// 
///```rust
///use double_sort::double_heap_sort;
/// 
///fn main() {
///    let mut vector = vec![48,23,78,67,89,22,33,44];
///  
///    double_heap_sort(&mut vector);
/// 
///    assert_eq!(vector,[22,23,33,44,48,67,78,89]);
///}
///```

pub fn double_heap_sort<T: Copy + Ord>(array: &mut Vec<T>) {

    use std::collections::BinaryHeap;
    use std::cmp::Reverse;
    
    if array.len() <= 2 {
        let mut node = Node(array[0],array.get(1).cloned());
        node.order();

        node.change_vector(array, 0);
        
        return;
    }

    //BinaryHeap is used due to it's efficient ordering capabilities.
    let mut heap = BinaryHeap::new();

    //Mutable values used to control the while loop
    let mut _counter = 0; //Amount of times the loop ran for

    let iter = array.chunks_exact(2);
    let mut node: Node<T>;

    if !iter.remainder().is_empty() {
        node = Node(iter.remainder()[0],None);

        node.order();
        heap.push(Reverse(node));
    }

    for chunk in iter {
        node = Node(chunk[0],Some(chunk[1]));

        node.order();
        heap.push(Reverse(node));
    }

    let mut index = 0;

    //Final sort of the values by comparing left and right values of neighbouring nodes
    loop {

        if heap.is_empty() {
            break;
        }

        let mut left = heap.pop().unwrap().0;

        if heap.is_empty() {
            left.change_vector(array, index);
            break;
        }

        let mut right = heap.pop().unwrap().0;

        let switched: bool; //Checks whether anything was changed

        if let Some(_number) = left.1 {
            switched = switch(left.1.as_mut().unwrap(),&mut right.0);
        } else {
            switched = switch(&mut left.0,&mut right.0);
        }

        if !switched {
            left.change_vector(array, index);

            //Increment the times where read did nothing
            _counter += 1;

            if left.none_present() {
                index += 1;
            } else {
                index += 2;
            }

            if right.none_present() {
                right.change_vector(array, index);

                index += 1;

                if heap.len() == 1 {
                    let left = heap.pop().unwrap().0;

                    left.change_vector(array, index);

                    break;
                }
                
            } else {
                heap.push(Reverse(right));
            }

            continue;
        }

        left.order();
        right.order();

        //Increment _counter
        _counter += 1;

        if heap.is_empty() {
            left.change_vector(array, index);

            if left.none_present() {
                index += 1;
            } else {
                index += 2;
            }

            right.change_vector(array, index); //Index out of bounds on random_sort
            break;
        }

        //Everything is pushed back into the heap so nothing is lost.
        left.change_vector(array, index);
        
        if left.none_present() {
            index += 1;
        } else {
            index += 2;
        }

        heap.push(Reverse(right));

    }
}