Commit 44a566b8 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

init

parents
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "learn_thread"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[package]
name = "learn_thread"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.5"
pub mod sort;
\ No newline at end of file
use learn_thread::sort::{quick_sort, quick_sort_node};
use rand::{self, Rng};
use std::{
io::Error,
thread::{self, JoinHandle},
time::Instant,
};
const ARR_LEN: usize = 10_000_000;
// const ARR_LEN: usize = 10;
fn main() -> Result<(), Error> {
let mut random = rand::thread_rng();
let mut arr = vec![0u32; ARR_LEN];
for i in 0..ARR_LEN {
arr[i] = random.gen::<u8>() as u32;
}
let s1 = Instant::now();
let (a1, a2) = quick_sort_node(&mut arr, 0, ARR_LEN);
let mut works: Vec<[usize; 2]> = vec![[a1[0], a1[1]], [a2[0], a2[1]]];
let mut thread_list: Vec<JoinHandle<([usize; 2], Vec<u32>)>> = vec![];
while let Some(w) = works.pop() {
let mut a = Box::new(arr.clone());
let t: JoinHandle<([usize; 2], Vec<u32>)> = thread::spawn(move || {
let mut a_th = Box::new(&mut a[w[0]..w[1]]);
let w: [usize; 2] = w.clone();
quick_sort(&mut a_th);
(w, a_th.to_owned())
});
thread_list.push(t);
}
while let Some(th) = thread_list.pop() {
let (w, mut a) = th.join().unwrap();
for i in w[0]..w[1] {
arr[i] = a.remove(0);
}
}
println!("{:?}", s1.elapsed());
// println!("{:?}", arr);
Ok(())
}
use std::{
sync::{Arc, Mutex},
thread::{self, JoinHandle},
};
pub fn quick_sort_node(arr: &mut [u32], st: usize, ed: usize) -> ([usize; 2], [usize; 2]) {
let mut k = st as i32 - 1;
for i in st..ed {
if arr[i] <= arr[ed - 1] {
k += 1;
(arr[i], arr[k as usize]) = (arr[k as usize], arr[i]);
}
}
let len2 = ed as i32 - k - 1;
(
if k > 1 { [st, k as usize] } else { [0, 0] },
if len2 > 1 {
[(k + 1) as usize, ed]
} else {
[0, 0]
},
)
}
pub fn quick_sort(arr: &mut [u32]) {
let mut work_n = vec![[0, arr.len()]];
while let Some(w) = work_n.pop() {
if w[0] == w[1] {
continue;
}
let (r1, r2) = quick_sort_node(arr, w[0], w[1]);
work_n.push(r1);
work_n.push(r2);
}
}
fn quick_sort_thread(
arr: &Arc<Mutex<Vec<u32>>>,
works: &Arc<Mutex<Vec<[usize; 2]>>>,
w: [usize; 2],
) -> JoinHandle<()> {
let work_th = works.clone();
let ar = arr.clone();
let t = thread::spawn(move || {
let mut arr = ar.lock().unwrap();
let (r1, r2) = quick_sort_node(&mut arr, w[0], w[1]);
if r1[0] != r1[1] {
work_th.lock().unwrap().push(r1);
}
if r2[0] != r2[1] {
work_th.lock().unwrap().push(r2);
}
});
t
}
/**
* this function can make stack overflow
*/
pub fn normal_quick_sort(arr: &mut [u32]) {
let len = arr.len();
let mut k: i32 = -1;
for i in 0..len {
if arr[i] <= arr[len - 1] {
k += 1;
if i as i32 > k {
(arr[i], arr[k as usize]) = (arr[k as usize], arr[i]);
}
}
}
let len2 = len as i32 - k - 1;
if k > 1 {
normal_quick_sort(&mut arr[0..k as usize]);
}
if len2 > 1 {
normal_quick_sort(&mut arr[(k + 1) as usize..]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment