Problem:
Partition
Quick sort is based on the Divide-and-conquer approach. In QuickSort(A, p, r), first, a procedure Partition(A, p, r) divides an array A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is less than or equal to A[q], which is, inturn, less than or equal to each element of A[q+1..r]. It also computes the index q.
In the conquer processes, the two subarrays A[p..q-1] and A[q+1..r] are sorted by recursive calls of QuickSort(A, p, q-1) and QuickSort(A, q+1, r).
Your task is to read a sequence A and perform the Partition based on the following pseudocode:
Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4     do if A[j] <= x
5        then i = i+1
6            exchange A[i] and A[j] 
7 exchange A[i+1] and A[r]
8 return i+1
Note that, in this algorithm, Partition always selects an element A[r] as a pivot element around which to partition the array A[p..r].
Input
The first line of the input includes an integer
n
, the number of elements in the sequence A.
In the second line,
A
i
(
i
= 1,2,...,
n
), elements of the sequence are given separated by space characters.
Output
Print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character. The element which is selected as the pivot of the partition should be indicated by
[
]
.
Constraints
1 ≤
n
≤ 100,000
0 ≤
A
i
≤ 100,000
Sample Input 1
12
13 19 9 5 12 8 7 4 21 2 6 11
Sample Output 1
9 5 8 7 4 2 6 [11] 21 13 19 12

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int partition(vector<int>& A, int p, int r) {
    int x = A[r];      // pivot
    int i = p - 1;
    for (int j = p; j <= r - 1; j++) {
        if (A[j] <= x) {
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r]);
    return i + 1;
}

int main() {
    int n;
    cin >> n;
    vector<int> A(n);
    for (int i = 0; i < n; i++) cin >> A[i];

    int q = partition(A, 0, n - 1);

    for (int i = 0; i < n; i++) {
        if (i == q) {
            cout << "[" << A[i] << "]";
        } else {
            cout << A[i];
        }
        if (i != n - 1) cout << " ";
    }
    cout << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3468
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int partition(vector<int>&A,int p,int r){
    int x = A[r];
    int i = p-1;
    for(int j = p;j<=r-1;++j){
        if(A[j] <= x){
        i = i+1;
        swap(A[i], A[j]);
        }
    }
    swap(A[i+1], A[r]);
    return i+1;
    }

int main(){
    int n;
    cin >> n;
    vector<int> A(n);
    for(int i=0;i<n;++i){
        cin >> A[i];
    }
    int x = partition(A,0,n-1);
    for(int j=0;j<x;++j){
        cout << A[j] << " ";
    }
    cout << "[" << A[x] << "]";
    for(int k=x+1;k<n;++k){
        cout << " " << A[k];
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3396
final_rank: 2

code_snippet: #include<iostream>
#include<vector>

using namespace std;

int partition(vector<int>&A, int p, int r){
    int x;
    int i;
    x = A[r];
    i = p-1;
    for(int j = p; j < r; j++){
        if(A[j] <= x){
            i = i + 1;
            swap(A[i] , A[j]);
        }
    }
    swap(A[i+1] , A[r]);
    return i + 1;
}

int main(){
    int n;
    cin >> n;
    vector<int>S(n);
    for(int i = 0;i < n;i++){
        int j;
        cin >> j;
        S[i] = j;
    }

    int pivot_index = partition(S, 0, n-1);

    for(int i = 0; i < n; i++){
        if(i == pivot_index){
            cout << "[" << S[i] << "]";
        }else{
            cout << S[i];
        }
        if(i != n-1){
            cout << " " ;
        }
    }
    cout << endl;
    return 0;

}
accuracy: 1
time_ms: 10
memory_kb: 3440
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int partition(vector<int>& A, int p, int r){
    int x = A[r];
    int i = p - 1;
    for(int j=p; j<=r-1; j++){
        if(A[j]<=x){
            i += 1;
            int k = A[i];
            A[i] = A[j];
            A[j] = k;
        }
    }
    int l = A[i+1];
    A[i+1] = A[r];
    A[r] = l;
    return i+1;
}


int main(){
    int n;
    cin >> n;
    vector<int>A(n);
    for(int i=0; i<n; i++){
        cin >> A[i];
    }

    int o = partition(A, 0, n-1);
    
    for(int i=0; i<n-1; i++){
        if(i==o){
            cout << "[" << A[i] << "]" << " ";
        }
        else{
            cout << A[i] << " ";
        }
    }
        
    if(n-1==o){
        cout << "[" << A[n-1] << "]";
    }
    else{
        cout << A[n-1];
    }
    cout << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3576
final_rank: 9

code_snippet: #include<iostream>
using namespace std;

int partition(int A[], int p, int r){
    int x = A[r];
    int i = p-1;
    for(int j=p;j<r;j++){
        if(A[j]<=x){
            i++;
            int s=A[i];
            A[i] = A[j];
            A[j] = s;
        }
    }
    int s = A[i+1];
    A[i+1] = A[r];
    A[r] = s;
    return i+1;
}


int main(){
    int n;
    cin >> n;
    
    int A[n];
    for(int k=0;k<n;k++){
        cin >> A[k];
    }
    
    int x = partition(A, 0, n-1);
    
    for(int j=0;j<n;j++){
        if(j==x){
            cout << "[" << A[j] << "]" << " ";
        }
        else if(j!=n-1){
            cout << A[j] << " ";
        }
        else{
            cout << A[j] << endl;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3708
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

int pertition(vector<int>& A, int p, int r){
    int x = A[r];
    int i = p - 1;
    for (int j = p; j < r; j++){
        if (A[j] <= x){
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r]);

    return i + 1;
}

int main(void){
    int n;
    cin >> n;
    vector<int> A(n);
    for (int i = 0; i < n; i++){
        cin >> A[i];
    }

    int t = pertition(A, 0, n - 1);

    for (int i = 0; i < n; i++){
        if (i == n - 1){
            cout << A[i] << endl;
        } else if (i == t){
            cout << "[" << A[i] << "] ";
        } else {
            cout << A[i] << " ";
        }
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3604
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

int Partition(vector<int> &A, int p, int r){
    int x = A[r];
    int i = p - 1;
    for(int j=p;j<r;j++){
        if(A[j] <= x){
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i+1], A[r]);
    return i+1;
}

int main(){
    int n;
    cin >> n;
    vector<int> A(n);
    for(int i=0;i<n;i++){
        cin >> A[i];
    }
    int q = Partition(A, 0, n-1);
    for(int i=0;i<n-1;i++){
        if(i == q){
            cout << "[" << A[i] << "] ";
        }
        else{
            cout << A[i] << " ";
        }
    }
    cout << A[n-1] << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3484
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <utility>

using namespace std;

int partition(vector<int> &A, int p, int r){
    int x = A[r];
    int i = p-1;
    for (int j=p; j<r; j++){
        if (A[j] <= x){
            i = i+1;
            swap(A[i],A[j]);
        }
    }
    swap(A[i+1],A[r]);

    return i+1;
}

int main(){
    int n,x1,pa;
    vector<int> A;

    cin >> n;
    for (int k=0; k<n; k++){
        cin >> x1;
        A.push_back(x1);
        if (k == n-1){
            pa = x1;
        }
    }
    int s = partition(A,0,n-1);

    for (int t=0; t<n; t++){
        if (t == s){
            cout << "[" << A[t] << "]" << " ";
        } else if (t == n-1){
            cout << A[t] << "\n";
        } else {
            cout << A[t] << " ";
        }

    }
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 10

code_snippet: #include<iostream>
using namespace std;

int cnt = 0;

int partition(int A[], int p, int r){
    int x = A[r];
    int i = p-1;
    for(int j=p;j<r;j++){
        if(A[j]<=x){
            cnt++;
            i++;
            int s=A[i];
            A[i] = A[j];
            A[j] = s;
        }
    }
    int s = A[i+1];
    A[i+1] = A[r];
    A[r] = s;
    return i+1;
}


int main(){
    int n;
    cin >> n;
    
    int A[n];
    for(int k=0;k<n;k++){
        cin >> A[k];
    }
    
    int x = A[n-1];
    
    partition(A, 0, n-1);
    
    for(int j=0;j<n;j++){
        if(j==cnt){
            cout << "[" << A[j] << "]" << " ";
        }
        else if(j!=n-1){
            cout << A[j] << " ";
        }
        else{
            cout << A[j] << endl;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3820
final_rank: 18

code_snippet: #include<iostream>

using namespace std;

int partition(int A[],int p,int r){
    int x=A[r];
    int i=p-1;
    for (int j=p;j<r;j++){
        if(A[j]<=x){
            i=i+1;
            swap(A[i],A[j]);
        }
    }
    swap(A[i+1],A[r]);
    return i+1;
}

int main(){
    int n;
    cin >> n;
    int A[n];
    for (int i=0;i<n;i++){
        cin >> A[i];
    }

    int p=partition(A,0,n-1);
    
    for(int i=0;i<n;i++){
        if(i==p){
            cout << "[" << A[i] << "]";
        }else{
            cout << A[i];
        }
        if (i !=n-1){
            cout << " ";
        }
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3740
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int partition(vector<int> &A, int p, int r){
    int x = A[r];
    int i = p - 1;

    for(int j = p; j < r; ++j){
        if(A[j] <= x){
            ++i;
            swap(A[i], A[j]);
        }
    }

    swap(A[i + 1], A[r]);
    return i + 1;
}

int main(){
    int n;
    cin >> n;
    vector<int> A(n);

    for(int k = 0; k < n; ++k){
        cin >> A[k];
    }

    int s = partition(A, 0, n - 1);

    for(int k = 0; k < s; ++k){
        cout << A[k] << " ";
    }

    cout << "[" << A[s] << "] ";

    for(int k = s + 1; k < n; ++k){
        cout << A[k];
        if(k != n - 1) cout << " ";
    }

    cout << "\n";
}
accuracy: 1
time_ms: 10
memory_kb: 3612
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <utility>
using namespace std;
int partition(vector<int>&A,int p, int r){
    int i,x;
    x = A[r];
    i = p-1;
    for(int j = p; j < r; ++j){
        if (A[j] <= x){
            i += 1;
            swap(A[i], A[j]);
        } 
    }
    swap(A[i+1], A[r]);
    return i+1;
}

int main(){
    int n;
    cin >> n;
    vector<int> A(n);
    for(int i = 0;i < n;++i){
        cin >> A[i];
    }
    int q;
    q = partition(A, 0, n-1);
    for(int k = 0; k < q; ++k){
        cout << A[k]  << " ";    
    }
    cout << "[" << A[q] << "]" << " ";
    for(int l = q+1 ; l < n; ++l){
        if (l != n-1){
            cout << A[l] << " ";
        }else{
            cout << A[l] << endl;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3640
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int partition(vector<int>& A, int p, int r){
    int x = A[r];
    int i = p-1;
    for (int j = p; j < r; j++) {
        if (A[j] <= x) {
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r]);
    return i+1;
}

int main() {
    int n, s;
    cin >> n;
    vector<int> A(n);
    for (int i = 0; i < n; i++) {
        cin >> A[i];
    }
    
    s = partition(A, 0, n-1);

    for (int i = 0; i < s; i++) {
        cout << A[i] << " ";
    }
    cout << "[" << A[s] << "] ";
    for (int j = s+1; j < n; j++) {
        cout << A[j];
        if (j != n-1) {
            cout << " ";
        } else {
            cout << endl;
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3368
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int partition(vector<int>& A, int p, int r) {
    int x = A[r];   // pivot
    int i = p - 1;

    for (int j = p; j < r; j++) {
        if (A[j] <= x) {
            i++;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r]);
    return i + 1;   // pivot の位置
}

int main() {
    int n;
    cin >> n;
    vector<int> S(n);

    for (int i = 0; i < n; i++) {
        cin >> S[i];
    }

    int pivot_index = partition(S, 0, n - 1);

    // 出力形式：
    // 左 side, [pivot], right side
    for (int j = 0; j < pivot_index; j++) {
        cout << S[j] << " ";
    }

    cout << "[" << S[pivot_index] << "] ";

    for (int k = pivot_index + 1; k < n; k++) {
        cout << S[k];
        if (k != n - 1) cout << " ";
    }

    cout << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3504
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
using namespace std; 

int partition(vector<int>&A, int p, int r){
    int x = A[r];
    int i = p - 1;
    for (int j = p; j < r; j++){
        if (A[j] <= x){
            i = i + 1;
            swap(A[i], A[j]);
        }
    }
    swap(A[i+1],A[r]);
    return i+1;
}

int main(){
    int n; cin >> n;
    vector<int> A(n);
    for (int i = 0;i < n; i++){
        cin >> A[i];
    }
    
    int q = partition (A,0,n-1);

    for (int i = 0; i < n; i++ ){
        if(i == q)cout << "[" << A[i] << "]";
        else cout << A[i];
        if (i != n-1)cout << " ";
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3684
final_rank: 20

code_snippet: #include <iostream>
#include <vector>

using namespace std;

int main(){
    
    int n;
    cin >> n;
    vector<int> A(n);

    for(int i=0;i<n;++i){
        cin >> A[i];
    }

    int p = 0;
    int r = n-1;
    int x = A[r];
    int i = p-1;
    
    for(int j=p;j<r;++j){
        if(A[j]<=x){
            i = i+1;
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }
    }
    int temp = A[i+1];
    A[i+1] = A[r];
    A[r] = temp;
    
    int q = i+1;
    
    for (int k = 0; k < n; k++) {
        if (k == q){
            cout << "[" << A[k] << "]";
        }else{
            cout << A[k];
        }
        if (k != n - 1) {
            cout << " ";
        }
    }
    cout << "\n";
    
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3636
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
// 以下の擬似コードを書く

int partition(vector<int> &A, int p, int r) {
    int x = A[r];
    int i = p - 1;
    for (int j = p; j <= r - 1; j++) {
        if (A[j] <= x) {
            i = i + 1;
            swap(A[i], A[j]);
        }
    }
    swap(A[i + 1], A[r]);
    return i + 1;
}

int main(){
    int n;
    cin >> n;
    vector<int> A(n);
    for (int i = 0; i < n; i++) {
        cin >> A[i];
    }
    
    int q = partition(A, 0, n - 1);

    for (int i = 0; i < n; i++) {
        if (i) cout << " ";
        if (i==q){
            cout << '['<<A[i]<<']' ; 
        }
        else{
        cout << A[i];
        }
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3556
final_rank: 8

code_snippet: #include <iostream>
#include <vector>

using namespace std;

int main(){
    
    int n;
    cin >> n;
    vector<int> A(n);

    for(int i=0;i<n;++i){
        cin >> A[i];
    }

    int p = 0;
    int r = n-1;
    int x = A[r];
    int i = p-1;
    
    for(int j=p;j<r;++j){
        if(A[j]<=x){
            i = i+1;
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }
    }
    int temp = A[i+1];
    A[i+1] = A[r];
    A[r] = temp;
    
    int q = i+1;
    
    for (int k = 0; k < n; k++) {
        if (k == q)
            cout << "[" << A[k] << "]";
        else
            cout << A[k];
        
        if (k != n - 1) cout << " ";
    }
    cout << "\n";
    
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3636
final_rank: 13

code_snippet: using namespace std;
#include <iostream>
#include <vector>

int partition(int A[], int p, int r){
    int x = A[r];
    int i = p-1;
    for(int j = p; j < r; j++){
        if(A[j] <= x){
            i = i+1;
            int a = A[i];
            A[i] = A[j];
            A[j] = a;
        }
    }
    int b = A[i+1];
    A[i+1] = A[r];
    A[r] = b;
    return i+1;
}

int main(){
    int n;
    cin >> n;
    vector<int> A(n);
    for(int i = 0; i < n; i++){
        cin >> A[i]; 
    }
    int c = partition(A.data(),0,n-1);
    for(int i = 0; i < n; i++){
        if (i > 0){
            cout << " ";
        }
        if(i == c){
            cout << "[" << A[i] << "]";
        }else{
            cout << A[i];
        }
    }
    cout << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3608
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int partition(vector<int>& A, int p, int r){
    int x =A[r];
    int i = p-1;
    
    for (int j =p; j<= r-1; ++j){
        if (A[j] <= x){
            ++i;
            swap(A[i], A[j]);
        }
    }
    swap(A[i+1], A[r]);
    return i+1;
}

int main(){
    int n;
    cin >> n;
    vector<int> S(n);
    
    for (int i = 0; i<n; i++){
        cin >> S[i];
    }
    
    int pI = partition(S, 0, n-1);
    for (int i = 0; i < n; ++i) {
        if (i == pI) {
            cout << "[" << S[i] << "]";
        } else {
            cout << S[i];
        }

        if (i < n - 1) {
            cout << " ";
        }
    }

    cout << endl;

    return 0;

    
}
accuracy: 1
time_ms: 10
memory_kb: 3444
final_rank: 4