Skip to content

Instantly share code, notes, and snippets.

@qkreltms
Last active February 12, 2019 07:24
Show Gist options
  • Select an option

  • Save qkreltms/702ded79e6e12e0d948ab8d2d7d2155d to your computer and use it in GitHub Desktop.

Select an option

Save qkreltms/702ded79e6e12e0d948ab8d2d7d2155d to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int N;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(bf.readLine());
arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(bf.readLine());
}
int left = 0;
int right = N - 1;
quick(arr,left,right);
for (int i = 0; i < N; i++) {
System.out.print(arr[i] + " ");
}
}
static void quick(int[] arr, int left, int right){
int pl = left;
int pr = right;
int mid = arr[(right+left) / 2];
do {
while (arr[pl] < mid) pl++;
while (arr[pr] > mid) pr--;
if (pl <= pr) {
int temp = arr[pr];
arr[pr] = arr[pl];
arr[pl] = temp;
pr--;
pl++;
}
}while (pl <= pr);
if(left < pr) quick(arr,left,pr);
if(pl< right) quick(arr,pl,right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment