Advertisement
karbaev

Untitled

Sep 4th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /**
  4.  * Created by Danila on 02.09.2016.
  5.  */
  6. public class qs {
  7.     final static int n=100000;
  8.     static int a[] =new int[n];
  9.  
  10.     static void swap(int i, int j){
  11.         int tmp=a[i];
  12.         a[i]=a[j];
  13.         a[j]=tmp;
  14.     }
  15.  
  16.     static void quickSort(int l, int r){
  17.         Random rand= new Random();
  18.  
  19.         int x= a[l+(r-l)/2];//rand.nextInt()%(r-l+1) + 1;
  20.         int i = l;
  21.         int j = r;
  22.  
  23.         while(i <= j) {
  24.             while((a[i] < x)&&(i<=r)) i++;
  25.             while((a[j] > x)&&(j>=l)) j--;
  26.             if(i <= j) {
  27.                 swap(i, j);
  28.                 i++;
  29.                 j--;
  30.             }
  31.         }
  32.         if (i<r)
  33.             quickSort(i, r);
  34.         if (l<j)
  35.             quickSort(l, j);
  36.     }
  37.  
  38.     public static void main(String[] args) {
  39.         Random rand = new Random();
  40.  
  41.         for(int i = 0; i < n; i++) {
  42.             a[i]=rand.nextInt(10);
  43.         }
  44.         for(int i = 0; i < n; i++) {
  45.             System.out.print(a[i] + " ");
  46.         }
  47.         System.out.println();
  48.         quickSort(0, n-1);
  49.         for(int i = 0; i < n; i++) {
  50.             System.out.print(a[i] + " ");
  51.         }
  52.  
  53.  
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement