Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. extern crate rand;
  2. use std::num::Wrapping;
  3. use rand::Rng;
  4.  
  5.  
  6. fn main() {
  7. // println!("Hello, world!");
  8. let mut x : Vec<i32> = array_generate(10,2);
  9. print(&x);
  10. quicksort(&mut x,0, 10 - 1);
  11. print(&x);
  12. }
  13.  
  14. fn print(array:&Vec<i32>){
  15. println!("Array:");
  16. for i in 0..array.len(){
  17. print!("{} ", array[i]);
  18. }
  19. println!();
  20. }
  21.  
  22. fn array_generate(size:i32, type_of_array:i32) -> Vec<i32>{
  23. let mut array = Vec::new();
  24. match type_of_array{
  25. 1 =>{
  26. for i in 0..size{
  27. array.push(i + 1);
  28. }
  29. return array;
  30. }
  31. 2 =>{
  32. for _i in 0..size{
  33. let rand_number = rand::thread_rng().gen_range(1, 2*size);
  34. array.push(rand_number);
  35. }
  36. return array;
  37. }
  38. 3 =>{
  39. for i in 0..size{
  40. array.push(size - i);
  41. }
  42. return array;
  43. }
  44. _ =>{
  45.  
  46. println!("Inccorect type {} of array choosen!", type_of_array);
  47. return array;
  48. }
  49. }
  50. }
  51.  
  52. fn quicksort(mas:&mut Vec<i32>, first:usize, last:usize) {
  53. if !mas.is_empty() {
  54. let mut f = first;
  55.  
  56. let mut l = last;
  57. let mid = mas[(f + l) / 2];
  58. let mut count;
  59.  
  60. loop {
  61. while mas[f] < mid {
  62. f += 1;
  63. }
  64. while mas[l] > mid {
  65. l -= 1;
  66. }
  67. if f <= l {
  68. count = mas[f];
  69. mas[f] = mas[l];
  70. mas[l] = count;
  71. f += 1;
  72. l -= 1;
  73. }
  74. if !(f < l) {
  75. break;
  76. }
  77. }
  78. if first < l {
  79. quicksort(mas, first, l);
  80. }
  81. if f < last {
  82. quicksort(mas, f, last);
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement