Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate rand;
- use std::num::Wrapping;
- use rand::Rng;
- fn main() {
- // println!("Hello, world!");
- let mut x : Vec<i32> = array_generate(10,2);
- print(&x);
- quicksort(&mut x,0, 10 - 1);
- print(&x);
- }
- fn print(array:&Vec<i32>){
- println!("Array:");
- for i in 0..array.len(){
- print!("{} ", array[i]);
- }
- println!();
- }
- fn array_generate(size:i32, type_of_array:i32) -> Vec<i32>{
- let mut array = Vec::new();
- match type_of_array{
- 1 =>{
- for i in 0..size{
- array.push(i + 1);
- }
- return array;
- }
- 2 =>{
- for _i in 0..size{
- let rand_number = rand::thread_rng().gen_range(1, 2*size);
- array.push(rand_number);
- }
- return array;
- }
- 3 =>{
- for i in 0..size{
- array.push(size - i);
- }
- return array;
- }
- _ =>{
- println!("Inccorect type {} of array choosen!", type_of_array);
- return array;
- }
- }
- }
- fn quicksort(mas:&mut Vec<i32>, first:usize, last:usize) {
- if !mas.is_empty() {
- let mut f = first;
- let mut l = last;
- let mid = mas[(f + l) / 2];
- let mut count;
- loop {
- while mas[f] < mid {
- f += 1;
- }
- while mas[l] > mid {
- l -= 1;
- }
- if f <= l {
- count = mas[f];
- mas[f] = mas[l];
- mas[l] = count;
- f += 1;
- l -= 1;
- }
- if !(f < l) {
- break;
- }
- }
- if first < l {
- quicksort(mas, first, l);
- }
- if f < last {
- quicksort(mas, f, last);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement