Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task for ООО Бизнес Технологии
- */
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Main {
- /**
- *
- * @param A - array to sort
- * @param p - left bound , include
- * @param r - right bound, include
- */
- static void sort(int[] A, int p, int r){
- int q;
- if(p< r){
- q = (p + r) / 2;
- sort(A, p, q);
- sort(A, q + 1, r);
- merge(A, p, q, r);
- }
- }
- /**
- *
- * @param A - array to sort
- * @param l - left bound of first subarray
- * @param m - right bound of first subarray, include
- * @param r - right bound of subarray, include
- */
- static void merge(int[] A, int l, int m, int r){
- ArrayList <Integer> subArray = new ArrayList<>();
- int first = l;
- int second = m + 1;
- while((first <= m) || (second <= r)){
- if( (first <= m) && (second <= r)){
- if(A[first] < A[second]){
- subArray.add(A[first]);
- first++;
- }
- else{
- subArray.add(A[second]);
- second++;
- }
- }
- else{
- if(first <= m) {
- subArray.add(A[first]);
- first++;
- }
- else{
- subArray.add(A[second]);
- } second++;
- }
- }
- for(int i = l; i <= r; i++)
- A[i] = subArray.get(i - l);
- }
- public static void main(String[] args) {
- int n;
- Scanner scan = new Scanner(System.in);
- n = scan.nextInt();
- int[] x = new int[n];
- for(int i = 0; i < n; i++)
- x[i] = scan.nextInt();
- sort(x, 0, n - 1);
- for(int i = 0; i < n; i++)
- System.out.print(x[i] + " ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement