# Sort huge array get smallest 1000

**URL:** https://discourse.julialang.org/t/sort-huge-array-get-smallest-1000/8081
**Category:** Performance
**Created:** [December 31, 2017, 2:35pm UTC](https://discourse.julialang.org/t/sort-huge-array-get-smallest-1000/8081 "2017-12-31T14:35:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)
#### Post date: [December 31, 2017, 2:35pm UTC](https://discourse.julialang.org/t/sort-huge-array-get-smallest-1000/8081/1 "2017-12-31T14:35:20Z")

</div>

Is there a function which sorts an array up to a given size?  
I know that quicksort divides the array into “halves” every time which should make it easy to get the smallest 1000 in an array of a million values. Does someone already implemented something like this?

---

<div class="post-metadata">

### Author: ![bicycle1885](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bicycle1885/32/107_2.png) [@bicycle1885](https://discourse.julialang.org/u/bicycle1885)
#### Post date: [December 31, 2017, 2:45pm UTC](https://discourse.julialang.org/t/sort-huge-array-get-smallest-1000/8081/2 "2017-12-31T14:45:46Z")

</div>

What you want would be `select(x, range)`. It returns sorted elements in `x[range]` if `x` is sorted:

```julia
julia> x = shuffle(collect(linspace(0, 1, 50)));

julia> select(x, 1:10)
10-element Array{Float64,1}:
 0.0
 0.0204082
 0.0408163
 0.0612245
 0.0816327
 0.102041
 0.122449
 0.142857
 0.163265
 0.183673

```

Note that this function will be renamed to `partialsort` on Julia 1.0.

---

<div class="post-metadata">

### Author: ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)
#### Post date: [December 31, 2017, 3:04pm UTC](https://discourse.julialang.org/t/sort-huge-array-get-smallest-1000/8081/3 "2017-12-31T15:04:31Z")

</div>

`partialsort` is definitely the better name 😉

> [@bicycle1885](#):
>
> if x is sorted:

?  
`x` doesn’t have to be sorted, right? Or maybe I just misunderstand you. Anyway. Thanks 🙂

---

<div class="post-metadata">

### Author: ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)
#### Post date: [December 31, 2017, 3:09pm UTC](https://discourse.julialang.org/t/sort-huge-array-get-smallest-1000/8081/4 "2017-12-31T15:09:18Z")

</div>

Oh and is there a function which gives me the indices then?

---

<div class="post-metadata">

### Author: ![bicycle1885](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bicycle1885/32/107_2.png) [@bicycle1885](https://discourse.julialang.org/u/bicycle1885)
#### Post date: [December 31, 2017, 3:13pm UTC](https://discourse.julialang.org/t/sort-huge-array-get-smallest-1000/8081/5 "2017-12-31T15:13:46Z")

</div>

You’re right. `select(x, range)` returns the same value of `sort(x)[range]`.

If you want indices, you can use `selectperm` instead, which will be renamed to `partialsortperm` on Julia 1.0.

---

<div class="post-metadata">

### Author: ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)
#### Post date: [December 31, 2017, 5:00pm UTC](https://discourse.julialang.org/t/sort-huge-array-get-smallest-1000/8081/6 "2017-12-31T17:00:11Z")

</div>

You can also use `sort(..., alg=PartialQuickSort(1000))` to sort the array, and I think you can use `sortperm` with PartialQuickSort to get the indices.

[https://docs.julialang.org/en/stable/stdlib/sort/#Sorting-Algorithms-1](https://docs.julialang.org/en/stable/stdlib/sort/#Sorting-Algorithms-1)
