Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
quickselect
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include<bits/stdc++.h> using namespace std; int part(vector<int>&v,int pivot,int lo,int hi) { int i=lo; int j=lo; while(j<hi) { if(v[j]<=pivot) { swap(v[i],v[j]); i++; j++; } else if(v[j]>pivot) { j++; } } swap(v[i],v[j]); return i; } int qs(vector<int>&v,int lo,int hi,int k,int n) { int pivot=v[hi]; int pi=part(v,pivot,lo,hi); if(k<pi) { return qs(v,lo,pi-1,k,n); } else if(k>pi) { return qs(v,pi+1,hi,k,n); } else { return v[pi]; } } int main() { int t; cin>>t; while(t--) { int n; cin>>n; vector<int>v(n); for(int i=0;i<n;i++) { cin>>v[i]; } int k;cin>>k; cout<<qs(v,0,n-1,k-1,n)<<endl; } return 0; }
g++
1 5 1 6 2 3 5 3
Show compiler warnings
[
+
] Compiler args
[
-
]
Show input
Compilation time: 1.54 sec, absolute running time: 0.17 sec, cpu time: 0.02 sec, memory peak: 5 Mb, absolute service time: 1,76 sec
edit mode
|
history
|
discussion
3